001    /**
002     * Copyright (c) 2000-2013 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 w) {
070                    _weeks = w;
071            }
072    
073            /**
074             * Constructor Duration
075             */
076            public Duration(int h, int m, int s) {
077                    this(0, h, m, s);
078            }
079    
080            /**
081             * Constructor Duration
082             */
083            public Duration(int d, int h, int m, int s) {
084                    _days = d;
085                    _hours = h;
086                    _minutes = m;
087                    _seconds = s;
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 cnse) {
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
149                            _seconds * _MILLIS_PER_SECOND + _minutes * _MILLIS_PER_MINUTE +
150                            _hours * _MILLIS_PER_HOUR + _days * _MILLIS_PER_DAY +
151                            _weeks * _MILLIS_PER_WEEK;
152            }
153    
154            /**
155             * Method getMinutes
156             *
157             * @return int
158             */
159            public int getMinutes() {
160                    return _minutes;
161            }
162    
163            /**
164             * Method getSeconds
165             *
166             * @return int
167             */
168            public int getSeconds() {
169                    return _seconds;
170            }
171    
172            /**
173             * Method getWeeks
174             *
175             * @return int
176             */
177            public int getWeeks() {
178                    return _weeks;
179            }
180    
181            /**
182             * Method setDays
183             */
184            public void setDays(int d) {
185                    if (d < 0) {
186                            throw new IllegalArgumentException("Day value out of range");
187                    }
188    
189                    checkNonWeeksOkay(d);
190    
191                    _days = d;
192    
193                    normalize();
194            }
195    
196            /**
197             * Method setHours
198             */
199            public void setHours(int h) {
200                    if (h < 0) {
201                            throw new IllegalArgumentException("Hour value out of range");
202                    }
203    
204                    checkNonWeeksOkay(h);
205    
206                    _hours = h;
207    
208                    normalize();
209            }
210    
211            /**
212             * Method setInterval
213             */
214            public void setInterval(long millis) {
215                    if (millis < 0) {
216                            throw new IllegalArgumentException("Negative-length interval");
217                    }
218    
219                    clear();
220    
221                    _days = (int)(millis / _MILLIS_PER_DAY);
222                    _seconds = (int)((millis % _MILLIS_PER_DAY) / _MILLIS_PER_SECOND);
223    
224                    normalize();
225            }
226    
227            /**
228             * Method setMinutes
229             */
230            public void setMinutes(int m) {
231                    if (m < 0) {
232                            throw new IllegalArgumentException("Minute value out of range");
233                    }
234    
235                    checkNonWeeksOkay(m);
236    
237                    _minutes = m;
238    
239                    normalize();
240            }
241    
242            /**
243             * Method setSeconds
244             */
245            public void setSeconds(int s) {
246                    if (s < 0) {
247                            throw new IllegalArgumentException("Second value out of range");
248                    }
249    
250                    checkNonWeeksOkay(s);
251    
252                    _seconds = s;
253    
254                    normalize();
255            }
256    
257            /**
258             * Method setWeeks
259             */
260            public void setWeeks(int w) {
261                    if (w < 0) {
262                            throw new IllegalArgumentException("Week value out of range");
263                    }
264    
265                    checkWeeksOkay(w);
266    
267                    _weeks = w;
268            }
269    
270            /**
271             * Method toString
272             *
273             * @return String
274             */
275            @Override
276            public String toString() {
277                    StringBundler sb = new StringBundler(12);
278    
279                    sb.append(getClass().getName());
280                    sb.append("[weeks=");
281                    sb.append(_weeks);
282                    sb.append(",days=");
283                    sb.append(_days);
284                    sb.append(",hours=");
285                    sb.append(_hours);
286                    sb.append(",minutes=");
287                    sb.append(_minutes);
288                    sb.append(",seconds=");
289                    sb.append(_seconds);
290                    sb.append("]");
291    
292                    return sb.toString();
293            }
294    
295            /**
296             * Method checkNonWeeksOkay
297             */
298            protected void checkNonWeeksOkay(int f) {
299                    if ((f != 0) && (_weeks != 0)) {
300                            throw new IllegalStateException(
301                                    "Weeks and non-weeks are incompatible");
302                    }
303            }
304    
305            /**
306             * Method checkWeeksOkay
307             */
308            protected void checkWeeksOkay(int f) {
309                    if ((f != 0) &&
310                            ((_days != 0) || (_hours != 0) || (_minutes != 0) ||
311                             (_seconds != 0))) {
312    
313                            throw new IllegalStateException(
314                                    "Weeks and non-weeks are incompatible");
315                    }
316            }
317    
318            /**
319             * Method normalize
320             */
321            protected void normalize() {
322                    _minutes += _seconds / _SECONDS_PER_MINUTE;
323                    _seconds %= _SECONDS_PER_MINUTE;
324                    _hours += _minutes / _MINUTES_PER_HOUR;
325                    _minutes %= _MINUTES_PER_HOUR;
326                    _days += _hours / _HOURS_PER_DAY;
327                    _hours %= _HOURS_PER_DAY;
328            }
329    
330            /**
331             * Field DAYS_PER_WEEK
332             */
333            private static final int _DAYS_PER_WEEK = 7;
334    
335            /**
336             * Field HOURS_PER_DAY
337             */
338            private static final int _HOURS_PER_DAY = 24;
339    
340            /**
341             * Field MILLIS_PER_DAY
342             */
343            private static final int _MILLIS_PER_DAY =
344                    Duration._HOURS_PER_DAY * Duration._MILLIS_PER_HOUR;
345    
346            /**
347             * Field MILLIS_PER_HOUR
348             */
349            private static final int _MILLIS_PER_HOUR =
350                    Duration._MINUTES_PER_HOUR * Duration._MILLIS_PER_MINUTE;
351    
352            /**
353             * Field MILLIS_PER_MINUTE
354             */
355            private static final int _MILLIS_PER_MINUTE =
356                    Duration._SECONDS_PER_MINUTE * Duration._MILLIS_PER_SECOND;
357    
358            /**
359             * Field MILLIS_PER_SECOND
360             */
361            private static final int _MILLIS_PER_SECOND = 1000;
362    
363            /**
364             * Field MILLIS_PER_WEEK
365             */
366            private static final int _MILLIS_PER_WEEK =
367                    Duration._DAYS_PER_WEEK * Duration._MILLIS_PER_DAY;
368    
369            /**
370             * Field MINUTES_PER_HOUR
371             */
372            private static final int _MINUTES_PER_HOUR = 60;
373    
374            /**
375             * Field SECONDS_PER_MINUTE
376             */
377            private static final int _SECONDS_PER_MINUTE = 60;
378    
379            /**
380             * Field days
381             */
382            private int _days;
383    
384            /**
385             * Field hours
386             */
387            private int _hours;
388    
389            /**
390             * Field minutes
391             */
392            private int _minutes;
393    
394            /**
395             * Field seconds
396             */
397            private int _seconds;
398    
399            /**
400             * Field weeks
401             */
402            private int _weeks;
403    
404    }