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 public class Duration implements Cloneable, Serializable {
057
058
061 private int weeks;
062
063
066 private int days;
067
068
071 private int hours;
072
073
076 private int minutes;
077
078
081 private int seconds;
082
083
086 private final static int SECONDS_PER_MINUTE = 60;
087
088
091 private final static int MINUTES_PER_HOUR = 60;
092
093
096 private final static int HOURS_PER_DAY = 24;
097
098
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
179 public int getWeeks() {
180 return weeks;
181 }
182
183
186 public void setWeeks(int w) {
187 if (w < 0) {
188 throw new IllegalArgumentException("Week value out of range");
189 }
190
191 checkWeeksOkay(w);
192
193 weeks = w;
194 }
195
196
201 public int getDays() {
202 return days;
203 }
204
205
208 public void setDays(int d) {
209 if (d < 0) {
210 throw new IllegalArgumentException("Day value out of range");
211 }
212
213 checkNonWeeksOkay(d);
214
215 days = d;
216
217 normalize();
218 }
219
220
225 public int getHours() {
226 return hours;
227 }
228
229
232 public void setHours(int h) {
233 if (h < 0) {
234 throw new IllegalArgumentException("Hour value out of range");
235 }
236
237 checkNonWeeksOkay(h);
238
239 hours = h;
240
241 normalize();
242 }
243
244
249 public int getMinutes() {
250 return minutes;
251 }
252
253
256 public void setMinutes(int m) {
257 if (m < 0) {
258 throw new IllegalArgumentException("Minute value out of range");
259 }
260
261 checkNonWeeksOkay(m);
262
263 minutes = m;
264
265 normalize();
266 }
267
268
273 public int getSeconds() {
274 return seconds;
275 }
276
277
280 public void setSeconds(int s) {
281 if (s < 0) {
282 throw new IllegalArgumentException("Second value out of range");
283 }
284
285 checkNonWeeksOkay(s);
286
287 seconds = s;
288
289 normalize();
290 }
291
292
297 public long getInterval() {
298 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
299 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
300 + weeks * MILLIS_PER_WEEK;
301 }
302
303
306 public void setInterval(long millis) {
307 if (millis < 0) {
308 throw new IllegalArgumentException("Negative-length interval");
309 }
310
311 clear();
312
313 days = (int)(millis / MILLIS_PER_DAY);
314 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
315
316 normalize();
317 }
318
319
322 protected void normalize() {
323 minutes += seconds / SECONDS_PER_MINUTE;
324 seconds %= SECONDS_PER_MINUTE;
325 hours += minutes / MINUTES_PER_HOUR;
326 minutes %= MINUTES_PER_HOUR;
327 days += hours / HOURS_PER_DAY;
328 hours %= HOURS_PER_DAY;
329 }
330
331
334 protected void checkWeeksOkay(int f) {
335 if ((f != 0)
336 && ((days != 0) || (hours != 0) || (minutes != 0)
337 || (seconds != 0))) {
338 throw new IllegalStateException(
339 "Weeks and non-weeks are incompatible");
340 }
341 }
342
343
346 protected void checkNonWeeksOkay(int f) {
347 if ((f != 0) && (weeks != 0)) {
348 throw new IllegalStateException(
349 "Weeks and non-weeks are incompatible");
350 }
351 }
352
353
358 public Object clone() {
359 try {
360 Duration other = (Duration)super.clone();
361
362 other.weeks = weeks;
363 other.days = days;
364 other.hours = hours;
365 other.minutes = minutes;
366 other.seconds = seconds;
367
368 return other;
369 }
370 catch (CloneNotSupportedException e) {
371 throw new InternalError();
372 }
373 }
374
375
380 public String toString() {
381 StringBundler sb = new StringBundler(12);
382
383 sb.append(getClass().getName());
384 sb.append("[weeks=");
385 sb.append(weeks);
386 sb.append(",days=");
387 sb.append(days);
388 sb.append(",hours=");
389 sb.append(hours);
390 sb.append(",minutes=");
391 sb.append(minutes);
392 sb.append(",seconds=");
393 sb.append(seconds);
394 sb.append("]");
395
396 return sb.toString();
397 }
398
399 }