001: package org.springunit.examples;
002:
003: import java.io.Serializable;
004:
005: import org.apache.commons.lang.builder.CompareToBuilder;
006: import org.apache.commons.lang.builder.EqualsBuilder;
007: import org.apache.commons.lang.builder.HashCodeBuilder;
008: import org.apache.commons.lang.builder.ToStringBuilder;
009: import org.apache.commons.lang.builder.ToStringStyle;
010:
011: /**
012: * @Inv("0 <= this.hours && this.hours <= 23")
013: * @Inv("0 <= this.minutes && this.minutes <= 59")
014: * @Inv("0 <= this.seconds && this.seconds <= 59")
015: * @Inv("0 <= this.millis && this.millis <= 999")
016: */
017: public class CompositeTime implements Comparable<CompositeTime>,
018: Serializable {
019:
020: private static final long serialVersionUID = -8015727463395514135L;
021:
022: /**
023: * Create CompositeTime having hours and minutes.<br/>
024: * @Pre("0 <= hours && hours <= 23")
025: * @Pre("0 <= minutes && minutes <= 59")
026: * @param hours
027: * @param minutes
028: */
029: public CompositeTime(int hours, int minutes) {
030: this (hours, minutes, 0, 0);
031: }
032:
033: /**
034: * Create CompositeTime having hours, minutes and seconds.<br/>
035: * @Pre("0 <= hours && hours <= 23")
036: * @Pre("0 <= minutes && minutes <= 59")
037: * @Pre("0 <= seconds && seconds <= 59")
038: * @param hours
039: * @param minutes
040: * @param seconds
041: */
042: public CompositeTime(int hours, int minutes, int seconds) {
043: this (hours, minutes, seconds, 0);
044: }
045:
046: /**
047: * Create CompositeTime having hours, minutes, seconds and milliseconds.<br/>
048: * @Pre("0 <= hours && hours <= 23")
049: * @Pre("0 <= minutes && minutes <= 59")
050: * @Pre("0 <= seconds && seconds <= 59")
051: * @Pre("0 <= millis && millis <= 999")
052: * @param hours
053: * @param minutes
054: * @param seconds
055: * @param millis
056: */
057: public CompositeTime(int hours, int minutes, int seconds, int millis) {
058: assert 0 <= hours && hours <= 23 : "0 <= hours && hours <= 23";
059: assert 0 <= minutes && minutes <= 59 : "0 <= minutes && minutes <= 59";
060: assert 0 <= seconds && seconds <= 59 : "0 <= seconds && seconds <= 59";
061: assert 0 <= millis && millis <= 999 : "0 <= millis && millis <= 999";
062: this .hours = hours;
063: this .minutes = minutes;
064: this .seconds = seconds;
065: this .millis = millis;
066: }
067:
068: /**
069: * Is this less than, equal to, or greater than that?<br/>
070: * @param that Object to be compared with this.
071: * @return boolean
072: */
073: public int compareTo(CompositeTime that) {
074: return new CompareToBuilder().append(getHours(),
075: that.getHours())
076: .append(getMinutes(), that.getMinutes()).append(
077: getSeconds(), that.getSeconds()).append(
078: getMillis(), that.getMillis()).toComparison();
079: }
080:
081: /**
082: * Decrement hours.<br/>
083: * @return true if underflow detected, false otherwise
084: */
085: public boolean decrementHours() {
086: if (this .hours == 0) {
087: this .hours = 23;
088: } else {
089: this .hours--;
090: }
091: boolean underflow = this .hours == 23;
092: return underflow;
093: }
094:
095: /**
096: * Decrement milliseconds.<br/>
097: * @return true if underflow detected, false otherwise
098: */
099: public boolean decrementMillis() {
100: if (this .millis == 0) {
101: this .millis = 999;
102: } else {
103: this .millis--;
104: }
105: boolean underflow = this .millis == 999;
106: if (underflow) {
107: return decrementSeconds();
108: }
109: return underflow;
110: }
111:
112: /**
113: * Decrement minutes.<br/>
114: * @return true if underflow detected, false otherwise
115: */
116: public boolean decrementMinutes() {
117: if (this .minutes == 0) {
118: this .minutes = 59;
119: } else {
120: this .minutes--;
121: }
122: boolean underflow = this .minutes == 59;
123: if (underflow) {
124: return decrementHours();
125: }
126: return underflow;
127: }
128:
129: /**
130: * Decrement seconds.<br/>
131: * @return true if underflow detected, false otherwise
132: */
133: public boolean decrementSeconds() {
134: if (this .seconds == 0) {
135: this .seconds = 59;
136: } else {
137: this .seconds--;
138: }
139: boolean underflow = this .seconds == 59;
140: if (underflow) {
141: return decrementMinutes();
142: }
143: return underflow;
144: }
145:
146: /**
147: * Is this equal to obj?<br/>
148: */
149: public boolean equals(Object obj) {
150: if (this == obj) {
151: return true;
152: }
153: if (!(obj instanceof CompositeTime)) {
154: return false;
155: }
156: CompositeTime other = (CompositeTime) obj;
157: return new EqualsBuilder().append(getHours(), other.getHours())
158: .append(getMinutes(), other.getMinutes()).append(
159: getSeconds(), other.getSeconds()).append(
160: getMillis(), other.getMillis()).isEquals();
161: }
162:
163: /**
164: * @return Returns the hours.
165: */
166: public int getHours() {
167: return hours;
168: }
169:
170: /**
171: * @return Returns the millis.
172: */
173: public int getMillis() {
174: return millis;
175: }
176:
177: /**
178: * @return Returns the minutes.
179: */
180: public int getMinutes() {
181: return minutes;
182: }
183:
184: /**
185: * @return Returns the seconds.
186: */
187: public int getSeconds() {
188: return seconds;
189: }
190:
191: /**
192: * Hash code.<br/>
193: */
194: public int hashCode() {
195: return new HashCodeBuilder(17, 37).append(getHours()).append(
196: getMinutes()).append(getSeconds()).append(getMillis())
197: .toHashCode();
198: }
199:
200: /**
201: * Increment hours.<br/>
202: * @return true if overflow detected, false otherwise
203: */
204: public boolean incrementHours() {
205: this .hours = ((this .hours + 1) % 24);
206: boolean overflow = this .hours == 0;
207: return overflow;
208: }
209:
210: /**
211: * Increment milliseconds.<br/>
212: * @return true if overflow detected, false otherwise
213: */
214: public boolean incrementMillis() {
215: this .millis = ((this .millis + 1) % 1000);
216: boolean overflow = this .millis == 0;
217: if (overflow) {
218: return incrementSeconds();
219: }
220: return overflow;
221: }
222:
223: /**
224: * Increment minutes.<br/>
225: * @return true if overflow detected, false otherwise
226: */
227: public boolean incrementMinutes() {
228: this .minutes = ((this .minutes + 1) % 60);
229: boolean overflow = this .minutes == 0;
230: if (overflow) {
231: return incrementHours();
232: }
233: return overflow;
234: }
235:
236: /**
237: * Increment seconds.<br/>
238: * @return true if overflow detected, false otherwise
239: */
240: public boolean incrementSeconds() {
241: this .seconds = ((this .seconds + 1) % 60);
242: boolean overflow = this .seconds == 0;
243: if (overflow) {
244: return incrementMinutes();
245: }
246: return overflow;
247: }
248:
249: /**
250: * @Pre("0 <= hours && hours <= 23")
251: * @param hours The hours to set.
252: */
253: public void setHours(int hours) {
254: this .hours = hours;
255: }
256:
257: /**
258: * @Pre("0 <= millis && millis <= 999")
259: * @param millis The millis to set.
260: */
261: public void setMillis(int millis) {
262: this .millis = millis;
263: }
264:
265: /**
266: * @Pre("0 <= minutes && minutes <= 59")
267: * @param minutes The minutes to set.
268: */
269: public void setMinutes(int minutes) {
270: this .minutes = minutes;
271: }
272:
273: /**
274: * @Pre("0 <= seconds && seconds <= 59")
275: * @param seconds The seconds to set.
276: */
277: public void setSeconds(int seconds) {
278: this .seconds = seconds;
279: }
280:
281: /**
282: * String representation.<br/>
283: */
284: public String toString() {
285: return new ToStringBuilder(this , ToStringStyle.MULTI_LINE_STYLE)
286: .append("hours", getHours()).append("minutes",
287: getMinutes()).append("seconds", getSeconds())
288: .append("millis", getMillis()).toString();
289: }
290:
291: private int hours;
292: private int millis;
293: private int minutes;
294: private int seconds;
295:
296: }
|