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
055 public class Duration implements Cloneable, Serializable {
056
057
060 private int weeks;
061
062
065 private int days;
066
067
070 private int hours;
071
072
075 private int minutes;
076
077
080 private int seconds;
081
082
085 private static final int SECONDS_PER_MINUTE = 60;
086
087
090 private static final int MINUTES_PER_HOUR = 60;
091
092
095 private static final int HOURS_PER_DAY = 24;
096
097
100 private static final int DAYS_PER_WEEK = 7;
101
102
105 private static final int MILLIS_PER_SECOND = 1000;
106
107
110 private static final int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
111 * MILLIS_PER_SECOND;
112
113
116 private static final int MILLIS_PER_HOUR = MINUTES_PER_HOUR
117 * MILLIS_PER_MINUTE;
118
119
122 private static final int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
123
124
127 private static final int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
128
129
132 public Duration() {
133
134
135
136 }
137
138
141 public Duration(int d, int h, int m, int s) {
142 days = d;
143 hours = h;
144 minutes = m;
145 seconds = s;
146 }
147
148
151 public Duration(int h, int m, int s) {
152 this(0, h, m, s);
153 }
154
155
158 public Duration(int w) {
159 weeks = w;
160 }
161
162
165 public void clear() {
166 weeks = 0;
167 days = 0;
168 hours = 0;
169 minutes = 0;
170 seconds = 0;
171 }
172
173
178 public int getWeeks() {
179 return weeks;
180 }
181
182
185 public void setWeeks(int w) {
186 if (w < 0) {
187 throw new IllegalArgumentException("Week value out of range");
188 }
189
190 checkWeeksOkay(w);
191
192 weeks = w;
193 }
194
195
200 public int getDays() {
201 return days;
202 }
203
204
207 public void setDays(int d) {
208 if (d < 0) {
209 throw new IllegalArgumentException("Day value out of range");
210 }
211
212 checkNonWeeksOkay(d);
213
214 days = d;
215
216 normalize();
217 }
218
219
224 public int getHours() {
225 return hours;
226 }
227
228
231 public void setHours(int h) {
232 if (h < 0) {
233 throw new IllegalArgumentException("Hour value out of range");
234 }
235
236 checkNonWeeksOkay(h);
237
238 hours = h;
239
240 normalize();
241 }
242
243
248 public int getMinutes() {
249 return minutes;
250 }
251
252
255 public void setMinutes(int m) {
256 if (m < 0) {
257 throw new IllegalArgumentException("Minute value out of range");
258 }
259
260 checkNonWeeksOkay(m);
261
262 minutes = m;
263
264 normalize();
265 }
266
267
272 public int getSeconds() {
273 return seconds;
274 }
275
276
279 public void setSeconds(int s) {
280 if (s < 0) {
281 throw new IllegalArgumentException("Second value out of range");
282 }
283
284 checkNonWeeksOkay(s);
285
286 seconds = s;
287
288 normalize();
289 }
290
291
296 public long getInterval() {
297 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
298 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
299 + weeks * MILLIS_PER_WEEK;
300 }
301
302
305 public void setInterval(long millis) {
306 if (millis < 0) {
307 throw new IllegalArgumentException("Negative-length interval");
308 }
309
310 clear();
311
312 days = (int)(millis / MILLIS_PER_DAY);
313 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
314
315 normalize();
316 }
317
318
321 protected void normalize() {
322 minutes += seconds / SECONDS_PER_MINUTE;
323 seconds %= SECONDS_PER_MINUTE;
324 hours += minutes / MINUTES_PER_HOUR;
325 minutes %= MINUTES_PER_HOUR;
326 days += hours / HOURS_PER_DAY;
327 hours %= HOURS_PER_DAY;
328 }
329
330
333 protected void checkWeeksOkay(int f) {
334 if ((f != 0)
335 && ((days != 0) || (hours != 0) || (minutes != 0)
336 || (seconds != 0))) {
337 throw new IllegalStateException(
338 "Weeks and non-weeks are incompatible");
339 }
340 }
341
342
345 protected void checkNonWeeksOkay(int f) {
346 if ((f != 0) && (weeks != 0)) {
347 throw new IllegalStateException(
348 "Weeks and non-weeks are incompatible");
349 }
350 }
351
352
357 @Override
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 @Override
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 }