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