1 16 package com.google.gwt.sample.dynatable.client; 17 18 import com.google.gwt.user.client.rpc.IsSerializable; 19 20 24 public class TimeSlot implements IsSerializable, Comparable { 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 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 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 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 ("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 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 |