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