1
30
31 package com.liferay.util.cal;
32
33 import com.liferay.portal.kernel.util.StringMaker;
34
35 import java.io.Serializable;
36
37
46 public class Duration implements Cloneable, Serializable {
47
48
51 private int weeks;
52
53
56 private int days;
57
58
61 private int hours;
62
63
66 private int minutes;
67
68
71 private int seconds;
72
73
76 private final static int SECONDS_PER_MINUTE = 60;
77
78
81 private final static int MINUTES_PER_HOUR = 60;
82
83
86 private final static int HOURS_PER_DAY = 24;
87
88
91 private final static int DAYS_PER_WEEK = 7;
92
93
96 private final static int MILLIS_PER_SECOND = 1000;
97
98
101 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
102 * MILLIS_PER_SECOND;
103
104
107 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
108 * MILLIS_PER_MINUTE;
109
110
113 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
114
115
118 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
119
120
125 public Duration() {
126
127
128
129 }
130
131
141 public Duration(int d, int h, int m, int s) {
142 days = d;
143 hours = h;
144 minutes = m;
145 seconds = s;
146 }
147
148
157 public Duration(int h, int m, int s) {
158 this(0, h, m, s);
159 }
160
161
168 public Duration(int w) {
169 weeks = w;
170 }
171
172
177 public void clear() {
178 weeks = 0;
179 days = 0;
180 hours = 0;
181 minutes = 0;
182 seconds = 0;
183 }
184 ;
185
186
193 public int getWeeks() {
194 return weeks;
195 }
196
197
204 public void setWeeks(int w) {
205 if (w < 0) {
206 throw new IllegalArgumentException("Week value out of range");
207 }
208
209 checkWeeksOkay(w);
210
211 weeks = w;
212 }
213
214
221 public int getDays() {
222 return days;
223 }
224
225
232 public void setDays(int d) {
233 if (d < 0) {
234 throw new IllegalArgumentException("Day value out of range");
235 }
236
237 checkNonWeeksOkay(d);
238
239 days = d;
240
241 normalize();
242 }
243
244
251 public int getHours() {
252 return hours;
253 }
254
255
262 public void setHours(int h) {
263 if (h < 0) {
264 throw new IllegalArgumentException("Hour value out of range");
265 }
266
267 checkNonWeeksOkay(h);
268
269 hours = h;
270
271 normalize();
272 }
273
274
281 public int getMinutes() {
282 return minutes;
283 }
284
285
292 public void setMinutes(int m) {
293 if (m < 0) {
294 throw new IllegalArgumentException("Minute value out of range");
295 }
296
297 checkNonWeeksOkay(m);
298
299 minutes = m;
300
301 normalize();
302 }
303
304
311 public int getSeconds() {
312 return seconds;
313 }
314
315
322 public void setSeconds(int s) {
323 if (s < 0) {
324 throw new IllegalArgumentException("Second value out of range");
325 }
326
327 checkNonWeeksOkay(s);
328
329 seconds = s;
330
331 normalize();
332 }
333
334
341 public long getInterval() {
342 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
343 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
344 + weeks * MILLIS_PER_WEEK;
345 }
346
347
354 public void setInterval(long millis) {
355 if (millis < 0) {
356 throw new IllegalArgumentException("Negative-length interval");
357 }
358
359 clear();
360
361 days = (int)(millis / MILLIS_PER_DAY);
362 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
363
364 normalize();
365 }
366
367
372 protected void normalize() {
373 minutes += seconds / SECONDS_PER_MINUTE;
374 seconds %= SECONDS_PER_MINUTE;
375 hours += minutes / MINUTES_PER_HOUR;
376 minutes %= MINUTES_PER_HOUR;
377 days += hours / HOURS_PER_DAY;
378 hours %= HOURS_PER_DAY;
379 }
380
381
388 protected void checkWeeksOkay(int f) {
389 if ((f != 0)
390 && ((days != 0) || (hours != 0) || (minutes != 0)
391 || (seconds != 0))) {
392 throw new IllegalStateException(
393 "Weeks and non-weeks are incompatible");
394 }
395 }
396
397
404 protected void checkNonWeeksOkay(int f) {
405 if ((f != 0) && (weeks != 0)) {
406 throw new IllegalStateException(
407 "Weeks and non-weeks are incompatible");
408 }
409 }
410
411
418 public Object clone() {
419 try {
420 Duration other = (Duration)super.clone();
421
422 other.weeks = weeks;
423 other.days = days;
424 other.hours = hours;
425 other.minutes = minutes;
426 other.seconds = seconds;
427
428 return other;
429 }
430 catch (CloneNotSupportedException e) {
431 throw new InternalError();
432 }
433 }
434
435
442 public String toString() {
443 StringMaker sm = new StringMaker();
444
445 sm.append(getClass().getName());
446 sm.append("[weeks=");
447 sm.append(weeks);
448 sm.append(",days=");
449 sm.append(days);
450 sm.append(",hours=");
451 sm.append(hours);
452 sm.append(",minutes=");
453 sm.append(minutes);
454 sm.append(",seconds=");
455 sm.append(seconds);
456 sm.append("]");
457
458 return sm.toString();
459 }
460
461 }