KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > impl > calendar > BaseCalendar


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  * and Juergen Donnerstag (c) 2002, EDS 2002
21  */

22
23 package org.quartz.impl.calendar;
24
25 import java.io.Serializable JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.TimeZone JavaDoc;
28
29 import org.quartz.Calendar;
30
31 /**
32  * <p>
33  * This implementation of the Calendar may be used (you don't have to) as a
34  * base class for more sophisticated one's. It merely implements the base
35  * functionality required by each Calendar.
36  * </p>
37  *
38  * <p>
39  * Regarded as base functionality is the treatment of base calendars. Base
40  * calendar allow you to chain (stack) as much calendars as you may need. For
41  * example to exclude weekends you may use WeeklyCalendar. In order to exclude
42  * holidays as well you may define a WeeklyCalendar instance to be the base
43  * calendar for HolidayCalendar instance.
44  * </p>
45  *
46  * @see org.quartz.Calendar
47  *
48  * @author Juergen Donnerstag
49  * @author James House
50  */

51 public class BaseCalendar implements Calendar, Serializable JavaDoc {
52
53     static final long serialVersionUID = 3106623404629760239L;
54     
55     // <p>A optional base calendar.</p>
56
private Calendar baseCalendar;
57
58     private String JavaDoc description;
59     
60     private TimeZone JavaDoc timeZone;
61
62     public BaseCalendar() {
63     }
64
65     public BaseCalendar(Calendar baseCalendar) {
66         setBaseCalendar(baseCalendar);
67     }
68
69     /**
70      * @param timeZone The time zone to use for this Calendar, <code>null</code>
71      * if <code>{@link TimeZone#getDefault()}</code> should be used
72      */

73     public BaseCalendar(TimeZone JavaDoc timeZone) {
74         setTimeZone(timeZone);
75     }
76
77     /**
78      * @param timeZone The time zone to use for this Calendar, <code>null</code>
79      * if <code>{@link TimeZone#getDefault()}</code> should be used
80      */

81     public BaseCalendar(Calendar baseCalendar, TimeZone JavaDoc timeZone) {
82         setBaseCalendar(baseCalendar);
83         setTimeZone(timeZone);
84     }
85
86     /**
87      * <p>
88      * Set a new base calendar or remove the existing one
89      * </p>
90      */

91     public void setBaseCalendar(Calendar baseCalendar) {
92         this.baseCalendar = baseCalendar;
93     }
94
95     /**
96      * <p>
97      * Get the base calendar. Will be null, if not set.
98      * </p>
99      */

100     public Calendar getBaseCalendar() {
101         return this.baseCalendar;
102     }
103
104     /**
105      * <p>
106      * Return the description given to the <code>Calendar</code> instance by
107      * its creator (if any).
108      * </p>
109      *
110      * @return null if no description was set.
111      */

112     public String JavaDoc getDescription() {
113         return description;
114     }
115
116     /**
117      * <p>
118      * Set a description for the <code>Calendar</code> instance - may be
119      * useful for remembering/displaying the purpose of the calendar, though
120      * the description has no meaning to Quartz.
121      * </p>
122      */

123     public void setDescription(String JavaDoc description) {
124         this.description = description;
125     }
126
127     /**
128      * Returns the time zone for which this <code>Calendar</code> will be
129      * resolved.
130      *
131      * @return This Calendar's timezone, <code>null</code> if Calendar should
132      * use the <code>{@link TimeZone#getDefault()}</code>
133      */

134     public TimeZone JavaDoc getTimeZone() {
135         return timeZone;
136     }
137
138     /**
139      * Sets the time zone for which this <code>Calendar</code> will be resolved.
140      *
141      * @param timeZone The time zone to use for this Calendar, <code>null</code>
142      * if <code>{@link TimeZone#getDefault()}</code> should be used
143      */

144     public void setTimeZone(TimeZone JavaDoc timeZone) {
145         this.timeZone = timeZone;
146     }
147     
148     /**
149      * <p>
150      * Check if date/time represented by timeStamp is included. If included
151      * return true. The implementation of BaseCalendar simply calls the base
152      * calendars isTimeIncluded() method if base calendar is set.
153      * </p>
154      *
155      * @see org.quartz.Calendar#isTimeIncluded(long)
156      */

157     public boolean isTimeIncluded(long timeStamp) {
158
159         if (timeStamp <= 0) {
160             throw new IllegalArgumentException JavaDoc(
161                     "timeStamp must be greater 0");
162         }
163
164         if (baseCalendar != null) {
165             if (baseCalendar.isTimeIncluded(timeStamp) == false) { return false; }
166         }
167
168         return true;
169     }
170
171     /**
172      * <p>
173      * Determine the next time (in milliseconds) that is 'included' by the
174      * Calendar after the given time. Return the original value if timeStamp is
175      * included. Return 0 if all days are excluded.
176      * </p>
177      *
178      * @see org.quartz.Calendar#getNextIncludedTime(long)
179      */

180     public long getNextIncludedTime(long timeStamp) {
181
182         if (timeStamp <= 0) {
183             throw new IllegalArgumentException JavaDoc(
184                     "timeStamp must be greater 0");
185         }
186
187         if (baseCalendar != null) {
188             return baseCalendar.getNextIncludedTime(timeStamp);
189         }
190
191         return timeStamp;
192     }
193
194     /**
195      * Utility method. Return the date of excludeDate. The time fraction will
196      * be reset to 00.00:00.
197      *
198      * @deprecated Always uses the default time zone.
199      */

200     public static Date JavaDoc buildHoliday(Date JavaDoc excludedDate) {
201         return new BaseCalendar().getStartOfDayJavaCalendar(excludedDate.getTime()).getTime();
202     }
203
204     /**
205      * Utility method. Return just the date of timeStamp. The time fraction
206      * will be reset to 00.00:00.
207      *
208      * @deprecated Always uses the default time zone.
209      */

210     public static long buildHoliday(long timeStamp) {
211         return new BaseCalendar().getStartOfDayJavaCalendar(timeStamp).getTime().getTime();
212     }
213
214     /**
215      * Utility method. Return a java.util.Calendar for timeStamp.
216      *
217      * @deprecated Always uses the default time zone.
218      */

219     public static java.util.Calendar JavaDoc getJavaCalendar(long timeStamp) {
220         return new BaseCalendar().createJavaCalendar(timeStamp);
221     }
222     
223     /**
224      * Build a <code>{@link java.util.Calendar}</code> for the given timeStamp.
225      * The new Calendar will use the <code>BaseCalendar</code> time zone if it
226      * is not <code>null</code>.
227      */

228     protected java.util.Calendar JavaDoc createJavaCalendar(long timeStamp) {
229         java.util.Calendar JavaDoc calendar = createJavaCalendar();
230         calendar.setTime(new Date JavaDoc(timeStamp));
231         return calendar;
232     }
233     
234     /**
235      * Build a <code>{@link java.util.Calendar}</code> with the current time.
236      * The new Calendar will use the <code>BaseCalendar</code> time zone if
237      * it is not <code>null</code>.
238      */

239     protected java.util.Calendar JavaDoc createJavaCalendar() {
240         return
241             (getTimeZone() == null) ?
242                 java.util.Calendar.getInstance() :
243                 java.util.Calendar.getInstance(getTimeZone());
244     }
245
246     /**
247      * Returns the start of the given day as a <code>{@link java.util.Calendar}</code>.
248      * This calculation will take the <code>BaseCalendar</code>
249      * time zone into account if it is not <code>null</code>.
250      *
251      * @param timeInMillis A time containing the desired date for the
252      * start-of-day time
253      * @return A <code>{@link java.util.Calendar}</code> set to the start of
254      * the given day.
255      */

256     protected java.util.Calendar JavaDoc getStartOfDayJavaCalendar(long timeInMillis) {
257         java.util.Calendar JavaDoc startOfDay = createJavaCalendar(timeInMillis);
258         startOfDay.set(java.util.Calendar.HOUR_OF_DAY, 0);
259         startOfDay.set(java.util.Calendar.MINUTE, 0);
260         startOfDay.set(java.util.Calendar.SECOND, 0);
261         startOfDay.set(java.util.Calendar.MILLISECOND, 0);
262         return startOfDay;
263     }
264     
265     /**
266      * Returns the end of the given day <code>{@link java.util.Calendar}</code>.
267      * This calculation will take the <code>BaseCalendar</code>
268      * time zone into account if it is not <code>null</code>.
269      *
270      * @param timeInMillis a time containing the desired date for the
271      * end-of-day time.
272      * @return A <code>{@link java.util.Calendar}</code> set to the end of
273      * the given day.
274      */

275     protected java.util.Calendar JavaDoc getEndOfDayJavaCalendar(long timeInMillis) {
276         java.util.Calendar JavaDoc endOfDay = createJavaCalendar(timeInMillis);
277         endOfDay.set(java.util.Calendar.HOUR_OF_DAY, 23);
278         endOfDay.set(java.util.Calendar.MINUTE, 59);
279         endOfDay.set(java.util.Calendar.SECOND, 59);
280         endOfDay.set(java.util.Calendar.MILLISECOND, 999);
281         return endOfDay;
282     }
283 }
284
Popular Tags