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