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 private int weeks;
060
061
064 private int days;
065
066
069 private int hours;
070
071
074 private int minutes;
075
076
079 private int seconds;
080
081
084 private final static int SECONDS_PER_MINUTE = 60;
085
086
089 private final static int MINUTES_PER_HOUR = 60;
090
091
094 private final static int HOURS_PER_DAY = 24;
095
096
099 private final static int DAYS_PER_WEEK = 7;
100
101
104 private final static int MILLIS_PER_SECOND = 1000;
105
106
109 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
110 * MILLIS_PER_SECOND;
111
112
115 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
116 * MILLIS_PER_MINUTE;
117
118
121 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
122
123
126 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
127
128
131 public Duration() {
132
133
134
135 }
136
137
140 public Duration(int d, int h, int m, int s) {
141 days = d;
142 hours = h;
143 minutes = m;
144 seconds = s;
145 }
146
147
150 public Duration(int h, int m, int s) {
151 this(0, h, m, s);
152 }
153
154
157 public Duration(int w) {
158 weeks = w;
159 }
160
161
164 public void clear() {
165 weeks = 0;
166 days = 0;
167 hours = 0;
168 minutes = 0;
169 seconds = 0;
170 }
171
172
177 public int getWeeks() {
178 return weeks;
179 }
180
181
184 public void setWeeks(int w) {
185 if (w < 0) {
186 throw new IllegalArgumentException("Week value out of range");
187 }
188
189 checkWeeksOkay(w);
190
191 weeks = w;
192 }
193
194
199 public int getDays() {
200 return days;
201 }
202
203
206 public void setDays(int d) {
207 if (d < 0) {
208 throw new IllegalArgumentException("Day value out of range");
209 }
210
211 checkNonWeeksOkay(d);
212
213 days = d;
214
215 normalize();
216 }
217
218
223 public int getHours() {
224 return hours;
225 }
226
227
230 public void setHours(int h) {
231 if (h < 0) {
232 throw new IllegalArgumentException("Hour value out of range");
233 }
234
235 checkNonWeeksOkay(h);
236
237 hours = h;
238
239 normalize();
240 }
241
242
247 public int getMinutes() {
248 return minutes;
249 }
250
251
254 public void setMinutes(int m) {
255 if (m < 0) {
256 throw new IllegalArgumentException("Minute value out of range");
257 }
258
259 checkNonWeeksOkay(m);
260
261 minutes = m;
262
263 normalize();
264 }
265
266
271 public int getSeconds() {
272 return seconds;
273 }
274
275
278 public void setSeconds(int s) {
279 if (s < 0) {
280 throw new IllegalArgumentException("Second value out of range");
281 }
282
283 checkNonWeeksOkay(s);
284
285 seconds = s;
286
287 normalize();
288 }
289
290
295 public long getInterval() {
296 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
297 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
298 + weeks * MILLIS_PER_WEEK;
299 }
300
301
304 public void setInterval(long millis) {
305 if (millis < 0) {
306 throw new IllegalArgumentException("Negative-length interval");
307 }
308
309 clear();
310
311 days = (int)(millis / MILLIS_PER_DAY);
312 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
313
314 normalize();
315 }
316
317
320 protected void normalize() {
321 minutes += seconds / SECONDS_PER_MINUTE;
322 seconds %= SECONDS_PER_MINUTE;
323 hours += minutes / MINUTES_PER_HOUR;
324 minutes %= MINUTES_PER_HOUR;
325 days += hours / HOURS_PER_DAY;
326 hours %= HOURS_PER_DAY;
327 }
328
329
332 protected void checkWeeksOkay(int f) {
333 if ((f != 0)
334 && ((days != 0) || (hours != 0) || (minutes != 0)
335 || (seconds != 0))) {
336 throw new IllegalStateException(
337 "Weeks and non-weeks are incompatible");
338 }
339 }
340
341
344 protected void checkNonWeeksOkay(int f) {
345 if ((f != 0) && (weeks != 0)) {
346 throw new IllegalStateException(
347 "Weeks and non-weeks are incompatible");
348 }
349 }
350
351
356 public Object clone() {
357 try {
358 Duration other = (Duration)super.clone();
359
360 other.weeks = weeks;
361 other.days = days;
362 other.hours = hours;
363 other.minutes = minutes;
364 other.seconds = seconds;
365
366 return other;
367 }
368 catch (CloneNotSupportedException e) {
369 throw new InternalError();
370 }
371 }
372
373
378 public String toString() {
379 StringBundler sb = new StringBundler(12);
380
381 sb.append(getClass().getName());
382 sb.append("[weeks=");
383 sb.append(weeks);
384 sb.append(",days=");
385 sb.append(days);
386 sb.append(",hours=");
387 sb.append(hours);
388 sb.append(",minutes=");
389 sb.append(minutes);
390 sb.append(",seconds=");
391 sb.append(seconds);
392 sb.append("]");
393
394 return sb.toString();
395 }
396
397 }