1
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
43 public class Duration implements Cloneable, Serializable {
44
45
48 private int weeks;
49
50
53 private int days;
54
55
58 private int hours;
59
60
63 private int minutes;
64
65
68 private int seconds;
69
70
73 private final static int SECONDS_PER_MINUTE = 60;
74
75
78 private final static int MINUTES_PER_HOUR = 60;
79
80
83 private final static int HOURS_PER_DAY = 24;
84
85
88 private final static int DAYS_PER_WEEK = 7;
89
90
93 private final static int MILLIS_PER_SECOND = 1000;
94
95
98 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
99 * MILLIS_PER_SECOND;
100
101
104 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
105 * MILLIS_PER_MINUTE;
106
107
110 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
111
112
115 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
116
117
122 public Duration() {
123
124
125
126 }
127
128
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
154 public Duration(int h, int m, int s) {
155 this(0, h, m, s);
156 }
157
158
165 public Duration(int w) {
166 weeks = w;
167 }
168
169
174 public void clear() {
175 weeks = 0;
176 days = 0;
177 hours = 0;
178 minutes = 0;
179 seconds = 0;
180 }
181 ;
182
183
190 public int getWeeks() {
191 return weeks;
192 }
193
194
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
218 public int getDays() {
219 return days;
220 }
221
222
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
248 public int getHours() {
249 return hours;
250 }
251
252
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
278 public int getMinutes() {
279 return minutes;
280 }
281
282
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
308 public int getSeconds() {
309 return seconds;
310 }
311
312
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
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
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
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
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
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
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
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 }