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