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