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