| Duration.java |
1 /*
2 * Copyright (c) 2000, Columbia University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
19 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 package com.liferay.portal.kernel.cal;
32
33 import com.liferay.portal.kernel.util.StringMaker;
34
35 import java.io.Serializable;
36
37 /**
38 * <a href="Duration.java.html"><b><i>View Source</i></b></a>
39 *
40 * @author Jonathan Lennox
41 *
42 */
43 public class Duration implements Cloneable, Serializable {
44
45 /**
46 * Field weeks
47 */
48 private int weeks;
49
50 /**
51 * Field days
52 */
53 private int days;
54
55 /**
56 * Field hours
57 */
58 private int hours;
59
60 /**
61 * Field minutes
62 */
63 private int minutes;
64
65 /**
66 * Field seconds
67 */
68 private int seconds;
69
70 /**
71 * Field SECONDS_PER_MINUTE
72 */
73 private final static int SECONDS_PER_MINUTE = 60;
74
75 /**
76 * Field MINUTES_PER_HOUR
77 */
78 private final static int MINUTES_PER_HOUR = 60;
79
80 /**
81 * Field HOURS_PER_DAY
82 */
83 private final static int HOURS_PER_DAY = 24;
84
85 /**
86 * Field DAYS_PER_WEEK
87 */
88 private final static int DAYS_PER_WEEK = 7;
89
90 /**
91 * Field MILLIS_PER_SECOND
92 */
93 private final static int MILLIS_PER_SECOND = 1000;
94
95 /**
96 * Field MILLIS_PER_MINUTE
97 */
98 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
99 * 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 * MILLIS_PER_HOUR;
111
112 /**
113 * Field MILLIS_PER_WEEK
114 */
115 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
116
117 /**
118 * Constructor Duration
119 *
120 *
121 */
122 public Duration() {
123
124 /* Zero-initialization of all fields happens by default */
125
126 }
127
128 /**
129 * Constructor Duration
130 *
131 *
132 * @param d
133 * @param h
134 * @param m
135 * @param s
136 *
137 */
138 public Duration(int d, int h, int m, int s) {
139 days = d;
140 hours = h;
141 minutes = m;
142 seconds = s;
143 }
144
145 /**
146 * Constructor Duration
147 *
148 *
149 * @param h
150 * @param m
151 * @param s
152 *
153 */
154 public Duration(int h, int m, int s) {
155 this(0, h, m, s);
156 }
157
158 /**
159 * Constructor Duration
160 *
161 *
162 * @param w
163 *
164 */
165 public Duration(int w) {
166 weeks = w;
167 }
168
169 /**
170 * Method clear
171 *
172 *
173 */
174 public void clear() {
175 weeks = 0;
176 days = 0;
177 hours = 0;
178 minutes = 0;
179 seconds = 0;
180 }
181 ;
182
183 /**
184 * Method getWeeks
185 *
186 *
187 * @return int
188 *
189 */
190 public int getWeeks() {
191 return weeks;
192 }
193
194 /**
195 * Method setWeeks
196 *
197 *
198 * @param w
199 *
200 */
201 public void setWeeks(int w) {
202 if (w < 0) {
203 throw new IllegalArgumentException("Week value out of range");
204 }
205
206 checkWeeksOkay(w);
207
208 weeks = w;
209 }
210
211 /**
212 * Method getDays
213 *
214 *
215 * @return int
216 *
217 */
218 public int getDays() {
219 return days;
220 }
221
222 /**
223 * Method setDays
224 *
225 *
226 * @param d
227 *
228 */
229 public void setDays(int d) {
230 if (d < 0) {
231 throw new IllegalArgumentException("Day value out of range");
232 }
233
234 checkNonWeeksOkay(d);
235
236 days = d;
237
238 normalize();
239 }
240
241 /**
242 * Method getHours
243 *
244 *
245 * @return int
246 *
247 */
248 public int getHours() {
249 return hours;
250 }
251
252 /**
253 * Method setHours
254 *
255 *
256 * @param h
257 *
258 */
259 public void setHours(int h) {
260 if (h < 0) {
261 throw new IllegalArgumentException("Hour value out of range");
262 }
263
264 checkNonWeeksOkay(h);
265
266 hours = h;
267
268 normalize();
269 }
270
271 /**
272 * Method getMinutes
273 *
274 *
275 * @return int
276 *
277 */
278 public int getMinutes() {
279 return minutes;
280 }
281
282 /**
283 * Method setMinutes
284 *
285 *
286 * @param m
287 *
288 */
289 public void setMinutes(int m) {
290 if (m < 0) {
291 throw new IllegalArgumentException("Minute value out of range");
292 }
293
294 checkNonWeeksOkay(m);
295
296 minutes = m;
297
298 normalize();
299 }
300
301 /**
302 * Method getSeconds
303 *
304 *
305 * @return int
306 *
307 */
308 public int getSeconds() {
309 return seconds;
310 }
311
312 /**
313 * Method setSeconds
314 *
315 *
316 * @param s
317 *
318 */
319 public void setSeconds(int s) {
320 if (s < 0) {
321 throw new IllegalArgumentException("Second value out of range");
322 }
323
324 checkNonWeeksOkay(s);
325
326 seconds = s;
327
328 normalize();
329 }
330
331 /**
332 * Method getInterval
333 *
334 *
335 * @return long
336 *
337 */
338 public long getInterval() {
339 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
340 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
341 + weeks * MILLIS_PER_WEEK;
342 }
343
344 /**
345 * Method setInterval
346 *
347 *
348 * @param millis
349 *
350 */
351 public void setInterval(long millis) {
352 if (millis < 0) {
353 throw new IllegalArgumentException("Negative-length interval");
354 }
355
356 clear();
357
358 days = (int)(millis / MILLIS_PER_DAY);
359 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
360
361 normalize();
362 }
363
364 /**
365 * Method normalize
366 *
367 *
368 */
369 protected void normalize() {
370 minutes += seconds / SECONDS_PER_MINUTE;
371 seconds %= SECONDS_PER_MINUTE;
372 hours += minutes / MINUTES_PER_HOUR;
373 minutes %= MINUTES_PER_HOUR;
374 days += hours / HOURS_PER_DAY;
375 hours %= HOURS_PER_DAY;
376 }
377
378 /**
379 * Method checkWeeksOkay
380 *
381 *
382 * @param f
383 *
384 */
385 protected void checkWeeksOkay(int f) {
386 if ((f != 0)
387 && ((days != 0) || (hours != 0) || (minutes != 0)
388 || (seconds != 0))) {
389 throw new IllegalStateException(
390 "Weeks and non-weeks are incompatible");
391 }
392 }
393
394 /**
395 * Method checkNonWeeksOkay
396 *
397 *
398 * @param f
399 *
400 */
401 protected void checkNonWeeksOkay(int f) {
402 if ((f != 0) && (weeks != 0)) {
403 throw new IllegalStateException(
404 "Weeks and non-weeks are incompatible");
405 }
406 }
407
408 /**
409 * Method clone
410 *
411 *
412 * @return Object
413 *
414 */
415 public Object clone() {
416 try {
417 Duration other = (Duration)super.clone();
418
419 other.weeks = weeks;
420 other.days = days;
421 other.hours = hours;
422 other.minutes = minutes;
423 other.seconds = seconds;
424
425 return other;
426 }
427 catch (CloneNotSupportedException e) {
428 throw new InternalError();
429 }
430 }
431
432 /**
433 * Method toString
434 *
435 *
436 * @return String
437 *
438 */
439 public String toString() {
440 StringMaker sm = new StringMaker();
441
442 sm.append(getClass().getName());
443 sm.append("[weeks=");
444 sm.append(weeks);
445 sm.append(",days=");
446 sm.append(days);
447 sm.append(",hours=");
448 sm.append(hours);
449 sm.append(",minutes=");
450 sm.append(minutes);
451 sm.append(",seconds=");
452 sm.append(seconds);
453 sm.append("]");
454
455 return sm.toString();
456 }
457
458 }