001: /*
002: * Copyright (c) 2000, Columbia University. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright
008: * notice, this list of conditions and the following disclaimer.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: *
014: * 3. Neither the name of the University nor the names of its contributors
015: * may be used to endorse or promote products derived from this software
016: * without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
019: * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
027: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
028: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.liferay.portal.kernel.cal;
032:
033: import com.liferay.portal.kernel.util.StringMaker;
034:
035: import java.io.Serializable;
036:
037: /**
038: * <a href="Duration.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Jonathan Lennox
041: *
042: */
043: public class Duration implements Cloneable, Serializable {
044:
045: /**
046: * Field weeks
047: */
048: private int weeks;
049:
050: /**
051: * Field days
052: */
053: private int days;
054:
055: /**
056: * Field hours
057: */
058: private int hours;
059:
060: /**
061: * Field minutes
062: */
063: private int minutes;
064:
065: /**
066: * Field seconds
067: */
068: private int seconds;
069:
070: /**
071: * Field SECONDS_PER_MINUTE
072: */
073: private final static int SECONDS_PER_MINUTE = 60;
074:
075: /**
076: * Field MINUTES_PER_HOUR
077: */
078: private final static int MINUTES_PER_HOUR = 60;
079:
080: /**
081: * Field HOURS_PER_DAY
082: */
083: private final static int HOURS_PER_DAY = 24;
084:
085: /**
086: * Field DAYS_PER_WEEK
087: */
088: private final static int DAYS_PER_WEEK = 7;
089:
090: /**
091: * Field MILLIS_PER_SECOND
092: */
093: private final static int MILLIS_PER_SECOND = 1000;
094:
095: /**
096: * Field MILLIS_PER_MINUTE
097: */
098: private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
099: * MILLIS_PER_SECOND;
100:
101: /**
102: * Field MILLIS_PER_HOUR
103: */
104: private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
105: * MILLIS_PER_MINUTE;
106:
107: /**
108: * Field MILLIS_PER_DAY
109: */
110: private final static int MILLIS_PER_DAY = HOURS_PER_DAY
111: * MILLIS_PER_HOUR;
112:
113: /**
114: * Field MILLIS_PER_WEEK
115: */
116: private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK
117: * MILLIS_PER_DAY;
118:
119: /**
120: * Constructor Duration
121: *
122: *
123: */
124: public Duration() {
125:
126: /* Zero-initialization of all fields happens by default */
127:
128: }
129:
130: /**
131: * Constructor Duration
132: *
133: *
134: * @param d
135: * @param h
136: * @param m
137: * @param s
138: *
139: */
140: public Duration(int d, int h, int m, int s) {
141: days = d;
142: hours = h;
143: minutes = m;
144: seconds = s;
145: }
146:
147: /**
148: * Constructor Duration
149: *
150: *
151: * @param h
152: * @param m
153: * @param s
154: *
155: */
156: public Duration(int h, int m, int s) {
157: this (0, h, m, s);
158: }
159:
160: /**
161: * Constructor Duration
162: *
163: *
164: * @param w
165: *
166: */
167: public Duration(int w) {
168: weeks = w;
169: }
170:
171: /**
172: * Method clear
173: *
174: *
175: */
176: public void clear() {
177: weeks = 0;
178: days = 0;
179: hours = 0;
180: minutes = 0;
181: seconds = 0;
182: };
183:
184: /**
185: * Method getWeeks
186: *
187: *
188: * @return int
189: *
190: */
191: public int getWeeks() {
192: return weeks;
193: }
194:
195: /**
196: * Method setWeeks
197: *
198: *
199: * @param w
200: *
201: */
202: public void setWeeks(int w) {
203: if (w < 0) {
204: throw new IllegalArgumentException(
205: "Week value out of range");
206: }
207:
208: checkWeeksOkay(w);
209:
210: weeks = w;
211: }
212:
213: /**
214: * Method getDays
215: *
216: *
217: * @return int
218: *
219: */
220: public int getDays() {
221: return days;
222: }
223:
224: /**
225: * Method setDays
226: *
227: *
228: * @param d
229: *
230: */
231: public void setDays(int d) {
232: if (d < 0) {
233: throw new IllegalArgumentException("Day value out of range");
234: }
235:
236: checkNonWeeksOkay(d);
237:
238: days = d;
239:
240: normalize();
241: }
242:
243: /**
244: * Method getHours
245: *
246: *
247: * @return int
248: *
249: */
250: public int getHours() {
251: return hours;
252: }
253:
254: /**
255: * Method setHours
256: *
257: *
258: * @param h
259: *
260: */
261: public void setHours(int h) {
262: if (h < 0) {
263: throw new IllegalArgumentException(
264: "Hour value out of range");
265: }
266:
267: checkNonWeeksOkay(h);
268:
269: hours = h;
270:
271: normalize();
272: }
273:
274: /**
275: * Method getMinutes
276: *
277: *
278: * @return int
279: *
280: */
281: public int getMinutes() {
282: return minutes;
283: }
284:
285: /**
286: * Method setMinutes
287: *
288: *
289: * @param m
290: *
291: */
292: public void setMinutes(int m) {
293: if (m < 0) {
294: throw new IllegalArgumentException(
295: "Minute value out of range");
296: }
297:
298: checkNonWeeksOkay(m);
299:
300: minutes = m;
301:
302: normalize();
303: }
304:
305: /**
306: * Method getSeconds
307: *
308: *
309: * @return int
310: *
311: */
312: public int getSeconds() {
313: return seconds;
314: }
315:
316: /**
317: * Method setSeconds
318: *
319: *
320: * @param s
321: *
322: */
323: public void setSeconds(int s) {
324: if (s < 0) {
325: throw new IllegalArgumentException(
326: "Second value out of range");
327: }
328:
329: checkNonWeeksOkay(s);
330:
331: seconds = s;
332:
333: normalize();
334: }
335:
336: /**
337: * Method getInterval
338: *
339: *
340: * @return long
341: *
342: */
343: public long getInterval() {
344: return seconds * MILLIS_PER_SECOND + minutes
345: * MILLIS_PER_MINUTE + hours * MILLIS_PER_HOUR + days
346: * MILLIS_PER_DAY + weeks * MILLIS_PER_WEEK;
347: }
348:
349: /**
350: * Method setInterval
351: *
352: *
353: * @param millis
354: *
355: */
356: public void setInterval(long millis) {
357: if (millis < 0) {
358: throw new IllegalArgumentException(
359: "Negative-length interval");
360: }
361:
362: clear();
363:
364: days = (int) (millis / MILLIS_PER_DAY);
365: seconds = (int) ((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
366:
367: normalize();
368: }
369:
370: /**
371: * Method normalize
372: *
373: *
374: */
375: protected void normalize() {
376: minutes += seconds / SECONDS_PER_MINUTE;
377: seconds %= SECONDS_PER_MINUTE;
378: hours += minutes / MINUTES_PER_HOUR;
379: minutes %= MINUTES_PER_HOUR;
380: days += hours / HOURS_PER_DAY;
381: hours %= HOURS_PER_DAY;
382: }
383:
384: /**
385: * Method checkWeeksOkay
386: *
387: *
388: * @param f
389: *
390: */
391: protected void checkWeeksOkay(int f) {
392: if ((f != 0)
393: && ((days != 0) || (hours != 0) || (minutes != 0) || (seconds != 0))) {
394: throw new IllegalStateException(
395: "Weeks and non-weeks are incompatible");
396: }
397: }
398:
399: /**
400: * Method checkNonWeeksOkay
401: *
402: *
403: * @param f
404: *
405: */
406: protected void checkNonWeeksOkay(int f) {
407: if ((f != 0) && (weeks != 0)) {
408: throw new IllegalStateException(
409: "Weeks and non-weeks are incompatible");
410: }
411: }
412:
413: /**
414: * Method clone
415: *
416: *
417: * @return Object
418: *
419: */
420: public Object clone() {
421: try {
422: Duration other = (Duration) super .clone();
423:
424: other.weeks = weeks;
425: other.days = days;
426: other.hours = hours;
427: other.minutes = minutes;
428: other.seconds = seconds;
429:
430: return other;
431: } catch (CloneNotSupportedException e) {
432: throw new InternalError();
433: }
434: }
435:
436: /**
437: * Method toString
438: *
439: *
440: * @return String
441: *
442: */
443: public String toString() {
444: StringMaker sm = new StringMaker();
445:
446: sm.append(getClass().getName());
447: sm.append("[weeks=");
448: sm.append(weeks);
449: sm.append(",days=");
450: sm.append(days);
451: sm.append(",hours=");
452: sm.append(hours);
453: sm.append(",minutes=");
454: sm.append(minutes);
455: sm.append(",seconds=");
456: sm.append(seconds);
457: sm.append("]");
458:
459: return sm.toString();
460: }
461:
462: }
|