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
055 public class Duration implements Cloneable, Serializable {
056
057
060 public Duration() {
061
062
063
064 }
065
066
069 public Duration(int w) {
070 _weeks = w;
071 }
072
073
076 public Duration(int h, int m, int s) {
077 this(0, h, m, s);
078 }
079
080
083 public Duration(int d, int h, int m, int s) {
084 _days = d;
085 _hours = h;
086 _minutes = m;
087 _seconds = s;
088 }
089
090
093 public void clear() {
094 _weeks = 0;
095 _days = 0;
096 _hours = 0;
097 _minutes = 0;
098 _seconds = 0;
099 }
100
101
106 @Override
107 public Object clone() {
108 try {
109 Duration other = (Duration)super.clone();
110
111 other._weeks = _weeks;
112 other._days = _days;
113 other._hours = _hours;
114 other._minutes = _minutes;
115 other._seconds = _seconds;
116
117 return other;
118 }
119 catch (CloneNotSupportedException cnse) {
120 throw new InternalError();
121 }
122 }
123
124
129 public int getDays() {
130 return _days;
131 }
132
133
138 public int getHours() {
139 return _hours;
140 }
141
142
147 public long getInterval() {
148 return
149 _seconds * _MILLIS_PER_SECOND + _minutes * _MILLIS_PER_MINUTE +
150 _hours * _MILLIS_PER_HOUR + _days * _MILLIS_PER_DAY +
151 _weeks * _MILLIS_PER_WEEK;
152 }
153
154
159 public int getMinutes() {
160 return _minutes;
161 }
162
163
168 public int getSeconds() {
169 return _seconds;
170 }
171
172
177 public int getWeeks() {
178 return _weeks;
179 }
180
181
184 public void setDays(int d) {
185 if (d < 0) {
186 throw new IllegalArgumentException("Day value out of range");
187 }
188
189 checkNonWeeksOkay(d);
190
191 _days = d;
192
193 normalize();
194 }
195
196
199 public void setHours(int h) {
200 if (h < 0) {
201 throw new IllegalArgumentException("Hour value out of range");
202 }
203
204 checkNonWeeksOkay(h);
205
206 _hours = h;
207
208 normalize();
209 }
210
211
214 public void setInterval(long millis) {
215 if (millis < 0) {
216 throw new IllegalArgumentException("Negative-length interval");
217 }
218
219 clear();
220
221 _days = (int)(millis / _MILLIS_PER_DAY);
222 _seconds = (int)((millis % _MILLIS_PER_DAY) / _MILLIS_PER_SECOND);
223
224 normalize();
225 }
226
227
230 public void setMinutes(int m) {
231 if (m < 0) {
232 throw new IllegalArgumentException("Minute value out of range");
233 }
234
235 checkNonWeeksOkay(m);
236
237 _minutes = m;
238
239 normalize();
240 }
241
242
245 public void setSeconds(int s) {
246 if (s < 0) {
247 throw new IllegalArgumentException("Second value out of range");
248 }
249
250 checkNonWeeksOkay(s);
251
252 _seconds = s;
253
254 normalize();
255 }
256
257
260 public void setWeeks(int w) {
261 if (w < 0) {
262 throw new IllegalArgumentException("Week value out of range");
263 }
264
265 checkWeeksOkay(w);
266
267 _weeks = w;
268 }
269
270
275 @Override
276 public String toString() {
277 StringBundler sb = new StringBundler(12);
278
279 sb.append(getClass().getName());
280 sb.append("[weeks=");
281 sb.append(_weeks);
282 sb.append(",days=");
283 sb.append(_days);
284 sb.append(",hours=");
285 sb.append(_hours);
286 sb.append(",minutes=");
287 sb.append(_minutes);
288 sb.append(",seconds=");
289 sb.append(_seconds);
290 sb.append("]");
291
292 return sb.toString();
293 }
294
295
298 protected void checkNonWeeksOkay(int f) {
299 if ((f != 0) && (_weeks != 0)) {
300 throw new IllegalStateException(
301 "Weeks and non-weeks are incompatible");
302 }
303 }
304
305
308 protected void checkWeeksOkay(int f) {
309 if ((f != 0) &&
310 ((_days != 0) || (_hours != 0) || (_minutes != 0) ||
311 (_seconds != 0))) {
312
313 throw new IllegalStateException(
314 "Weeks and non-weeks are incompatible");
315 }
316 }
317
318
321 protected void normalize() {
322 _minutes += _seconds / _SECONDS_PER_MINUTE;
323 _seconds %= _SECONDS_PER_MINUTE;
324 _hours += _minutes / _MINUTES_PER_HOUR;
325 _minutes %= _MINUTES_PER_HOUR;
326 _days += _hours / _HOURS_PER_DAY;
327 _hours %= _HOURS_PER_DAY;
328 }
329
330
333 private static final int _DAYS_PER_WEEK = 7;
334
335
338 private static final int _HOURS_PER_DAY = 24;
339
340
343 private static final int _MILLIS_PER_DAY =
344 Duration._HOURS_PER_DAY * Duration._MILLIS_PER_HOUR;
345
346
349 private static final int _MILLIS_PER_HOUR =
350 Duration._MINUTES_PER_HOUR * Duration._MILLIS_PER_MINUTE;
351
352
355 private static final int _MILLIS_PER_MINUTE =
356 Duration._SECONDS_PER_MINUTE * Duration._MILLIS_PER_SECOND;
357
358
361 private static final int _MILLIS_PER_SECOND = 1000;
362
363
366 private static final int _MILLIS_PER_WEEK =
367 Duration._DAYS_PER_WEEK * Duration._MILLIS_PER_DAY;
368
369
372 private static final int _MINUTES_PER_HOUR = 60;
373
374
377 private static final int _SECONDS_PER_MINUTE = 60;
378
379
382 private int _days;
383
384
387 private int _hours;
388
389
392 private int _minutes;
393
394
397 private int _seconds;
398
399
402 private int _weeks;
403
404 }