001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    /*
016     * Copyright (c) 2000, Columbia University.  All rights reserved.
017     *
018     * Redistribution and use in source and binary forms, with or without
019     * modification, are permitted provided that the following conditions are met:
020     *
021     * 1. Redistributions of source code must retain the above copyright
022     *        notice, this list of conditions and the following disclaimer.
023     *
024     * 2. Redistributions in binary form must reproduce the above copyright
025     *        notice, this list of conditions and the following disclaimer in the
026     *        documentation and/or other materials provided with the distribution.
027     *
028     * 3. Neither the name of the University nor the names of its contributors
029     *        may be used to endorse or promote products derived from this software
030     *        without specific prior written permission.
031     *
032     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
033     * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
034     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
035     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
036     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
037     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
038     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
039     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
040     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
041     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
042     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
043     */
044    
045    package com.liferay.util.cal;
046    
047    import com.liferay.portal.kernel.util.StringBundler;
048    
049    import java.io.Serializable;
050    
051    /**
052     * @author         Jonathan Lennox
053     * @deprecated This class has been repackaged at
054     *                         <code>com.liferay.portal.kernel.cal</code>.
055     */
056    public class Duration implements Cloneable, Serializable {
057    
058            /**
059             * Field weeks
060             */
061            private int weeks;
062    
063            /**
064             * Field days
065             */
066            private int days;
067    
068            /**
069             * Field hours
070             */
071            private int hours;
072    
073            /**
074             * Field minutes
075             */
076            private int minutes;
077    
078            /**
079             * Field seconds
080             */
081            private int seconds;
082    
083            /**
084             * Field SECONDS_PER_MINUTE
085             */
086            private final static int SECONDS_PER_MINUTE = 60;
087    
088            /**
089             * Field MINUTES_PER_HOUR
090             */
091            private final static int MINUTES_PER_HOUR = 60;
092    
093            /**
094             * Field HOURS_PER_DAY
095             */
096            private final static int HOURS_PER_DAY = 24;
097    
098            /**
099             * 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             * Method getWeeks
176             *
177             * @return int
178             */
179            public int getWeeks() {
180                    return weeks;
181            }
182    
183            /**
184             * Method setWeeks
185             */
186            public void setWeeks(int w) {
187                    if (w < 0) {
188                            throw new IllegalArgumentException("Week value out of range");
189                    }
190    
191                    checkWeeksOkay(w);
192    
193                    weeks = w;
194            }
195    
196            /**
197             * Method getDays
198             *
199             * @return int
200             */
201            public int getDays() {
202                    return days;
203            }
204    
205            /**
206             * Method setDays
207             */
208            public void setDays(int d) {
209                    if (d < 0) {
210                            throw new IllegalArgumentException("Day value out of range");
211                    }
212    
213                    checkNonWeeksOkay(d);
214    
215                    days = d;
216    
217                    normalize();
218            }
219    
220            /**
221             * Method getHours
222             *
223             * @return int
224             */
225            public int getHours() {
226                    return hours;
227            }
228    
229            /**
230             * Method setHours
231             */
232            public void setHours(int h) {
233                    if (h < 0) {
234                            throw new IllegalArgumentException("Hour value out of range");
235                    }
236    
237                    checkNonWeeksOkay(h);
238    
239                    hours = h;
240    
241                    normalize();
242            }
243    
244            /**
245             * Method getMinutes
246             *
247             * @return int
248             */
249            public int getMinutes() {
250                    return minutes;
251            }
252    
253            /**
254             * Method setMinutes
255             */
256            public void setMinutes(int m) {
257                    if (m < 0) {
258                            throw new IllegalArgumentException("Minute value out of range");
259                    }
260    
261                    checkNonWeeksOkay(m);
262    
263                    minutes = m;
264    
265                    normalize();
266            }
267    
268            /**
269             * Method getSeconds
270             *
271             * @return int
272             */
273            public int getSeconds() {
274                    return seconds;
275            }
276    
277            /**
278             * Method setSeconds
279             */
280            public void setSeconds(int s) {
281                    if (s < 0) {
282                            throw new IllegalArgumentException("Second value out of range");
283                    }
284    
285                    checkNonWeeksOkay(s);
286    
287                    seconds = s;
288    
289                    normalize();
290            }
291    
292            /**
293             * Method getInterval
294             *
295             * @return long
296             */
297            public long getInterval() {
298                    return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
299                               + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
300                               + weeks * MILLIS_PER_WEEK;
301            }
302    
303            /**
304             * Method setInterval
305             */
306            public void setInterval(long millis) {
307                    if (millis < 0) {
308                            throw new IllegalArgumentException("Negative-length interval");
309                    }
310    
311                    clear();
312    
313                    days = (int)(millis / MILLIS_PER_DAY);
314                    seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
315    
316                    normalize();
317            }
318    
319            /**
320             * Method normalize
321             */
322            protected void normalize() {
323                    minutes += seconds / SECONDS_PER_MINUTE;
324                    seconds %= SECONDS_PER_MINUTE;
325                    hours += minutes / MINUTES_PER_HOUR;
326                    minutes %= MINUTES_PER_HOUR;
327                    days += hours / HOURS_PER_DAY;
328                    hours %= HOURS_PER_DAY;
329            }
330    
331            /**
332             * Method checkWeeksOkay
333             */
334            protected void checkWeeksOkay(int f) {
335                    if ((f != 0)
336                            && ((days != 0) || (hours != 0) || (minutes != 0)
337                                    || (seconds != 0))) {
338                            throw new IllegalStateException(
339                                    "Weeks and non-weeks are incompatible");
340                    }
341            }
342    
343            /**
344             * Method checkNonWeeksOkay
345             */
346            protected void checkNonWeeksOkay(int f) {
347                    if ((f != 0) && (weeks != 0)) {
348                            throw new IllegalStateException(
349                                    "Weeks and non-weeks are incompatible");
350                    }
351            }
352    
353            /**
354             * Method clone
355             *
356             * @return Object
357             */
358            public Object clone() {
359                    try {
360                            Duration other = (Duration)super.clone();
361    
362                            other.weeks = weeks;
363                            other.days = days;
364                            other.hours = hours;
365                            other.minutes = minutes;
366                            other.seconds = seconds;
367    
368                            return other;
369                    }
370                    catch (CloneNotSupportedException e) {
371                            throw new InternalError();
372                    }
373            }
374    
375            /**
376             * Method toString
377             *
378             * @return String
379             */
380            public String toString() {
381                    StringBundler sb = new StringBundler(12);
382    
383                    sb.append(getClass().getName());
384                    sb.append("[weeks=");
385                    sb.append(weeks);
386                    sb.append(",days=");
387                    sb.append(days);
388                    sb.append(",hours=");
389                    sb.append(hours);
390                    sb.append(",minutes=");
391                    sb.append(minutes);
392                    sb.append(",seconds=");
393                    sb.append(seconds);
394                    sb.append("]");
395    
396                    return sb.toString();
397            }
398    
399    }