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