001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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             * Field weeks
059             */
060            private int weeks;
061    
062            /**
063             * Field days
064             */
065            private int days;
066    
067            /**
068             * Field hours
069             */
070            private int hours;
071    
072            /**
073             * Field minutes
074             */
075            private int minutes;
076    
077            /**
078             * Field seconds
079             */
080            private int seconds;
081    
082            /**
083             * Field SECONDS_PER_MINUTE
084             */
085            private static final int SECONDS_PER_MINUTE = 60;
086    
087            /**
088             * Field MINUTES_PER_HOUR
089             */
090            private static final int MINUTES_PER_HOUR = 60;
091    
092            /**
093             * Field HOURS_PER_DAY
094             */
095            private static final int HOURS_PER_DAY = 24;
096    
097            /**
098             * Field DAYS_PER_WEEK
099             */
100            private static final int DAYS_PER_WEEK = 7;
101    
102            /**
103             * Field MILLIS_PER_SECOND
104             */
105            private static final int MILLIS_PER_SECOND = 1000;
106    
107            /**
108             * Field MILLIS_PER_MINUTE
109             */
110            private static final int MILLIS_PER_MINUTE = SECONDS_PER_MINUTE
111                                                                                                     * MILLIS_PER_SECOND;
112    
113            /**
114             * Field MILLIS_PER_HOUR
115             */
116            private static final int MILLIS_PER_HOUR = MINUTES_PER_HOUR
117                                                                                               * MILLIS_PER_MINUTE;
118    
119            /**
120             * Field MILLIS_PER_DAY
121             */
122            private static final int MILLIS_PER_DAY = HOURS_PER_DAY * MILLIS_PER_HOUR;
123    
124            /**
125             * Field MILLIS_PER_WEEK
126             */
127            private static final int MILLIS_PER_WEEK = DAYS_PER_WEEK * MILLIS_PER_DAY;
128    
129            /**
130             * Constructor Duration
131             */
132            public Duration() {
133    
134                    /* Zero-initialization of all fields happens by default */
135    
136            }
137    
138            /**
139             * Constructor Duration
140             */
141            public Duration(int d, int h, int m, int s) {
142                    days = d;
143                    hours = h;
144                    minutes = m;
145                    seconds = s;
146            }
147    
148            /**
149             * Constructor Duration
150             */
151            public Duration(int h, int m, int s) {
152                    this(0, h, m, s);
153            }
154    
155            /**
156             * Constructor Duration
157             */
158            public Duration(int w) {
159                    weeks = w;
160            }
161    
162            /**
163             * Method clear
164             */
165            public void clear() {
166                    weeks = 0;
167                    days = 0;
168                    hours = 0;
169                    minutes = 0;
170                    seconds = 0;
171            }
172    
173            /**
174             * Method getWeeks
175             *
176             * @return int
177             */
178            public int getWeeks() {
179                    return weeks;
180            }
181    
182            /**
183             * Method setWeeks
184             */
185            public void setWeeks(int w) {
186                    if (w < 0) {
187                            throw new IllegalArgumentException("Week value out of range");
188                    }
189    
190                    checkWeeksOkay(w);
191    
192                    weeks = w;
193            }
194    
195            /**
196             * Method getDays
197             *
198             * @return int
199             */
200            public int getDays() {
201                    return days;
202            }
203    
204            /**
205             * Method setDays
206             */
207            public void setDays(int d) {
208                    if (d < 0) {
209                            throw new IllegalArgumentException("Day value out of range");
210                    }
211    
212                    checkNonWeeksOkay(d);
213    
214                    days = d;
215    
216                    normalize();
217            }
218    
219            /**
220             * Method getHours
221             *
222             * @return int
223             */
224            public int getHours() {
225                    return hours;
226            }
227    
228            /**
229             * Method setHours
230             */
231            public void setHours(int h) {
232                    if (h < 0) {
233                            throw new IllegalArgumentException("Hour value out of range");
234                    }
235    
236                    checkNonWeeksOkay(h);
237    
238                    hours = h;
239    
240                    normalize();
241            }
242    
243            /**
244             * Method getMinutes
245             *
246             * @return int
247             */
248            public int getMinutes() {
249                    return minutes;
250            }
251    
252            /**
253             * Method setMinutes
254             */
255            public void setMinutes(int m) {
256                    if (m < 0) {
257                            throw new IllegalArgumentException("Minute value out of range");
258                    }
259    
260                    checkNonWeeksOkay(m);
261    
262                    minutes = m;
263    
264                    normalize();
265            }
266    
267            /**
268             * Method getSeconds
269             *
270             * @return int
271             */
272            public int getSeconds() {
273                    return seconds;
274            }
275    
276            /**
277             * Method setSeconds
278             */
279            public void setSeconds(int s) {
280                    if (s < 0) {
281                            throw new IllegalArgumentException("Second value out of range");
282                    }
283    
284                    checkNonWeeksOkay(s);
285    
286                    seconds = s;
287    
288                    normalize();
289            }
290    
291            /**
292             * Method getInterval
293             *
294             * @return long
295             */
296            public long getInterval() {
297                    return seconds * MILLIS_PER_SECOND + minutes * MILLIS_PER_MINUTE
298                               + hours * MILLIS_PER_HOUR + days * MILLIS_PER_DAY
299                               + weeks * MILLIS_PER_WEEK;
300            }
301    
302            /**
303             * Method setInterval
304             */
305            public void setInterval(long millis) {
306                    if (millis < 0) {
307                            throw new IllegalArgumentException("Negative-length interval");
308                    }
309    
310                    clear();
311    
312                    days = (int)(millis / MILLIS_PER_DAY);
313                    seconds = (int)((millis % MILLIS_PER_DAY) / MILLIS_PER_SECOND);
314    
315                    normalize();
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             * Method checkWeeksOkay
332             */
333            protected void checkWeeksOkay(int f) {
334                    if ((f != 0)
335                            && ((days != 0) || (hours != 0) || (minutes != 0)
336                                    || (seconds != 0))) {
337                            throw new IllegalStateException(
338                                    "Weeks and non-weeks are incompatible");
339                    }
340            }
341    
342            /**
343             * Method checkNonWeeksOkay
344             */
345            protected void checkNonWeeksOkay(int f) {
346                    if ((f != 0) && (weeks != 0)) {
347                            throw new IllegalStateException(
348                                    "Weeks and non-weeks are incompatible");
349                    }
350            }
351    
352            /**
353             * Method clone
354             *
355             * @return Object
356             */
357            @Override
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            @Override
381            public String toString() {
382                    StringBundler sb = new StringBundler(12);
383    
384                    sb.append(getClass().getName());
385                    sb.append("[weeks=");
386                    sb.append(weeks);
387                    sb.append(",days=");
388                    sb.append(days);
389                    sb.append(",hours=");
390                    sb.append(hours);
391                    sb.append(",minutes=");
392                    sb.append(minutes);
393                    sb.append(",seconds=");
394                    sb.append(seconds);
395                    sb.append("]");
396    
397                    return sb.toString();
398            }
399    
400    }