001    /**
002     * Copyright (c) 2000-2012 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 Moved to {@link com.liferay.portal.kernel.cal.Duration}
054     */
055    public class Duration implements Cloneable, Serializable {
056    
057            /**
058             * Constructor Duration
059             */
060            public Duration() {
061    
062                    /* Zero-initialization of all fields happens by default */
063    
064            }
065    
066            /**
067             * Constructor Duration
068             */
069            public Duration(int d, int h, int m, int s) {
070                    _days = d;
071                    _hours = h;
072                    _minutes = m;
073                    _seconds = s;
074            }
075    
076            /**
077             * Constructor Duration
078             */
079            public Duration(int h, int m, int s) {
080                    this(0, h, m, s);
081            }
082    
083            /**
084             * Constructor Duration
085             */
086            public Duration(int w) {
087                    _weeks = w;
088            }
089    
090            /**
091             * Method clear
092             */
093            public void clear() {
094                    _weeks = 0;
095                    _days = 0;
096                    _hours = 0;
097                    _minutes = 0;
098                    _seconds = 0;
099            }
100    
101            /**
102             * Method clone
103             *
104             * @return Object
105             */
106            @Override
107            public Object clone() {
108                    try {
109                            Duration other = (Duration)super.clone();
110    
111                            other._weeks = _weeks;
112                            other._days = _days;
113                            other._hours = _hours;
114                            other._minutes = _minutes;
115                            other._seconds = _seconds;
116    
117                            return other;
118                    }
119                    catch (CloneNotSupportedException e) {
120                            throw new InternalError();
121                    }
122            }
123    
124            /**
125             * Method getDays
126             *
127             * @return int
128             */
129            public int getDays() {
130                    return _days;
131            }
132    
133            /**
134             * Method getHours
135             *
136             * @return int
137             */
138            public int getHours() {
139                    return _hours;
140            }
141    
142            /**
143             * Method getInterval
144             *
145             * @return long
146             */
147            public long getInterval() {
148                    return _seconds * _MILLIS_PER_SECOND + _minutes * _MILLIS_PER_MINUTE
149                               + _hours * _MILLIS_PER_HOUR + _days * _MILLIS_PER_DAY
150                               + _weeks * _MILLIS_PER_WEEK;
151            }
152    
153            /**
154             * Method getMinutes
155             *
156             * @return int
157             */
158            public int getMinutes() {
159                    return _minutes;
160            }
161    
162            /**
163             * Method getSeconds
164             *
165             * @return int
166             */
167            public int getSeconds() {
168                    return _seconds;
169            }
170    
171            /**
172             * Method getWeeks
173             *
174             * @return int
175             */
176            public int getWeeks() {
177                    return _weeks;
178            }
179    
180            /**
181             * Method setDays
182             */
183            public void setDays(int d) {
184                    if (d < 0) {
185                            throw new IllegalArgumentException("Day value out of range");
186                    }
187    
188                    checkNonWeeksOkay(d);
189    
190                    _days = d;
191    
192                    normalize();
193            }
194    
195            /**
196             * Method setHours
197             */
198            public void setHours(int h) {
199                    if (h < 0) {
200                            throw new IllegalArgumentException("Hour value out of range");
201                    }
202    
203                    checkNonWeeksOkay(h);
204    
205                    _hours = h;
206    
207                    normalize();
208            }
209    
210            /**
211             * Method setInterval
212             */
213            public void setInterval(long millis) {
214                    if (millis < 0) {
215                            throw new IllegalArgumentException("Negative-length interval");
216                    }
217    
218                    clear();
219    
220                    _days = (int)(millis / _MILLIS_PER_DAY);
221                    _seconds = (int)((millis % _MILLIS_PER_DAY) / _MILLIS_PER_SECOND);
222    
223                    normalize();
224            }
225    
226            /**
227             * Method setMinutes
228             */
229            public void setMinutes(int m) {
230                    if (m < 0) {
231                            throw new IllegalArgumentException("Minute value out of range");
232                    }
233    
234                    checkNonWeeksOkay(m);
235    
236                    _minutes = m;
237    
238                    normalize();
239            }
240    
241            /**
242             * Method setSeconds
243             */
244            public void setSeconds(int s) {
245                    if (s < 0) {
246                            throw new IllegalArgumentException("Second value out of range");
247                    }
248    
249                    checkNonWeeksOkay(s);
250    
251                    _seconds = s;
252    
253                    normalize();
254            }
255    
256            /**
257             * Method setWeeks
258             */
259            public void setWeeks(int w) {
260                    if (w < 0) {
261                            throw new IllegalArgumentException("Week value out of range");
262                    }
263    
264                    checkWeeksOkay(w);
265    
266                    _weeks = w;
267            }
268    
269            /**
270             * Method toString
271             *
272             * @return String
273             */
274            @Override
275            public String toString() {
276                    StringBundler sb = new StringBundler(12);
277    
278                    sb.append(getClass().getName());
279                    sb.append("[weeks=");
280                    sb.append(_weeks);
281                    sb.append(",days=");
282                    sb.append(_days);
283                    sb.append(",hours=");
284                    sb.append(_hours);
285                    sb.append(",minutes=");
286                    sb.append(_minutes);
287                    sb.append(",seconds=");
288                    sb.append(_seconds);
289                    sb.append("]");
290    
291                    return sb.toString();
292            }
293    
294            /**
295             * Method checkNonWeeksOkay
296             */
297            protected void checkNonWeeksOkay(int f) {
298                    if ((f != 0) && (_weeks != 0)) {
299                            throw new IllegalStateException(
300                                    "Weeks and non-weeks are incompatible");
301                    }
302            }
303    
304            /**
305             * Method checkWeeksOkay
306             */
307            protected void checkWeeksOkay(int f) {
308                    if ((f != 0)
309                            && ((_days != 0) || (_hours != 0) || (_minutes != 0)
310                                    || (_seconds != 0))) {
311                            throw new IllegalStateException(
312                                    "Weeks and non-weeks are incompatible");
313                    }
314            }
315    
316            /**
317             * Method normalize
318             */
319            protected void normalize() {
320                    _minutes += _seconds / _SECONDS_PER_MINUTE;
321                    _seconds %= _SECONDS_PER_MINUTE;
322                    _hours += _minutes / _MINUTES_PER_HOUR;
323                    _minutes %= _MINUTES_PER_HOUR;
324                    _days += _hours / _HOURS_PER_DAY;
325                    _hours %= _HOURS_PER_DAY;
326            }
327    
328            /**
329             * Field DAYS_PER_WEEK
330             */
331            private static final int _DAYS_PER_WEEK = 7;
332    
333            /**
334             * Field HOURS_PER_DAY
335             */
336            private static final int _HOURS_PER_DAY = 24;
337    
338            /**
339             * Field MILLIS_PER_DAY
340             */
341            private static final int _MILLIS_PER_DAY =
342                    Duration._HOURS_PER_DAY * Duration._MILLIS_PER_HOUR;
343    
344            /**
345             * Field MILLIS_PER_HOUR
346             */
347            private static final int _MILLIS_PER_HOUR =
348                    Duration._MINUTES_PER_HOUR * Duration._MILLIS_PER_MINUTE;
349    
350            /**
351             * Field MILLIS_PER_MINUTE
352             */
353            private static final int _MILLIS_PER_MINUTE =
354                    Duration._SECONDS_PER_MINUTE * Duration._MILLIS_PER_SECOND;
355    
356            /**
357             * Field MILLIS_PER_SECOND
358             */
359            private static final int _MILLIS_PER_SECOND = 1000;
360    
361            /**
362             * Field MILLIS_PER_WEEK
363             */
364            private static final int _MILLIS_PER_WEEK =
365                    Duration._DAYS_PER_WEEK * Duration._MILLIS_PER_DAY;
366    
367            /**
368             * Field MINUTES_PER_HOUR
369             */
370            private static final int _MINUTES_PER_HOUR = 60;
371    
372            /**
373             * Field SECONDS_PER_MINUTE
374             */
375            private static final int _SECONDS_PER_MINUTE = 60;
376    
377            /**
378             * Field days
379             */
380            private int _days;
381    
382            /**
383             * Field hours
384             */
385            private int _hours;
386    
387            /**
388             * Field minutes
389             */
390            private int _minutes;
391    
392            /**
393             * Field seconds
394             */
395            private int _seconds;
396    
397            /**
398             * Field weeks
399             */
400            private int _weeks;
401    
402    }