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 d, int h, int m, int s) {
070 _days = d;
071 _hours = h;
072 _minutes = m;
073 _seconds = s;
074 }
075
076
079 public Duration(int h, int m, int s) {
080 this(0, h, m, s);
081 }
082
083
086 public Duration(int w) {
087 _weeks = w;
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 e) {
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 _seconds * _MILLIS_PER_SECOND + _minutes * _MILLIS_PER_MINUTE
149 + _hours * _MILLIS_PER_HOUR + _days * _MILLIS_PER_DAY
150 + _weeks * _MILLIS_PER_WEEK;
151 }
152
153
158 public int getMinutes() {
159 return _minutes;
160 }
161
162
167 public int getSeconds() {
168 return _seconds;
169 }
170
171
176 public int getWeeks() {
177 return _weeks;
178 }
179
180
183 public void setDays(int d) {
184 if (d < 0) {
185 throw new IllegalArgumentException("Day value out of range");
186 }
187
188 checkNonWeeksOkay(d);
189
190 _days = d;
191
192 normalize();
193 }
194
195
198 public void setHours(int h) {
199 if (h < 0) {
200 throw new IllegalArgumentException("Hour value out of range");
201 }
202
203 checkNonWeeksOkay(h);
204
205 _hours = h;
206
207 normalize();
208 }
209
210
213 public void setInterval(long millis) {
214 if (millis < 0) {
215 throw new IllegalArgumentException("Negative-length interval");
216 }
217
218 clear();
219
220 _days = (int)(millis / _MILLIS_PER_DAY);
221 _seconds = (int)((millis % _MILLIS_PER_DAY) / _MILLIS_PER_SECOND);
222
223 normalize();
224 }
225
226
229 public void setMinutes(int m) {
230 if (m < 0) {
231 throw new IllegalArgumentException("Minute value out of range");
232 }
233
234 checkNonWeeksOkay(m);
235
236 _minutes = m;
237
238 normalize();
239 }
240
241
244 public void setSeconds(int s) {
245 if (s < 0) {
246 throw new IllegalArgumentException("Second value out of range");
247 }
248
249 checkNonWeeksOkay(s);
250
251 _seconds = s;
252
253 normalize();
254 }
255
256
259 public void setWeeks(int w) {
260 if (w < 0) {
261 throw new IllegalArgumentException("Week value out of range");
262 }
263
264 checkWeeksOkay(w);
265
266 _weeks = w;
267 }
268
269
274 @Override
275 public String toString() {
276 StringBundler sb = new StringBundler(12);
277
278 sb.append(getClass().getName());
279 sb.append("[weeks=");
280 sb.append(_weeks);
281 sb.append(",days=");
282 sb.append(_days);
283 sb.append(",hours=");
284 sb.append(_hours);
285 sb.append(",minutes=");
286 sb.append(_minutes);
287 sb.append(",seconds=");
288 sb.append(_seconds);
289 sb.append("]");
290
291 return sb.toString();
292 }
293
294
297 protected void checkNonWeeksOkay(int f) {
298 if ((f != 0) && (_weeks != 0)) {
299 throw new IllegalStateException(
300 "Weeks and non-weeks are incompatible");
301 }
302 }
303
304
307 protected void checkWeeksOkay(int f) {
308 if ((f != 0)
309 && ((_days != 0) || (_hours != 0) || (_minutes != 0)
310 || (_seconds != 0))) {
311 throw new IllegalStateException(
312 "Weeks and non-weeks are incompatible");
313 }
314 }
315
316
319 protected void normalize() {
320 _minutes += _seconds / _SECONDS_PER_MINUTE;
321 _seconds %= _SECONDS_PER_MINUTE;
322 _hours += _minutes / _MINUTES_PER_HOUR;
323 _minutes %= _MINUTES_PER_HOUR;
324 _days += _hours / _HOURS_PER_DAY;
325 _hours %= _HOURS_PER_DAY;
326 }
327
328
331 private static final int _DAYS_PER_WEEK = 7;
332
333
336 private static final int _HOURS_PER_DAY = 24;
337
338
341 private static final int _MILLIS_PER_DAY =
342 Duration._HOURS_PER_DAY * Duration._MILLIS_PER_HOUR;
343
344
347 private static final int _MILLIS_PER_HOUR =
348 Duration._MINUTES_PER_HOUR * Duration._MILLIS_PER_MINUTE;
349
350
353 private static final int _MILLIS_PER_MINUTE =
354 Duration._SECONDS_PER_MINUTE * Duration._MILLIS_PER_SECOND;
355
356
359 private static final int _MILLIS_PER_SECOND = 1000;
360
361
364 private static final int _MILLIS_PER_WEEK =
365 Duration._DAYS_PER_WEEK * Duration._MILLIS_PER_DAY;
366
367
370 private static final int _MINUTES_PER_HOUR = 60;
371
372
375 private static final int _SECONDS_PER_MINUTE = 60;
376
377
380 private int _days;
381
382
385 private int _hours;
386
387
390 private int _minutes;
391
392
395 private int _seconds;
396
397
400 private int _weeks;
401
402 }