001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.dynatable.client;
017:
018: import com.google.gwt.user.client.rpc.IsSerializable;
019:
020: /**
021: * Hold relevant data for a time slot. This class is intended to be serialized
022: * as part of RPC calls.
023: */
024: public class TimeSlot implements IsSerializable, Comparable<TimeSlot> {
025:
026: private static final transient String[] DAYS = new String[] {
027: "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
028:
029: private int endMinutes;
030:
031: private int startMinutes;
032:
033: private int zeroBasedDayOfWeek;
034:
035: public TimeSlot() {
036: }
037:
038: public TimeSlot(int zeroBasedDayOfWeek, int startMinutes,
039: int endMinutes) {
040: this .zeroBasedDayOfWeek = zeroBasedDayOfWeek;
041: this .startMinutes = startMinutes;
042: this .endMinutes = endMinutes;
043: }
044:
045: public int compareTo(TimeSlot o) {
046: if (zeroBasedDayOfWeek < o.zeroBasedDayOfWeek) {
047: return -1;
048: } else if (zeroBasedDayOfWeek > o.zeroBasedDayOfWeek) {
049: return 1;
050: } else {
051: if (startMinutes < o.startMinutes) {
052: return -1;
053: } else if (startMinutes > o.startMinutes) {
054: return 1;
055: }
056: }
057:
058: return 0;
059: }
060:
061: public int getDayOfWeek() {
062: return zeroBasedDayOfWeek;
063: }
064:
065: public String getDescription() {
066: return DAYS[zeroBasedDayOfWeek] + " "
067: + getHrsMins(startMinutes) + "-"
068: + getHrsMins(endMinutes);
069: }
070:
071: public int getEndMinutes() {
072: return endMinutes;
073: }
074:
075: public int getStartMinutes() {
076: return startMinutes;
077: }
078:
079: public void setDayOfWeek(int zeroBasedDayOfWeek) {
080: if (0 <= zeroBasedDayOfWeek && zeroBasedDayOfWeek < 7) {
081: this .zeroBasedDayOfWeek = zeroBasedDayOfWeek;
082: } else {
083: throw new IllegalArgumentException(
084: "day must be in the range 0-6");
085: }
086: }
087:
088: public void setEndMinutes(int endMinutes) {
089: this .endMinutes = endMinutes;
090: }
091:
092: public void setStartMinutes(int startMinutes) {
093: this .startMinutes = startMinutes;
094: }
095:
096: private String getHrsMins(int mins) {
097: int hrs = mins / 60;
098: if (hrs > 12) {
099: hrs -= 12;
100: }
101:
102: int remainder = mins % 60;
103:
104: return hrs
105: + ":"
106: + (remainder < 10 ? "0" + remainder : String
107: .valueOf(remainder));
108: }
109: }
|