| Duration.java |
1 /*
2 * Copyright (c) 2000, Columbia University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
19 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 package com.liferay.util.cal;
32
33 import com.liferay.portal.kernel.util.StringMaker;
34
35 import java.io.Serializable;
36
37 /**
38 * <a href="Duration.java.html"><b><i>View Source</i></b></a>
39 *
40 * @author Jonathan Lennox
41 *
42 * @deprecated This class has been repackaged at
43 * <code>com.liferay.portal.kernel.cal</code>.
44 *
45 */
46 public class Duration implements Cloneable, Serializable {
47
48 /**
49 * Field weeks
50 */
51 private int weeks;
52
53 /**
54 * Field days
55 */
56 private int days;
57
58 /**
59 * Field hours
60 */
61 private int hours;
62
63 /**
64 * Field minutes
65 */
66 private int minutes;
67
68 /**
69 * Field seconds
70 */
71 private int seconds;
72
73 /**
74 * Field SECONDS_PER_MINUTE
75 */
76 private final static int SECONDS_PER_MINUTE = 60;
77
78 /**
79 * Field MINUTES_PER_HOUR
80 */
81 private final static int MINUTES_PER_HOUR = 60;
82
83 /**
84 * Field HOURS_PER_DAY
85 */
86 private final static int HOURS_PER_DAY = 24;
87
88 /**
89 * Field DAYS_PER_WEEK
90 */
91 private final static int DAYS_PER_WEEK = 7;
92
93 /**
94 * Field MILLIS_PER_SECOND
95 */
96 private final static int MILLIS_PER_SECOND = 1000;
97
98 /**
99 * Field MILLIS_PER_MINUTE
100 */
101 private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
102 * MILLIS_PER_SECOND;
103
104 /**
105 * Field MILLIS_PER_HOUR
106 */
107 private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
108 * MILLIS_PER_MINUTE;
109
110 /**
111 * Field MILLIS_PER_DAY
112 */
113 private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
114
115 /**
116 * Field MILLIS_PER_WEEK
117 */
118 private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
119
120 /**
121 * Constructor Duration
122 *
123 *
124 */
125 public Duration() {
126
127 /* Zero-initialization of all fields happens by default */
128
129 }
130
131 /**
132 * Constructor Duration
133 *
134 *
135 * @param d
136 * @param h
137 * @param m
138 * @param s
139 *
140 */
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 /**
149 * Constructor Duration
150 *
151 *
152 * @param h
153 * @param m
154 * @param s
155 *
156 */
157 public Duration(int h, int m, int s) {
158 this(0, h, m, s);
159 }
160
161 /**
162 * Constructor Duration
163 *
164 *
165 * @param w
166 *
167 */
168 public Duration(int w) {
169 weeks = w;
170 }
171
172 /**
173 * Method clear
174 *
175 *
176 */
177 public void clear() {
178 weeks = 0;
179 days = 0;
180 hours = 0;
181 minutes = 0;
182 seconds = 0;
183 }
184 ;
185
186 /**
187 * Method getWeeks
188 *
189 *
190 * @return int
191 *
192 */
193 public int getWeeks() {
194 return weeks;
195 }
196
197 /**
198 * Method setWeeks
199 *
200 *
201 * @param w
202 *
203 */
204 public void setWeeks(int w) {
205 if (w < 0) {
206 throw new IllegalArgumentException("Week value out of range");
207 }
208
209 checkWeeksOkay(w);
210
211 weeks = w;
212 }
213
214 /**
215 * Method getDays
216 *
217 *
218 * @return int
219 *
220 */
221 public int getDays() {
222 return days;
223 }
224
225 /**
226 * Method setDays
227 *
228 *
229 * @param d
230 *
231 */
232 public void setDays(int d) {
233 if (d < 0) {
234 throw new IllegalArgumentException("Day value out of range");
235 }
236
237 checkNonWeeksOkay(d);
238
239 days = d;
240
241 normalize();
242 }
243
244 /**
245 * Method getHours
246 *
247 *
248 * @return int
249 *
250 */
251 public int getHours() {
252 return hours;
253 }
254
255 /**
256 * Method setHours
257 *
258 *
259 * @param h
260 *
261 */
262 public void setHours(int h) {
263 if (h < 0) {
264 throw new IllegalArgumentException("Hour value out of range");
265 }
266
267 checkNonWeeksOkay(h);
268
269 hours = h;
270
271 normalize();
272 }
273
274 /**
275 * Method getMinutes
276 *
277 *
278 * @return int
279 *
280 */
281 public int getMinutes() {
282 return minutes;
283 }
284
285 /**
286 * Method setMinutes
287 *
288 *
289 * @param m
290 *
291 */
292 public void setMinutes(int m) {
293 if (m < 0) {
294 throw new IllegalArgumentException("Minute value out of range");
295 }
296
297 checkNonWeeksOkay(m);
298
299 minutes = m;
300
301 normalize();
302 }
303
304 /**
305 * Method getSeconds
306 *
307 *
308 * @return int
309 *
310 */
311 public int getSeconds() {
312 return seconds;
313 }
314
315 /**
316 * Method setSeconds
317 *
318 *
319 * @param s
320 *
321 */
322 public void setSeconds(int s) {
323 if (s < 0) {
324 throw new IllegalArgumentException("Second value out of range");
325 }
326
327 checkNonWeeksOkay(s);
328
329 seconds = s;
330
331 normalize();
332 }
333
334 /**
335 * Method getInterval
336 *
337 *
338 * @return long
339 *
340 */
341 public long getInterval() {
342 return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
343 + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
344 + weeks * MILLIS_PER_WEEK;
345 }
346
347 /**
348 * Method setInterval
349 *
350 *
351 * @param millis
352 *
353 */
354 public void setInterval(long millis) {
355 if (millis < 0) {
356 throw new IllegalArgumentException("Negative-length interval");
357 }
358
359 clear();
360
361 days = (int)(millis / MILLIS_PER_DAY);
362 seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
363
364 normalize();
365 }
366
367 /**
368 * Method normalize
369 *
370 *
371 */
372 protected void normalize() {
373 minutes += seconds / SECONDS_PER_MINUTE;
374 seconds %= SECONDS_PER_MINUTE;
375 hours += minutes / MINUTES_PER_HOUR;
376 minutes %= MINUTES_PER_HOUR;
377 days += hours / HOURS_PER_DAY;
378 hours %= HOURS_PER_DAY;
379 }
380
381 /**
382 * Method checkWeeksOkay
383 *
384 *
385 * @param f
386 *
387 */
388 protected void checkWeeksOkay(int f) {
389 if ((f != 0)
390 && ((days != 0) || (hours != 0) || (minutes != 0)
391 || (seconds != 0))) {
392 throw new IllegalStateException(
393 "Weeks and non-weeks are incompatible");
394 }
395 }
396
397 /**
398 * Method checkNonWeeksOkay
399 *
400 *
401 * @param f
402 *
403 */
404 protected void checkNonWeeksOkay(int f) {
405 if ((f != 0) && (weeks != 0)) {
406 throw new IllegalStateException(
407 "Weeks and non-weeks are incompatible");
408 }
409 }
410
411 /**
412 * Method clone
413 *
414 *
415 * @return Object
416 *
417 */
418 public Object clone() {
419 try {
420 Duration other = (Duration)super.clone();
421
422 other.weeks = weeks;
423 other.days = days;
424 other.hours = hours;
425 other.minutes = minutes;
426 other.seconds = seconds;
427
428 return other;
429 }
430 catch (CloneNotSupportedException e) {
431 throw new InternalError();
432 }
433 }
434
435 /**
436 * Method toString
437 *
438 *
439 * @return String
440 *
441 */
442 public String toString() {
443 StringMaker sm = new StringMaker();
444
445 sm.append(getClass().getName());
446 sm.append("[weeks=");
447 sm.append(weeks);
448 sm.append(",days=");
449 sm.append(days);
450 sm.append(",hours=");
451 sm.append(hours);
452 sm.append(",minutes=");
453 sm.append(minutes);
454 sm.append(",seconds=");
455 sm.append(seconds);
456 sm.append("]");
457
458 return sm.toString();
459 }
460
461 }