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 @Deprecated
057 public class Duration implements Cloneable, Serializable {
058
059
062 public Duration() {
063
064
065
066 }
067
068
071 public Duration(int w) {
072 _weeks = w;
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 d, int h, int m, int s) {
086 _days = d;
087 _hours = h;
088 _minutes = m;
089 _seconds = s;
090 }
091
092
095 public void clear() {
096 _weeks = 0;
097 _days = 0;
098 _hours = 0;
099 _minutes = 0;
100 _seconds = 0;
101 }
102
103
108 @Override
109 public Object clone() {
110 try {
111 Duration other = (Duration)super.clone();
112
113 other._weeks = _weeks;
114 other._days = _days;
115 other._hours = _hours;
116 other._minutes = _minutes;
117 other._seconds = _seconds;
118
119 return other;
120 }
121 catch (CloneNotSupportedException cnse) {
122 throw new InternalError();
123 }
124 }
125
126
131 public int getDays() {
132 return _days;
133 }
134
135
140 public int getHours() {
141 return _hours;
142 }
143
144
149 public long getInterval() {
150 return
151 _seconds * _MILLIS_PER_SECOND + _minutes * _MILLIS_PER_MINUTE +
152 _hours * _MILLIS_PER_HOUR + _days * _MILLIS_PER_DAY +
153 _weeks * _MILLIS_PER_WEEK;
154 }
155
156
161 public int getMinutes() {
162 return _minutes;
163 }
164
165
170 public int getSeconds() {
171 return _seconds;
172 }
173
174
179 public int getWeeks() {
180 return _weeks;
181 }
182
183
186 public void setDays(int d) {
187 if (d < 0) {
188 throw new IllegalArgumentException("Day value out of range");
189 }
190
191 checkNonWeeksOkay(d);
192
193 _days = d;
194
195 normalize();
196 }
197
198
201 public void setHours(int h) {
202 if (h < 0) {
203 throw new IllegalArgumentException("Hour value out of range");
204 }
205
206 checkNonWeeksOkay(h);
207
208 _hours = h;
209
210 normalize();
211 }
212
213
216 public void setInterval(long millis) {
217 if (millis < 0) {
218 throw new IllegalArgumentException("Negative-length interval");
219 }
220
221 clear();
222
223 _days = (int)(millis / _MILLIS_PER_DAY);
224 _seconds = (int)((millis % _MILLIS_PER_DAY) / _MILLIS_PER_SECOND);
225
226 normalize();
227 }
228
229
232 public void setMinutes(int m) {
233 if (m < 0) {
234 throw new IllegalArgumentException("Minute value out of range");
235 }
236
237 checkNonWeeksOkay(m);
238
239 _minutes = m;
240
241 normalize();
242 }
243
244
247 public void setSeconds(int s) {
248 if (s < 0) {
249 throw new IllegalArgumentException("Second value out of range");
250 }
251
252 checkNonWeeksOkay(s);
253
254 _seconds = s;
255
256 normalize();
257 }
258
259
262 public void setWeeks(int w) {
263 if (w < 0) {
264 throw new IllegalArgumentException("Week value out of range");
265 }
266
267 checkWeeksOkay(w);
268
269 _weeks = w;
270 }
271
272
277 @Override
278 public String toString() {
279 StringBundler sb = new StringBundler(12);
280
281 sb.append(getClass().getName());
282 sb.append("[weeks=");
283 sb.append(_weeks);
284 sb.append(",days=");
285 sb.append(_days);
286 sb.append(",hours=");
287 sb.append(_hours);
288 sb.append(",minutes=");
289 sb.append(_minutes);
290 sb.append(",seconds=");
291 sb.append(_seconds);
292 sb.append("]");
293
294 return sb.toString();
295 }
296
297
300 protected void checkNonWeeksOkay(int f) {
301 if ((f != 0) && (_weeks != 0)) {
302 throw new IllegalStateException(
303 "Weeks and non-weeks are incompatible");
304 }
305 }
306
307
310 protected void checkWeeksOkay(int f) {
311 if ((f != 0) &&
312 ((_days != 0) || (_hours != 0) || (_minutes != 0) ||
313 (_seconds != 0))) {
314
315 throw new IllegalStateException(
316 "Weeks and non-weeks are incompatible");
317 }
318 }
319
320
323 protected void normalize() {
324 _minutes += _seconds / _SECONDS_PER_MINUTE;
325 _seconds %= _SECONDS_PER_MINUTE;
326 _hours += _minutes / _MINUTES_PER_HOUR;
327 _minutes %= _MINUTES_PER_HOUR;
328 _days += _hours / _HOURS_PER_DAY;
329 _hours %= _HOURS_PER_DAY;
330 }
331
332
335 private static final int _DAYS_PER_WEEK = 7;
336
337
340 private static final int _HOURS_PER_DAY = 24;
341
342
345 private static final int _MILLIS_PER_DAY =
346 Duration._HOURS_PER_DAY * Duration._MILLIS_PER_HOUR;
347
348
351 private static final int _MILLIS_PER_HOUR =
352 Duration._MINUTES_PER_HOUR * Duration._MILLIS_PER_MINUTE;
353
354
357 private static final int _MILLIS_PER_MINUTE =
358 Duration._SECONDS_PER_MINUTE * Duration._MILLIS_PER_SECOND;
359
360
363 private static final int _MILLIS_PER_SECOND = 1000;
364
365
368 private static final int _MILLIS_PER_WEEK =
369 Duration._DAYS_PER_WEEK * Duration._MILLIS_PER_DAY;
370
371
374 private static final int _MINUTES_PER_HOUR = 60;
375
376
379 private static final int _SECONDS_PER_MINUTE = 60;
380
381
384 private int _days;
385
386
389 private int _hours;
390
391
394 private int _minutes;
395
396
399 private int _seconds;
400
401
404 private int _weeks;
405
406 }