1
14
15
44
45 package com.liferay.portal.kernel.cal;
46
47 import com.liferay.portal.kernel.util.StringBundler;
48
49 import java.io.Serializable;
50
51
56 public class Duration implements Cloneable, Serializable {
57
58
61 private int weeks;
62
63
66 private int days;
67
68
71 private int hours;
72
73
76 private int minutes;
77
78
81 private int seconds;
82
83
86 private final static int SECONDS_PER_MINUTE = 60;
87
88
91 private final static int MINUTES_PER_HOUR = 60;
92
93
96 private final static int HOURS_PER_DAY = 24;
97
98
101 private final static int DAYS_PER_WEEK = 7;
102
103
106 private final static int MILLIS_PER_SECOND = 1000;
107
108
111 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
112 * MILLIS_PER_SECOND;
113
114
117 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
118 * MILLIS_PER_MINUTE;
119
120
123 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
124
125
128 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
129
130
133 public Duration() {
134
135
136
137 }
138
139
142 public Duration(int d, int h, int m, int s) {
143 days = d;
144 hours = h;
145 minutes = m;
146 seconds = s;
147 }
148
149
152 public Duration(int h, int m, int s) {
153 this(0, h, m, s);
154 }
155
156
159 public Duration(int w) {
160 weeks = w;
161 }
162
163
166 public void clear() {
167 weeks = 0;
168 days = 0;
169 hours = 0;
170 minutes = 0;
171 seconds = 0;
172 }
173 ;
174
175
180 public int getWeeks() {
181 return weeks;
182 }
183
184
187 public void setWeeks(int w) {
188 if (w < 0) {
189 throw new IllegalArgumentException("Week value out of range");
190 }
191
192 checkWeeksOkay(w);
193
194 weeks = w;
195 }
196
197
202 public int getDays() {
203 return days;
204 }
205
206
209 public void setDays(int d) {
210 if (d < 0) {
211 throw new IllegalArgumentException("Day value out of range");
212 }
213
214 checkNonWeeksOkay(d);
215
216 days = d;
217
218 normalize();
219 }
220
221
226 public int getHours() {
227 return hours;
228 }
229
230
233 public void setHours(int h) {
234 if (h < 0) {
235 throw new IllegalArgumentException("Hour value out of range");
236 }
237
238 checkNonWeeksOkay(h);
239
240 hours = h;
241
242 normalize();
243 }
244
245
250 public int getMinutes() {
251 return minutes;
252 }
253
254
257 public void setMinutes(int m) {
258 if (m < 0) {
259 throw new IllegalArgumentException("Minute value out of range");
260 }
261
262 checkNonWeeksOkay(m);
263
264 minutes = m;
265
266 normalize();
267 }
268
269
274 public int getSeconds() {
275 return seconds;
276 }
277
278
281 public void setSeconds(int s) {
282 if (s < 0) {
283 throw new IllegalArgumentException("Second value out of range");
284 }
285
286 checkNonWeeksOkay(s);
287
288 seconds = s;
289
290 normalize();
291 }
292
293
298 public long getInterval() {
299 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
300 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
301 + weeks * MILLIS_PER_WEEK;
302 }
303
304
307 public void setInterval(long millis) {
308 if (millis < 0) {
309 throw new IllegalArgumentException("Negative-length interval");
310 }
311
312 clear();
313
314 days = (int)(millis / MILLIS_PER_DAY);
315 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
316
317 normalize();
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 protected void checkWeeksOkay(int f) {
336 if ((f != 0)
337 && ((days != 0) || (hours != 0) || (minutes != 0)
338 || (seconds != 0))) {
339 throw new IllegalStateException(
340 "Weeks and non-weeks are incompatible");
341 }
342 }
343
344
347 protected void checkNonWeeksOkay(int f) {
348 if ((f != 0) && (weeks != 0)) {
349 throw new IllegalStateException(
350 "Weeks and non-weeks are incompatible");
351 }
352 }
353
354
359 public Object clone() {
360 try {
361 Duration other = (Duration)super.clone();
362
363 other.weeks = weeks;
364 other.days = days;
365 other.hours = hours;
366 other.minutes = minutes;
367 other.seconds = seconds;
368
369 return other;
370 }
371 catch (CloneNotSupportedException e) {
372 throw new InternalError();
373 }
374 }
375
376
381 public String toString() {
382 StringBundler sb = new StringBundler(12);
383
384 sb.append(getClass().getName());
385 sb.append("[weeks=");
386 sb.append(weeks);
387 sb.append(",days=");
388 sb.append(days);
389 sb.append(",hours=");
390 sb.append(hours);
391 sb.append(",minutes=");
392 sb.append(minutes);
393 sb.append(",seconds=");
394 sb.append(seconds);
395 sb.append("]");
396
397 return sb.toString();
398 }
399
400 }