1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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.util.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   * @deprecated This class has been repackaged at
56   *             <code>com.liferay.portal.kernel.cal</code>.
57   */
58  public class Duration implements Cloneable, Serializable {
59  
60      /**
61       * Field weeks
62       */
63      private int weeks;
64  
65      /**
66       * Field days
67       */
68      private int days;
69  
70      /**
71       * Field hours
72       */
73      private int hours;
74  
75      /**
76       * Field minutes
77       */
78      private int minutes;
79  
80      /**
81       * Field seconds
82       */
83      private int seconds;
84  
85      /**
86       * Field SECONDS_PER_MINUTE
87       */
88      private final static int SECONDS_PER_MINUTE = 60;
89  
90      /**
91       * Field MINUTES_PER_HOUR
92       */
93      private final static int MINUTES_PER_HOUR = 60;
94  
95      /**
96       * Field HOURS_PER_DAY
97       */
98      private final static int HOURS_PER_DAY = 24;
99  
100     /**
101      * Field DAYS_PER_WEEK
102      */
103     private final static int DAYS_PER_WEEK = 7;
104 
105     /**
106      * Field MILLIS_PER_SECOND
107      */
108     private final static int MILLIS_PER_SECOND = 1000;
109 
110     /**
111      * Field MILLIS_PER_MINUTE
112      */
113     private final static int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
114                                                  * MILLIS_PER_SECOND;
115 
116     /**
117      * Field MILLIS_PER_HOUR
118      */
119     private final static int MILLIS_PER_HOUR = MINUTES_PER_HOUR
120                                                * MILLIS_PER_MINUTE;
121 
122     /**
123      * Field MILLIS_PER_DAY
124      */
125     private final static int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
126 
127     /**
128      * Field MILLIS_PER_WEEK
129      */
130     private final static int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
131 
132     /**
133      * Constructor Duration
134      */
135     public Duration() {
136 
137         /* Zero-initialization of all fields happens by default */
138 
139     }
140 
141     /**
142      * Constructor Duration
143      */
144     public Duration(int d, int h, int m, int s) {
145         days = d;
146         hours = h;
147         minutes = m;
148         seconds = s;
149     }
150 
151     /**
152      * Constructor Duration
153      */
154     public Duration(int h, int m, int s) {
155         this(0, h, m, s);
156     }
157 
158     /**
159      * Constructor Duration
160      */
161     public Duration(int w) {
162         weeks = w;
163     }
164 
165     /**
166      * Method clear
167      */
168     public void clear() {
169         weeks = 0;
170         days = 0;
171         hours = 0;
172         minutes = 0;
173         seconds = 0;
174     }
175     ;
176 
177     /**
178      * Method getWeeks
179      *
180      * @return int
181      */
182     public int getWeeks() {
183         return weeks;
184     }
185 
186     /**
187      * Method setWeeks
188      */
189     public void setWeeks(int w) {
190         if (w < 0) {
191             throw new IllegalArgumentException("Week value out of range");
192         }
193 
194         checkWeeksOkay(w);
195 
196         weeks = w;
197     }
198 
199     /**
200      * Method getDays
201      *
202      * @return int
203      */
204     public int getDays() {
205         return days;
206     }
207 
208     /**
209      * Method setDays
210      */
211     public void setDays(int d) {
212         if (d < 0) {
213             throw new IllegalArgumentException("Day value out of range");
214         }
215 
216         checkNonWeeksOkay(d);
217 
218         days = d;
219 
220         normalize();
221     }
222 
223     /**
224      * Method getHours
225      *
226      * @return int
227      */
228     public int getHours() {
229         return hours;
230     }
231 
232     /**
233      * Method setHours
234      */
235     public void setHours(int h) {
236         if (h < 0) {
237             throw new IllegalArgumentException("Hour value out of range");
238         }
239 
240         checkNonWeeksOkay(h);
241 
242         hours = h;
243 
244         normalize();
245     }
246 
247     /**
248      * Method getMinutes
249      *
250      * @return int
251      */
252     public int getMinutes() {
253         return minutes;
254     }
255 
256     /**
257      * Method setMinutes
258      */
259     public void setMinutes(int m) {
260         if (m < 0) {
261             throw new IllegalArgumentException("Minute value out of range");
262         }
263 
264         checkNonWeeksOkay(m);
265 
266         minutes = m;
267 
268         normalize();
269     }
270 
271     /**
272      * Method getSeconds
273      *
274      * @return int
275      */
276     public int getSeconds() {
277         return seconds;
278     }
279 
280     /**
281      * Method setSeconds
282      */
283     public void setSeconds(int s) {
284         if (s < 0) {
285             throw new IllegalArgumentException("Second value out of range");
286         }
287 
288         checkNonWeeksOkay(s);
289 
290         seconds = s;
291 
292         normalize();
293     }
294 
295     /**
296      * Method getInterval
297      *
298      * @return long
299      */
300     public long getInterval() {
301         return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
302                + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
303                + weeks * MILLIS_PER_WEEK;
304     }
305 
306     /**
307      * Method setInterval
308      */
309     public void setInterval(long millis) {
310         if (millis < 0) {
311             throw new IllegalArgumentException("Negative-length interval");
312         }
313 
314         clear();
315 
316         days = (int)(millis / MILLIS_PER_DAY);
317         seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
318 
319         normalize();
320     }
321 
322     /**
323      * Method normalize
324      */
325     protected void normalize() {
326         minutes += seconds / SECONDS_PER_MINUTE;
327         seconds %= SECONDS_PER_MINUTE;
328         hours += minutes / MINUTES_PER_HOUR;
329         minutes %= MINUTES_PER_HOUR;
330         days += hours / HOURS_PER_DAY;
331         hours %= HOURS_PER_DAY;
332     }
333 
334     /**
335      * Method checkWeeksOkay
336      */
337     protected void checkWeeksOkay(int f) {
338         if ((f != 0)
339             && ((days != 0) || (hours != 0) || (minutes != 0)
340                 || (seconds != 0))) {
341             throw new IllegalStateException(
342                 "Weeks and non-weeks are incompatible");
343         }
344     }
345 
346     /**
347      * Method checkNonWeeksOkay
348      */
349     protected void checkNonWeeksOkay(int f) {
350         if ((f != 0) && (weeks != 0)) {
351             throw new IllegalStateException(
352                 "Weeks and non-weeks are incompatible");
353         }
354     }
355 
356     /**
357      * Method clone
358      *
359      * @return Object
360      */
361     public Object clone() {
362         try {
363             Duration other = (Duration)super.clone();
364 
365             other.weeks = weeks;
366             other.days = days;
367             other.hours = hours;
368             other.minutes = minutes;
369             other.seconds = seconds;
370 
371             return other;
372         }
373         catch (CloneNotSupportedException e) {
374             throw new InternalError();
375         }
376     }
377 
378     /**
379      * Method toString
380      *
381      * @return String
382      */
383     public String toString() {
384         StringBundler sb = new StringBundler(12);
385 
386         sb.append(getClass().getName());
387         sb.append("[weeks=");
388         sb.append(weeks);
389         sb.append(",days=");
390         sb.append(days);
391         sb.append(",hours=");
392         sb.append(hours);
393         sb.append(",minutes=");
394         sb.append(minutes);
395         sb.append(",seconds=");
396         sb.append(seconds);
397         sb.append("]");
398 
399         return sb.toString();
400     }
401 
402 }