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