1
22
23
52
53 package com.liferay.portal.kernel.cal;
54
55 import java.io.Serializable;
56
57
62 public class Duration implements Cloneable, Serializable {
63
64
67 private int weeks;
68
69
72 private int days;
73
74
77 private int hours;
78
79
82 private int minutes;
83
84
87 private int seconds;
88
89
92 private final static int SECONDS_PER_MINUTE = 60;
93
94
97 private final static int MINUTES_PER_HOUR = 60;
98
99
102 private final static int HOURS_PER_DAY = 24;
103
104
107 private final static int DAYS_PER_WEEK = 7;
108
109
112 private final static int MILLIS_PER_SECOND = 1000;
113
114
117 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
118 * MILLIS_PER_SECOND;
119
120
123 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
124 * MILLIS_PER_MINUTE;
125
126
129 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
130
131
134 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
135
136
139 public Duration() {
140
141
142
143 }
144
145
148 public Duration(int d, int h, int m, int s) {
149 days = d;
150 hours = h;
151 minutes = m;
152 seconds = s;
153 }
154
155
158 public Duration(int h, int m, int s) {
159 this(0, h, m, s);
160 }
161
162
165 public Duration(int w) {
166 weeks = w;
167 }
168
169
172 public void clear() {
173 weeks = 0;
174 days = 0;
175 hours = 0;
176 minutes = 0;
177 seconds = 0;
178 }
179 ;
180
181
186 public int getWeeks() {
187 return weeks;
188 }
189
190
193 public void setWeeks(int w) {
194 if (w < 0) {
195 throw new IllegalArgumentException("Week value out of range");
196 }
197
198 checkWeeksOkay(w);
199
200 weeks = w;
201 }
202
203
208 public int getDays() {
209 return days;
210 }
211
212
215 public void setDays(int d) {
216 if (d < 0) {
217 throw new IllegalArgumentException("Day value out of range");
218 }
219
220 checkNonWeeksOkay(d);
221
222 days = d;
223
224 normalize();
225 }
226
227
232 public int getHours() {
233 return hours;
234 }
235
236
239 public void setHours(int h) {
240 if (h < 0) {
241 throw new IllegalArgumentException("Hour value out of range");
242 }
243
244 checkNonWeeksOkay(h);
245
246 hours = h;
247
248 normalize();
249 }
250
251
256 public int getMinutes() {
257 return minutes;
258 }
259
260
263 public void setMinutes(int m) {
264 if (m < 0) {
265 throw new IllegalArgumentException("Minute value out of range");
266 }
267
268 checkNonWeeksOkay(m);
269
270 minutes = m;
271
272 normalize();
273 }
274
275
280 public int getSeconds() {
281 return seconds;
282 }
283
284
287 public void setSeconds(int s) {
288 if (s < 0) {
289 throw new IllegalArgumentException("Second value out of range");
290 }
291
292 checkNonWeeksOkay(s);
293
294 seconds = s;
295
296 normalize();
297 }
298
299
304 public long getInterval() {
305 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
306 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
307 + weeks * MILLIS_PER_WEEK;
308 }
309
310
313 public void setInterval(long millis) {
314 if (millis < 0) {
315 throw new IllegalArgumentException("Negative-length interval");
316 }
317
318 clear();
319
320 days = (int)(millis / MILLIS_PER_DAY);
321 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
322
323 normalize();
324 }
325
326
329 protected void normalize() {
330 minutes += seconds / SECONDS_PER_MINUTE;
331 seconds %= SECONDS_PER_MINUTE;
332 hours += minutes / MINUTES_PER_HOUR;
333 minutes %= MINUTES_PER_HOUR;
334 days += hours / HOURS_PER_DAY;
335 hours %= HOURS_PER_DAY;
336 }
337
338
341 protected void checkWeeksOkay(int f) {
342 if ((f != 0)
343 && ((days != 0) || (hours != 0) || (minutes != 0)
344 || (seconds != 0))) {
345 throw new IllegalStateException(
346 "Weeks and non-weeks are incompatible");
347 }
348 }
349
350
353 protected void checkNonWeeksOkay(int f) {
354 if ((f != 0) && (weeks != 0)) {
355 throw new IllegalStateException(
356 "Weeks and non-weeks are incompatible");
357 }
358 }
359
360
365 public Object clone() {
366 try {
367 Duration other = (Duration)super.clone();
368
369 other.weeks = weeks;
370 other.days = days;
371 other.hours = hours;
372 other.minutes = minutes;
373 other.seconds = seconds;
374
375 return other;
376 }
377 catch (CloneNotSupportedException e) {
378 throw new InternalError();
379 }
380 }
381
382
387 public String toString() {
388 StringBuilder sb = new StringBuilder();
389
390 sb.append(getClass().getName());
391 sb.append("[weeks=");
392 sb.append(weeks);
393 sb.append(",days=");
394 sb.append(days);
395 sb.append(",hours=");
396 sb.append(hours);
397 sb.append(",minutes=");
398 sb.append(minutes);
399 sb.append(",seconds=");
400 sb.append(seconds);
401 sb.append("]");
402
403 return sb.toString();
404 }
405
406 }