KickJava   Java API By Example, From Geeks To Geeks.

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


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.TimeZone JavaDoc;
27
28 import org.quartz.Calendar;
29
30 /**
31  * <p>
32  * This implementation of the Calendar excludes a set of days of the week. You
33  * may use it to exclude weekends for example. But you may define any day of
34  * the week. By default it excludes SATURDAY and SUNDAY.
35  * </p>
36  *
37  * @see org.quartz.Calendar
38  * @see org.quartz.impl.calendar.BaseCalendar
39  *
40  * @author Juergen Donnerstag
41  */

42 public class WeeklyCalendar extends BaseCalendar implements Calendar,
43         Serializable JavaDoc {
44     static final long serialVersionUID = -6809298821229007586L;
45     
46     // An array to store the week days which are to be excluded.
47
// java.util.Calendar.MONDAY etc. are used as index.
48
private boolean[] excludeDays = new boolean[8];
49
50     // Will be set to true, if all week days are excluded
51
private boolean excludeAll = false;
52
53     public WeeklyCalendar() {
54         this(null, null);
55     }
56
57     public WeeklyCalendar(Calendar baseCalendar) {
58         this(baseCalendar, null);
59     }
60
61     public WeeklyCalendar(TimeZone JavaDoc timeZone) {
62         super(null, timeZone);
63     }
64     
65     public WeeklyCalendar(Calendar baseCalendar, TimeZone JavaDoc timeZone) {
66         super(baseCalendar, timeZone);
67
68         excludeDays[java.util.Calendar.SUNDAY] = true;
69         excludeDays[java.util.Calendar.SATURDAY] = true;
70         excludeAll = areAllDaysExcluded();
71     }
72
73     /**
74      * <p>
75      * Get the array with the week days
76      * </p>
77      */

78     public boolean[] getDaysExcluded() {
79         return excludeDays;
80     }
81
82     /**
83      * <p>
84      * Return true, if wday (see Calendar.get()) is defined to be exluded. E. g.
85      * saturday and sunday.
86      * </p>
87      */

88     public boolean isDayExcluded(int wday) {
89         return excludeDays[wday];
90     }
91
92     /**
93      * <p>
94      * Redefine the array of days excluded. The array must of size greater or
95      * equal 8. java.util.Calendar's constants like MONDAY should be used as
96      * index. A value of true is regarded as: exclude it.
97      * </p>
98      */

99     public void setDaysExcluded(boolean[] weekDays) {
100         if (weekDays == null) {
101             return;
102         }
103
104         excludeDays = weekDays;
105         excludeAll = areAllDaysExcluded();
106     }
107
108     /**
109      * <p>
110      * Redefine a certain day of the week to be excluded (true) or included
111      * (false). Use java.util.Calendar's constants like MONDAY to determine the
112      * wday.
113      * </p>
114      */

115     public void setDayExcluded(int wday, boolean exclude) {
116         excludeDays[wday] = exclude;
117         excludeAll = areAllDaysExcluded();
118     }
119
120     /**
121      * <p>
122      * Check if all week days are excluded. That is no day is included.
123      * </p>
124      *
125      * @return boolean
126      */

127     public boolean areAllDaysExcluded() {
128         return
129             isDayExcluded(java.util.Calendar.SUNDAY) &&
130             isDayExcluded(java.util.Calendar.MONDAY) &&
131             isDayExcluded(java.util.Calendar.TUESDAY) &&
132             isDayExcluded(java.util.Calendar.WEDNESDAY) &&
133             isDayExcluded(java.util.Calendar.THURSDAY) &&
134             isDayExcluded(java.util.Calendar.FRIDAY) &&
135             isDayExcluded(java.util.Calendar.SATURDAY);
136     }
137
138     /**
139      * <p>
140      * Determine whether the given time (in milliseconds) is 'included' by the
141      * Calendar.
142      * </p>
143      *
144      * <p>
145      * Note that this Calendar is only has full-day precision.
146      * </p>
147      */

148     public boolean isTimeIncluded(long timeStamp) {
149         if (excludeAll == true) {
150             return false;
151         }
152
153         // Test the base calendar first. Only if the base calendar not already
154
// excludes the time/date, continue evaluating this calendar instance.
155
if (super.isTimeIncluded(timeStamp) == false) { return false; }
156
157         java.util.Calendar JavaDoc cl = createJavaCalendar(timeStamp);
158         int wday = cl.get(java.util.Calendar.DAY_OF_WEEK);
159
160         return !(isDayExcluded(wday));
161     }
162
163     /**
164      * <p>
165      * Determine the next time (in milliseconds) that is 'included' by the
166      * Calendar after the given time. Return the original value if timeStamp is
167      * included. Return 0 if all days are excluded.
168      * </p>
169      *
170      * <p>
171      * Note that this Calendar is only has full-day precision.
172      * </p>
173      */

174     public long getNextIncludedTime(long timeStamp) {
175         if (excludeAll == true) {
176             return 0;
177         }
178
179         // Call base calendar implementation first
180
long baseTime = super.getNextIncludedTime(timeStamp);
181         if ((baseTime > 0) && (baseTime > timeStamp)) {
182             timeStamp = baseTime;
183         }
184
185         // Get timestamp for 00:00:00
186
java.util.Calendar JavaDoc cl = getStartOfDayJavaCalendar(timeStamp);
187         int wday = cl.get(java.util.Calendar.DAY_OF_WEEK);
188
189         if (!isDayExcluded(wday)) {
190             return timeStamp; // return the original value
191
}
192
193         while (isDayExcluded(wday) == true) {
194             cl.add(java.util.Calendar.DATE, 1);
195             wday = cl.get(java.util.Calendar.DAY_OF_WEEK);
196         }
197
198         return cl.getTime().getTime();
199     }
200 }
201
Popular Tags