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.util.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: * @deprecated This class has been repackaged at
043: * <code>com.liferay.portal.kernel.cal</code>.
044: *
045: */
046: public class Duration implements Cloneable, Serializable {
047:
048: /**
049: * Field weeks
050: */
051: private int weeks;
052:
053: /**
054: * Field days
055: */
056: private int days;
057:
058: /**
059: * Field hours
060: */
061: private int hours;
062:
063: /**
064: * Field minutes
065: */
066: private int minutes;
067:
068: /**
069: * Field seconds
070: */
071: private int seconds;
072:
073: /**
074: * Field SECONDS_PER_MINUTE
075: */
076: private final static int SECONDS_PER_MINUTE = 60;
077:
078: /**
079: * Field MINUTES_PER_HOUR
080: */
081: private final static int MINUTES_PER_HOUR = 60;
082:
083: /**
084: * Field HOURS_PER_DAY
085: */
086: private final static int HOURS_PER_DAY = 24;
087:
088: /**
089: * Field DAYS_PER_WEEK
090: */
091: private final static int DAYS_PER_WEEK = 7;
092:
093: /**
094: * Field MILLIS_PER_SECOND
095: */
096: private final static int MILLIS_PER_SECOND = 1000;
097:
098: /**
099: * Field MILLIS_PER_MINUTE
100: */
101: private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
102: * MILLIS_PER_SECOND;
103:
104: /**
105: * Field MILLIS_PER_HOUR
106: */
107: private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
108: * MILLIS_PER_MINUTE;
109:
110: /**
111: * Field MILLIS_PER_DAY
112: */
113: private final static int MILLIS_PER_DAY = HOURS_PER_DAY
114: * MILLIS_PER_HOUR;
115:
116: /**
117: * Field MILLIS_PER_WEEK
118: */
119: private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK
120: * MILLIS_PER_DAY;
121:
122: /**
123: * Constructor Duration
124: *
125: *
126: */
127: public Duration() {
128:
129: /* Zero-initialization of all fields happens by default */
130:
131: }
132:
133: /**
134: * Constructor Duration
135: *
136: *
137: * @param d
138: * @param h
139: * @param m
140: * @param s
141: *
142: */
143: public Duration(int d, int h, int m, int s) {
144: days = d;
145: hours = h;
146: minutes = m;
147: seconds = s;
148: }
149:
150: /**
151: * Constructor Duration
152: *
153: *
154: * @param h
155: * @param m
156: * @param s
157: *
158: */
159: public Duration(int h, int m, int s) {
160: this (0, h, m, s);
161: }
162:
163: /**
164: * Constructor Duration
165: *
166: *
167: * @param w
168: *
169: */
170: public Duration(int w) {
171: weeks = w;
172: }
173:
174: /**
175: * Method clear
176: *
177: *
178: */
179: public void clear() {
180: weeks = 0;
181: days = 0;
182: hours = 0;
183: minutes = 0;
184: seconds = 0;
185: };
186:
187: /**
188: * Method getWeeks
189: *
190: *
191: * @return int
192: *
193: */
194: public int getWeeks() {
195: return weeks;
196: }
197:
198: /**
199: * Method setWeeks
200: *
201: *
202: * @param w
203: *
204: */
205: public void setWeeks(int w) {
206: if (w < 0) {
207: throw new IllegalArgumentException(
208: "Week value out of range");
209: }
210:
211: checkWeeksOkay(w);
212:
213: weeks = w;
214: }
215:
216: /**
217: * Method getDays
218: *
219: *
220: * @return int
221: *
222: */
223: public int getDays() {
224: return days;
225: }
226:
227: /**
228: * Method setDays
229: *
230: *
231: * @param d
232: *
233: */
234: public void setDays(int d) {
235: if (d < 0) {
236: throw new IllegalArgumentException("Day value out of range");
237: }
238:
239: checkNonWeeksOkay(d);
240:
241: days = d;
242:
243: normalize();
244: }
245:
246: /**
247: * Method getHours
248: *
249: *
250: * @return int
251: *
252: */
253: public int getHours() {
254: return hours;
255: }
256:
257: /**
258: * Method setHours
259: *
260: *
261: * @param h
262: *
263: */
264: public void setHours(int h) {
265: if (h < 0) {
266: throw new IllegalArgumentException(
267: "Hour value out of range");
268: }
269:
270: checkNonWeeksOkay(h);
271:
272: hours = h;
273:
274: normalize();
275: }
276:
277: /**
278: * Method getMinutes
279: *
280: *
281: * @return int
282: *
283: */
284: public int getMinutes() {
285: return minutes;
286: }
287:
288: /**
289: * Method setMinutes
290: *
291: *
292: * @param m
293: *
294: */
295: public void setMinutes(int m) {
296: if (m < 0) {
297: throw new IllegalArgumentException(
298: "Minute value out of range");
299: }
300:
301: checkNonWeeksOkay(m);
302:
303: minutes = m;
304:
305: normalize();
306: }
307:
308: /**
309: * Method getSeconds
310: *
311: *
312: * @return int
313: *
314: */
315: public int getSeconds() {
316: return seconds;
317: }
318:
319: /**
320: * Method setSeconds
321: *
322: *
323: * @param s
324: *
325: */
326: public void setSeconds(int s) {
327: if (s < 0) {
328: throw new IllegalArgumentException(
329: "Second value out of range");
330: }
331:
332: checkNonWeeksOkay(s);
333:
334: seconds = s;
335:
336: normalize();
337: }
338:
339: /**
340: * Method getInterval
341: *
342: *
343: * @return long
344: *
345: */
346: public long getInterval() {
347: return seconds * MILLIS_PER_SECOND + minutes
348: * MILLIS_PER_MINUTE + hours * MILLIS_PER_HOUR + days
349: * MILLIS_PER_DAY + weeks * MILLIS_PER_WEEK;
350: }
351:
352: /**
353: * Method setInterval
354: *
355: *
356: * @param millis
357: *
358: */
359: public void setInterval(long millis) {
360: if (millis < 0) {
361: throw new IllegalArgumentException(
362: "Negative-length interval");
363: }
364:
365: clear();
366:
367: days = (int) (millis / MILLIS_PER_DAY);
368: seconds = (int) ((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
369:
370: normalize();
371: }
372:
373: /**
374: * Method normalize
375: *
376: *
377: */
378: protected void normalize() {
379: minutes += seconds / SECONDS_PER_MINUTE;
380: seconds %= SECONDS_PER_MINUTE;
381: hours += minutes / MINUTES_PER_HOUR;
382: minutes %= MINUTES_PER_HOUR;
383: days += hours / HOURS_PER_DAY;
384: hours %= HOURS_PER_DAY;
385: }
386:
387: /**
388: * Method checkWeeksOkay
389: *
390: *
391: * @param f
392: *
393: */
394: protected void checkWeeksOkay(int f) {
395: if ((f != 0)
396: && ((days != 0) || (hours != 0) || (minutes != 0) || (seconds != 0))) {
397: throw new IllegalStateException(
398: "Weeks and non-weeks are incompatible");
399: }
400: }
401:
402: /**
403: * Method checkNonWeeksOkay
404: *
405: *
406: * @param f
407: *
408: */
409: protected void checkNonWeeksOkay(int f) {
410: if ((f != 0) && (weeks != 0)) {
411: throw new IllegalStateException(
412: "Weeks and non-weeks are incompatible");
413: }
414: }
415:
416: /**
417: * Method clone
418: *
419: *
420: * @return Object
421: *
422: */
423: public Object clone() {
424: try {
425: Duration other = (Duration) super .clone();
426:
427: other.weeks = weeks;
428: other.days = days;
429: other.hours = hours;
430: other.minutes = minutes;
431: other.seconds = seconds;
432:
433: return other;
434: } catch (CloneNotSupportedException e) {
435: throw new InternalError();
436: }
437: }
438:
439: /**
440: * Method toString
441: *
442: *
443: * @return String
444: *
445: */
446: public String toString() {
447: StringMaker sm = new StringMaker();
448:
449: sm.append(getClass().getName());
450: sm.append("[weeks=");
451: sm.append(weeks);
452: sm.append(",days=");
453: sm.append(days);
454: sm.append(",hours=");
455: sm.append(hours);
456: sm.append(",minutes=");
457: sm.append(minutes);
458: sm.append(",seconds=");
459: sm.append(seconds);
460: sm.append("]");
461:
462: return sm.toString();
463: }
464:
465: }
|