001
014
015
044
045 package com.liferay.portal.kernel.cal;
046
047 import com.liferay.portal.kernel.util.StringBundler;
048
049 import java.io.Serializable;
050
051
054 public class Duration implements Cloneable, Serializable {
055
056
059 public Duration() {
060
061
062
063 }
064
065
068 public Duration(int d, int h, int m, int s) {
069 _days = d;
070 _hours = h;
071 _minutes = m;
072 _seconds = s;
073 }
074
075
078 public Duration(int h, int m, int s) {
079 this(0, h, m, s);
080 }
081
082
085 public Duration(int w) {
086 _weeks = w;
087 }
088
089
092 public void clear() {
093 _weeks = 0;
094 _days = 0;
095 _hours = 0;
096 _minutes = 0;
097 _seconds = 0;
098 }
099
100
105 @Override
106 public Object clone() {
107 try {
108 Duration other = (Duration)super.clone();
109
110 other._weeks = _weeks;
111 other._days = _days;
112 other._hours = _hours;
113 other._minutes = _minutes;
114 other._seconds = _seconds;
115
116 return other;
117 }
118 catch (CloneNotSupportedException e) {
119 throw new InternalError();
120 }
121 }
122
123
128 public int getDays() {
129 return _days;
130 }
131
132
137 public int getHours() {
138 return _hours;
139 }
140
141
146 public long getInterval() {
147 return _seconds * _MILLIS_PER_SECOND + _minutes * _MILLIS_PER_MINUTE
148 + _hours * _MILLIS_PER_HOUR + _days * _MILLIS_PER_DAY
149 + _weeks * _MILLIS_PER_WEEK;
150 }
151
152
157 public int getMinutes() {
158 return _minutes;
159 }
160
161
166 public int getSeconds() {
167 return _seconds;
168 }
169
170
175 public int getWeeks() {
176 return _weeks;
177 }
178
179
182 public void setDays(int d) {
183 if (d < 0) {
184 throw new IllegalArgumentException("Day value out of range");
185 }
186
187 checkNonWeeksOkay(d);
188
189 _days = d;
190
191 normalize();
192 }
193
194
197 public void setHours(int h) {
198 if (h < 0) {
199 throw new IllegalArgumentException("Hour value out of range");
200 }
201
202 checkNonWeeksOkay(h);
203
204 _hours = h;
205
206 normalize();
207 }
208
209
212 public void setInterval(long millis) {
213 if (millis < 0) {
214 throw new IllegalArgumentException("Negative-length interval");
215 }
216
217 clear();
218
219 _days = (int)(millis / _MILLIS_PER_DAY);
220 _seconds = (int)((millis % _MILLIS_PER_DAY) / _MILLIS_PER_SECOND);
221
222 normalize();
223 }
224
225
228 public void setMinutes(int m) {
229 if (m < 0) {
230 throw new IllegalArgumentException("Minute value out of range");
231 }
232
233 checkNonWeeksOkay(m);
234
235 _minutes = m;
236
237 normalize();
238 }
239
240
243 public void setSeconds(int s) {
244 if (s < 0) {
245 throw new IllegalArgumentException("Second value out of range");
246 }
247
248 checkNonWeeksOkay(s);
249
250 _seconds = s;
251
252 normalize();
253 }
254
255
258 public void setWeeks(int w) {
259 if (w < 0) {
260 throw new IllegalArgumentException("Week value out of range");
261 }
262
263 checkWeeksOkay(w);
264
265 _weeks = w;
266 }
267
268
273 @Override
274 public String toString() {
275 StringBundler sb = new StringBundler(12);
276
277 sb.append(getClass().getName());
278 sb.append("[weeks=");
279 sb.append(_weeks);
280 sb.append(",days=");
281 sb.append(_days);
282 sb.append(",hours=");
283 sb.append(_hours);
284 sb.append(",minutes=");
285 sb.append(_minutes);
286 sb.append(",seconds=");
287 sb.append(_seconds);
288 sb.append("]");
289
290 return sb.toString();
291 }
292
293
296 protected void checkNonWeeksOkay(int f) {
297 if ((f != 0) && (_weeks != 0)) {
298 throw new IllegalStateException(
299 "Weeks and non-weeks are incompatible");
300 }
301 }
302
303
306 protected void checkWeeksOkay(int f) {
307 if ((f != 0)
308 && ((_days != 0) || (_hours != 0) || (_minutes != 0)
309 || (_seconds != 0))) {
310 throw new IllegalStateException(
311 "Weeks and non-weeks are incompatible");
312 }
313 }
314
315
318 protected void normalize() {
319 _minutes += _seconds / _SECONDS_PER_MINUTE;
320 _seconds %= _SECONDS_PER_MINUTE;
321 _hours += _minutes / _MINUTES_PER_HOUR;
322 _minutes %= _MINUTES_PER_HOUR;
323 _days += _hours / _HOURS_PER_DAY;
324 _hours %= _HOURS_PER_DAY;
325 }
326
327
330 private static final int _DAYS_PER_WEEK = 7;
331
332
335 private static final int _HOURS_PER_DAY = 24;
336
337
340 private static final long _MILLIS_PER_DAY =
341 Duration._HOURS_PER_DAY * Duration._MILLIS_PER_HOUR;
342
343
346 private static final long _MILLIS_PER_HOUR =
347 Duration._MINUTES_PER_HOUR * Duration._MILLIS_PER_MINUTE;
348
349
352 private static final long _MILLIS_PER_MINUTE =
353 Duration._SECONDS_PER_MINUTE * Duration._MILLIS_PER_SECOND;
354
355
358 private static final long _MILLIS_PER_SECOND = 1000;
359
360
363 private static final long _MILLIS_PER_WEEK =
364 Duration._DAYS_PER_WEEK * Duration._MILLIS_PER_DAY;
365
366
369 private static final int _MINUTES_PER_HOUR = 60;
370
371
374 private static final int _SECONDS_PER_MINUTE = 60;
375
376
379 private int _days;
380
381
384 private int _hours;
385
386
389 private int _minutes;
390
391
394 private int _seconds;
395
396
399 private int _weeks;
400
401 }