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