001
014
015
044
045 package com.liferay.util.cal;
046
047 import com.liferay.portal.kernel.util.StringBundler;
048
049 import java.io.Serializable;
050
051
056 public class Duration implements Cloneable, Serializable {
057
058
061 public Duration() {
062
063
064
065 }
066
067
070 public Duration(int w) {
071 _weeks = w;
072 }
073
074
077 public Duration(int h, int m, int s) {
078 this(0, h, m, s);
079 }
080
081
084 public Duration(int d, int h, int m, int s) {
085 _days = d;
086 _hours = h;
087 _minutes = m;
088 _seconds = s;
089 }
090
091
094 public void clear() {
095 _weeks = 0;
096 _days = 0;
097 _hours = 0;
098 _minutes = 0;
099 _seconds = 0;
100 }
101
102
107 @Override
108 public Object clone() {
109 try {
110 Duration other = (Duration)super.clone();
111
112 other._weeks = _weeks;
113 other._days = _days;
114 other._hours = _hours;
115 other._minutes = _minutes;
116 other._seconds = _seconds;
117
118 return other;
119 }
120 catch (CloneNotSupportedException cnse) {
121 throw new InternalError();
122 }
123 }
124
125
130 public int getDays() {
131 return _days;
132 }
133
134
139 public int getHours() {
140 return _hours;
141 }
142
143
148 public long getInterval() {
149 return
150 _seconds * _MILLIS_PER_SECOND + _minutes * _MILLIS_PER_MINUTE +
151 _hours * _MILLIS_PER_HOUR + _days * _MILLIS_PER_DAY +
152 _weeks * _MILLIS_PER_WEEK;
153 }
154
155
160 public int getMinutes() {
161 return _minutes;
162 }
163
164
169 public int getSeconds() {
170 return _seconds;
171 }
172
173
178 public int getWeeks() {
179 return _weeks;
180 }
181
182
185 public void setDays(int d) {
186 if (d < 0) {
187 throw new IllegalArgumentException("Day value out of range");
188 }
189
190 checkNonWeeksOkay(d);
191
192 _days = d;
193
194 normalize();
195 }
196
197
200 public void setHours(int h) {
201 if (h < 0) {
202 throw new IllegalArgumentException("Hour value out of range");
203 }
204
205 checkNonWeeksOkay(h);
206
207 _hours = h;
208
209 normalize();
210 }
211
212
215 public void setInterval(long millis) {
216 if (millis < 0) {
217 throw new IllegalArgumentException("Negative-length interval");
218 }
219
220 clear();
221
222 _days = (int)(millis / _MILLIS_PER_DAY);
223 _seconds = (int)((millis % _MILLIS_PER_DAY) / _MILLIS_PER_SECOND);
224
225 normalize();
226 }
227
228
231 public void setMinutes(int m) {
232 if (m < 0) {
233 throw new IllegalArgumentException("Minute value out of range");
234 }
235
236 checkNonWeeksOkay(m);
237
238 _minutes = m;
239
240 normalize();
241 }
242
243
246 public void setSeconds(int s) {
247 if (s < 0) {
248 throw new IllegalArgumentException("Second value out of range");
249 }
250
251 checkNonWeeksOkay(s);
252
253 _seconds = s;
254
255 normalize();
256 }
257
258
261 public void setWeeks(int w) {
262 if (w < 0) {
263 throw new IllegalArgumentException("Week value out of range");
264 }
265
266 checkWeeksOkay(w);
267
268 _weeks = w;
269 }
270
271
276 @Override
277 public String toString() {
278 StringBundler sb = new StringBundler(12);
279
280 sb.append(getClass().getName());
281 sb.append("[weeks=");
282 sb.append(_weeks);
283 sb.append(",days=");
284 sb.append(_days);
285 sb.append(",hours=");
286 sb.append(_hours);
287 sb.append(",minutes=");
288 sb.append(_minutes);
289 sb.append(",seconds=");
290 sb.append(_seconds);
291 sb.append("]");
292
293 return sb.toString();
294 }
295
296
299 protected void checkNonWeeksOkay(int f) {
300 if ((f != 0) && (_weeks != 0)) {
301 throw new IllegalStateException(
302 "Weeks and non-weeks are incompatible");
303 }
304 }
305
306
309 protected void checkWeeksOkay(int f) {
310 if ((f != 0) &&
311 ((_days != 0) || (_hours != 0) || (_minutes != 0) ||
312 (_seconds != 0))) {
313
314 throw new IllegalStateException(
315 "Weeks and non-weeks are incompatible");
316 }
317 }
318
319
322 protected void normalize() {
323 _minutes += _seconds / _SECONDS_PER_MINUTE;
324 _seconds %= _SECONDS_PER_MINUTE;
325 _hours += _minutes / _MINUTES_PER_HOUR;
326 _minutes %= _MINUTES_PER_HOUR;
327 _days += _hours / _HOURS_PER_DAY;
328 _hours %= _HOURS_PER_DAY;
329 }
330
331
334 private static final int _DAYS_PER_WEEK = 7;
335
336
339 private static final int _HOURS_PER_DAY = 24;
340
341
344 private static final int _MILLIS_PER_DAY =
345 Duration._HOURS_PER_DAY * Duration._MILLIS_PER_HOUR;
346
347
350 private static final int _MILLIS_PER_HOUR =
351 Duration._MINUTES_PER_HOUR * Duration._MILLIS_PER_MINUTE;
352
353
356 private static final int _MILLIS_PER_MINUTE =
357 Duration._SECONDS_PER_MINUTE * Duration._MILLIS_PER_SECOND;
358
359
362 private static final int _MILLIS_PER_SECOND = 1000;
363
364
367 private static final int _MILLIS_PER_WEEK =
368 Duration._DAYS_PER_WEEK * Duration._MILLIS_PER_DAY;
369
370
373 private static final int _MINUTES_PER_HOUR = 60;
374
375
378 private static final int _SECONDS_PER_MINUTE = 60;
379
380
383 private int _days;
384
385
388 private int _hours;
389
390
393 private int _minutes;
394
395
398 private int _seconds;
399
400
403 private int _weeks;
404
405 }