1
22
23
52
53 package com.liferay.util.cal;
54
55 import java.io.Serializable;
56
57
64 public class Duration implements Cloneable, Serializable {
65
66
69 private int weeks;
70
71
74 private int days;
75
76
79 private int hours;
80
81
84 private int minutes;
85
86
89 private int seconds;
90
91
94 private final static int SECONDS_PER_MINUTE = 60;
95
96
99 private final static int MINUTES_PER_HOUR = 60;
100
101
104 private final static int HOURS_PER_DAY = 24;
105
106
109 private final static int DAYS_PER_WEEK = 7;
110
111
114 private final static int MILLIS_PER_SECOND = 1000;
115
116
119 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
120 * MILLIS_PER_SECOND;
121
122
125 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
126 * MILLIS_PER_MINUTE;
127
128
131 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
132
133
136 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
137
138
141 public Duration() {
142
143
144
145 }
146
147
150 public Duration(int d, int h, int m, int s) {
151 days = d;
152 hours = h;
153 minutes = m;
154 seconds = s;
155 }
156
157
160 public Duration(int h, int m, int s) {
161 this(0, h, m, s);
162 }
163
164
167 public Duration(int w) {
168 weeks = w;
169 }
170
171
174 public void clear() {
175 weeks = 0;
176 days = 0;
177 hours = 0;
178 minutes = 0;
179 seconds = 0;
180 }
181 ;
182
183
188 public int getWeeks() {
189 return weeks;
190 }
191
192
195 public void setWeeks(int w) {
196 if (w < 0) {
197 throw new IllegalArgumentException("Week value out of range");
198 }
199
200 checkWeeksOkay(w);
201
202 weeks = w;
203 }
204
205
210 public int getDays() {
211 return days;
212 }
213
214
217 public void setDays(int d) {
218 if (d < 0) {
219 throw new IllegalArgumentException("Day value out of range");
220 }
221
222 checkNonWeeksOkay(d);
223
224 days = d;
225
226 normalize();
227 }
228
229
234 public int getHours() {
235 return hours;
236 }
237
238
241 public void setHours(int h) {
242 if (h < 0) {
243 throw new IllegalArgumentException("Hour value out of range");
244 }
245
246 checkNonWeeksOkay(h);
247
248 hours = h;
249
250 normalize();
251 }
252
253
258 public int getMinutes() {
259 return minutes;
260 }
261
262
265 public void setMinutes(int m) {
266 if (m < 0) {
267 throw new IllegalArgumentException("Minute value out of range");
268 }
269
270 checkNonWeeksOkay(m);
271
272 minutes = m;
273
274 normalize();
275 }
276
277
282 public int getSeconds() {
283 return seconds;
284 }
285
286
289 public void setSeconds(int s) {
290 if (s < 0) {
291 throw new IllegalArgumentException("Second value out of range");
292 }
293
294 checkNonWeeksOkay(s);
295
296 seconds = s;
297
298 normalize();
299 }
300
301
306 public long getInterval() {
307 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
308 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
309 + weeks * MILLIS_PER_WEEK;
310 }
311
312
315 public void setInterval(long millis) {
316 if (millis < 0) {
317 throw new IllegalArgumentException("Negative-length interval");
318 }
319
320 clear();
321
322 days = (int)(millis / MILLIS_PER_DAY);
323 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
324
325 normalize();
326 }
327
328
331 protected void normalize() {
332 minutes += seconds / SECONDS_PER_MINUTE;
333 seconds %= SECONDS_PER_MINUTE;
334 hours += minutes / MINUTES_PER_HOUR;
335 minutes %= MINUTES_PER_HOUR;
336 days += hours / HOURS_PER_DAY;
337 hours %= HOURS_PER_DAY;
338 }
339
340
343 protected void checkWeeksOkay(int f) {
344 if ((f != 0)
345 && ((days != 0) || (hours != 0) || (minutes != 0)
346 || (seconds != 0))) {
347 throw new IllegalStateException(
348 "Weeks and non-weeks are incompatible");
349 }
350 }
351
352
355 protected void checkNonWeeksOkay(int f) {
356 if ((f != 0) && (weeks != 0)) {
357 throw new IllegalStateException(
358 "Weeks and non-weeks are incompatible");
359 }
360 }
361
362
367 public Object clone() {
368 try {
369 Duration other = (Duration)super.clone();
370
371 other.weeks = weeks;
372 other.days = days;
373 other.hours = hours;
374 other.minutes = minutes;
375 other.seconds = seconds;
376
377 return other;
378 }
379 catch (CloneNotSupportedException e) {
380 throw new InternalError();
381 }
382 }
383
384
389 public String toString() {
390 StringBuilder sb = new StringBuilder();
391
392 sb.append(getClass().getName());
393 sb.append("[weeks=");
394 sb.append(weeks);
395 sb.append(",days=");
396 sb.append(days);
397 sb.append(",hours=");
398 sb.append(hours);
399 sb.append(",minutes=");
400 sb.append(minutes);
401 sb.append(",seconds=");
402 sb.append(seconds);
403 sb.append("]");
404
405 return sb.toString();
406 }
407
408 }