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: public class CompositeDateTime implements
012: Comparable<CompositeDateTime>, Serializable {
013:
014: private static final long serialVersionUID = 2646861090982558639L;
015:
016: /**
017: * Create composite date/time from <code>date</code>
018: * and <code>time</code>.<br/>
019: * @Pre("date != null")
020: * @Pre("time != null")
021: * @param date
022: * @param time
023: */
024: public CompositeDateTime(CompositeDate date, CompositeTime time) {
025: assert date != null : "date != null";
026: assert time != null : "time != null";
027: this .date = date;
028: this .time = time;
029: }
030:
031: /**
032: * Is this less than, equal to, or greater than that?<br/>
033: * @param that Object to be compared with this.
034: * @return boolean
035: */
036: public int compareTo(CompositeDateTime that) {
037: return new CompareToBuilder().append(getDate(), that.getDate())
038: .append(getTime(), that.getTime()).toComparison();
039: }
040:
041: /**
042: * Decrement by one day.<br/>
043: * @throws DateUnderflowException if underflow detected
044: */
045: public void decrementDay() throws DateUnderflowException {
046: this .date.decrementDay();
047: }
048:
049: /**
050: * Decrement by one month.<br/>
051: * @throws DateUnderflowException if underflow detected
052: */
053: public void decrementMonth() throws DateUnderflowException {
054: this .date.decrementMonth();
055: }
056:
057: /**
058: * Decrement by one year.
059: * @throws DateUnderflowException if underflow detected
060: */
061: public void decrementYear() throws DateUnderflowException {
062: this .date.decrementYear();
063: }
064:
065: /**
066: * Decrement hours.<br/>
067: * @throws DateUnderflowException if underflow detected
068: */
069: public void decrementHours() throws DateUnderflowException {
070: boolean underflow = this .time.decrementHours();
071: if (underflow) {
072: this .date.decrementDay();
073: }
074: }
075:
076: /**
077: * Decrement milliseconds.<br/>
078: * @throws DateUnderflowException if underflow detected
079: */
080: public void decrementMillis() throws DateUnderflowException {
081: boolean underflow = this .time.decrementMillis();
082: if (underflow) {
083: this .date.decrementDay();
084: }
085: }
086:
087: /**
088: * Decrement minutes.<br/>
089: * @throws DateUnderflowException if underflow detected
090: */
091: public void decrementMinutes() throws DateUnderflowException {
092: boolean underflow = this .time.decrementMinutes();
093: if (underflow) {
094: this .date.decrementDay();
095: }
096: }
097:
098: /**
099: * Decrement seconds.<br/>
100: * @throws DateUnderflowException if underflow detected
101: */
102: public void decrementSeconds() throws DateUnderflowException {
103: boolean underflow = this .time.decrementSeconds();
104: if (underflow) {
105: this .date.decrementDay();
106: }
107: }
108:
109: /**
110: * Is this equal to obj?<br/>
111: */
112: public boolean equals(Object obj) {
113: if (this == obj) {
114: return true;
115: }
116: if (!(obj instanceof CompositeDateTime)) {
117: return false;
118: }
119: CompositeDateTime other = (CompositeDateTime) obj;
120: return new EqualsBuilder().append(getDate(), other.getDate())
121: .append(getTime(), other.getTime()).isEquals();
122: }
123:
124: /**
125: * @return Returns the date.
126: */
127: public CompositeDate getDate() {
128: return date;
129: }
130:
131: /**
132: * @return Returns the time.
133: */
134: public CompositeTime getTime() {
135: return time;
136: }
137:
138: /**
139: * Hash code.<br/>
140: */
141: public int hashCode() {
142: return new HashCodeBuilder(17, 37).append(getDate()).append(
143: getTime()).toHashCode();
144: }
145:
146: /**
147: * Increment by one day.<br/>
148: * @throws DateOverflowException if overflow detected
149: */
150: public void incrementDay() throws DateOverflowException {
151: this .date.incrementDay();
152: }
153:
154: /**
155: * Increment by one month.<br/>
156: * @throws DateOverflowException if overflow detected
157: */
158: public void incrementMonth() throws DateOverflowException {
159: this .date.incrementMonth();
160: }
161:
162: /**
163: * Increment by one year.<br/>
164: * @throws DateOverflowException if overflow detected
165: */
166: public void incrementYear() throws DateOverflowException {
167: this .date.incrementYear();
168: }
169:
170: /**
171: * Increment hours.<br/>
172: * @throws DateOverflowException if overflow detected
173: */
174: public void incrementHours() throws DateOverflowException {
175: boolean overflow = this .time.incrementHours();
176: if (overflow) {
177: this .date.incrementDay();
178: }
179: }
180:
181: /**
182: * Increment milliseconds.<br/>
183: * @throws DateOverflowException if overflow detected
184: */
185: public void incrementMillis() throws DateOverflowException {
186: boolean overflow = this .time.incrementMillis();
187: if (overflow) {
188: this .date.incrementDay();
189: }
190: }
191:
192: /**
193: * Increment minutes.<br/>
194: * @throws DateOverflowException if overflow detected
195: */
196: public void incrementMinutes() throws DateOverflowException {
197: boolean overflow = this .time.incrementMinutes();
198: if (overflow) {
199: this .date.incrementDay();
200: }
201: }
202:
203: /**
204: * Increment seconds.<br/>
205: * @throws DateOverflowException if overflow detected
206: */
207: public void incrementSeconds() throws DateOverflowException {
208: boolean overflow = this .time.incrementSeconds();
209: if (overflow) {
210: this .date.incrementDay();
211: }
212: }
213:
214: /**
215: * @Pre("date != null")
216: * @param date The date to set.
217: */
218: public void setDate(CompositeDate date) {
219: assert date != null : "date != null";
220: this .date = date;
221: }
222:
223: /**
224: * @Pre("time != null")
225: * @param time The time to set.
226: */
227: public void setTime(CompositeTime time) {
228: this .time = time;
229: }
230:
231: /**
232: * String representation.<br/>
233: */
234: public String toString() {
235: return new ToStringBuilder(this , ToStringStyle.MULTI_LINE_STYLE)
236: .append("date", getDate()).append("time", getTime())
237: .toString();
238: }
239:
240: private CompositeDate date;
241: private CompositeTime time;
242:
243: }
|