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
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 public Object clone() {
358 try {
359 Duration other = (Duration)super.clone();
360
361 other.weeks = weeks;
362 other.days = days;
363 other.hours = hours;
364 other.minutes = minutes;
365 other.seconds = seconds;
366
367 return other;
368 }
369 catch (CloneNotSupportedException e) {
370 throw new InternalError();
371 }
372 }
373
374
379 public String toString() {
380 StringBundler sb = new StringBundler(12);
381
382 sb.append(getClass().getName());
383 sb.append("[weeks=");
384 sb.append(weeks);
385 sb.append(",days=");
386 sb.append(days);
387 sb.append(",hours=");
388 sb.append(hours);
389 sb.append(",minutes=");
390 sb.append(minutes);
391 sb.append(",seconds=");
392 sb.append(seconds);
393 sb.append("]");
394
395 return sb.toString();
396 }
397
398 }