1
14
15
44
45 package com.liferay.util.cal;
46
47 import com.liferay.portal.kernel.util.StringBundler;
48
49 import java.io.Serializable;
50
51
58 public class Duration implements Cloneable, Serializable {
59
60
63 private int weeks;
64
65
68 private int days;
69
70
73 private int hours;
74
75
78 private int minutes;
79
80
83 private int seconds;
84
85
88 private final static int SECONDS_PER_MINUTE = 60;
89
90
93 private final static int MINUTES_PER_HOUR = 60;
94
95
98 private final static int HOURS_PER_DAY = 24;
99
100
103 private final static int DAYS_PER_WEEK = 7;
104
105
108 private final static int MILLIS_PER_SECOND = 1000;
109
110
113 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
114 * MILLIS_PER_SECOND;
115
116
119 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
120 * MILLIS_PER_MINUTE;
121
122
125 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
126
127
130 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
131
132
135 public Duration() {
136
137
138
139 }
140
141
144 public Duration(int d, int h, int m, int s) {
145 days = d;
146 hours = h;
147 minutes = m;
148 seconds = s;
149 }
150
151
154 public Duration(int h, int m, int s) {
155 this(0, h, m, s);
156 }
157
158
161 public Duration(int w) {
162 weeks = w;
163 }
164
165
168 public void clear() {
169 weeks = 0;
170 days = 0;
171 hours = 0;
172 minutes = 0;
173 seconds = 0;
174 }
175 ;
176
177
182 public int getWeeks() {
183 return weeks;
184 }
185
186
189 public void setWeeks(int w) {
190 if (w < 0) {
191 throw new IllegalArgumentException("Week value out of range");
192 }
193
194 checkWeeksOkay(w);
195
196 weeks = w;
197 }
198
199
204 public int getDays() {
205 return days;
206 }
207
208
211 public void setDays(int d) {
212 if (d < 0) {
213 throw new IllegalArgumentException("Day value out of range");
214 }
215
216 checkNonWeeksOkay(d);
217
218 days = d;
219
220 normalize();
221 }
222
223
228 public int getHours() {
229 return hours;
230 }
231
232
235 public void setHours(int h) {
236 if (h < 0) {
237 throw new IllegalArgumentException("Hour value out of range");
238 }
239
240 checkNonWeeksOkay(h);
241
242 hours = h;
243
244 normalize();
245 }
246
247
252 public int getMinutes() {
253 return minutes;
254 }
255
256
259 public void setMinutes(int m) {
260 if (m < 0) {
261 throw new IllegalArgumentException("Minute value out of range");
262 }
263
264 checkNonWeeksOkay(m);
265
266 minutes = m;
267
268 normalize();
269 }
270
271
276 public int getSeconds() {
277 return seconds;
278 }
279
280
283 public void setSeconds(int s) {
284 if (s < 0) {
285 throw new IllegalArgumentException("Second value out of range");
286 }
287
288 checkNonWeeksOkay(s);
289
290 seconds = s;
291
292 normalize();
293 }
294
295
300 public long getInterval() {
301 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
302 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
303 + weeks * MILLIS_PER_WEEK;
304 }
305
306
309 public void setInterval(long millis) {
310 if (millis < 0) {
311 throw new IllegalArgumentException("Negative-length interval");
312 }
313
314 clear();
315
316 days = (int)(millis / MILLIS_PER_DAY);
317 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
318
319 normalize();
320 }
321
322
325 protected void normalize() {
326 minutes += seconds / SECONDS_PER_MINUTE;
327 seconds %= SECONDS_PER_MINUTE;
328 hours += minutes / MINUTES_PER_HOUR;
329 minutes %= MINUTES_PER_HOUR;
330 days += hours / HOURS_PER_DAY;
331 hours %= HOURS_PER_DAY;
332 }
333
334
337 protected void checkWeeksOkay(int f) {
338 if ((f != 0)
339 && ((days != 0) || (hours != 0) || (minutes != 0)
340 || (seconds != 0))) {
341 throw new IllegalStateException(
342 "Weeks and non-weeks are incompatible");
343 }
344 }
345
346
349 protected void checkNonWeeksOkay(int f) {
350 if ((f != 0) && (weeks != 0)) {
351 throw new IllegalStateException(
352 "Weeks and non-weeks are incompatible");
353 }
354 }
355
356
361 public Object clone() {
362 try {
363 Duration other = (Duration)super.clone();
364
365 other.weeks = weeks;
366 other.days = days;
367 other.hours = hours;
368 other.minutes = minutes;
369 other.seconds = seconds;
370
371 return other;
372 }
373 catch (CloneNotSupportedException e) {
374 throw new InternalError();
375 }
376 }
377
378
383 public String toString() {
384 StringBundler sb = new StringBundler(12);
385
386 sb.append(getClass().getName());
387 sb.append("[weeks=");
388 sb.append(weeks);
389 sb.append(",days=");
390 sb.append(days);
391 sb.append(",hours=");
392 sb.append(hours);
393 sb.append(",minutes=");
394 sb.append(minutes);
395 sb.append(",seconds=");
396 sb.append(seconds);
397 sb.append("]");
398
399 return sb.toString();
400 }
401
402 }