KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > dynatable > client > TimeSlot


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.sample.dynatable.client;
17
18 import com.google.gwt.user.client.rpc.IsSerializable;
19
20 /**
21  * Hold relevant data for a time slot. This class is intended to be serialized
22  * as part of RPC calls.
23  */

24 public class TimeSlot implements IsSerializable, Comparable JavaDoc {
25
26   private static final transient String JavaDoc[] DAYS = new String JavaDoc[] {
27       "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"};
28
29   private int endMinutes;
30
31   private int startMinutes;
32
33   private int zeroBasedDayOfWeek;
34
35   public TimeSlot() {
36   }
37
38   public TimeSlot(int zeroBasedDayOfWeek, int startMinutes, int endMinutes) {
39     this.zeroBasedDayOfWeek = zeroBasedDayOfWeek;
40     this.startMinutes = startMinutes;
41     this.endMinutes = endMinutes;
42   }
43
44   public int compareTo(Object JavaDoc o) {
45     TimeSlot other = (TimeSlot) o;
46     if (zeroBasedDayOfWeek < other.zeroBasedDayOfWeek) {
47       return -1;
48     } else if (zeroBasedDayOfWeek > other.zeroBasedDayOfWeek) {
49       return 1;
50     } else {
51       if (startMinutes < other.startMinutes) {
52         return -1;
53       } else if (startMinutes > other.startMinutes) {
54         return 1;
55       }
56     }
57
58     return 0;
59   }
60
61   public int getDayOfWeek() {
62     return zeroBasedDayOfWeek;
63   }
64
65   public String JavaDoc getDescription() {
66     return DAYS[zeroBasedDayOfWeek] + " " + getHrsMins(startMinutes) + "-"
67         + getHrsMins(endMinutes);
68   }
69
70   public int getEndMinutes() {
71     return endMinutes;
72   }
73
74   public int getStartMinutes() {
75     return startMinutes;
76   }
77
78   public void setDayOfWeek(int zeroBasedDayOfWeek) {
79     if (0 <= zeroBasedDayOfWeek && zeroBasedDayOfWeek < 7) {
80       this.zeroBasedDayOfWeek = zeroBasedDayOfWeek;
81     } else {
82       throw new IllegalArgumentException JavaDoc("day must be in the range 0-6");
83     }
84   }
85
86   public void setEndMinutes(int endMinutes) {
87     this.endMinutes = endMinutes;
88   }
89
90   public void setStartMinutes(int startMinutes) {
91     this.startMinutes = startMinutes;
92   }
93
94   private String JavaDoc getHrsMins(int mins) {
95     int hrs = mins / 60;
96     if (hrs > 12) {
97       hrs -= 12;
98     }
99
100     int remainder = mins % 60;
101
102     return hrs + ":"
103         + (remainder < 10 ? "0" + remainder : String.valueOf(remainder));
104   }
105 }
106
Popular Tags