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