KickJava   Java API By Example, From Geeks To Geeks.

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


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 month. You
33  * may use it to exclude every 1. of each month for example. But you may define
34  * any day of a month.
35  * </p>
36  *
37  * @see org.quartz.Calendar
38  * @see org.quartz.impl.calendar.BaseCalendar
39  *
40  * @author Juergen Donnerstag
41  */

42 public class MonthlyCalendar extends BaseCalendar implements Calendar,
43         Serializable JavaDoc {
44
45     static final long serialVersionUID = 419164961091807944L;
46
47     private static final int MAX_DAYS_IN_MONTH = 31;
48
49     // An array to store a months days which are to be excluded.
50
// java.util.Calendar.get( ) as index.
51
private boolean[] excludeDays = new boolean[MAX_DAYS_IN_MONTH];
52
53     // Will be set to true, if all week days are excluded
54
private boolean excludeAll = false;
55
56     public MonthlyCalendar() {
57         this(null, null);
58     }
59
60     public MonthlyCalendar(Calendar baseCalendar) {
61         this(baseCalendar, null);
62     }
63
64     public MonthlyCalendar(TimeZone JavaDoc timeZone) {
65         this(null, timeZone);
66     }
67
68     public MonthlyCalendar(Calendar baseCalendar, TimeZone JavaDoc timeZone) {
69         super(baseCalendar, timeZone);
70         
71         // all days are included by default
72
excludeAll = areAllDaysExcluded();
73     }
74
75     /**
76      * <p>
77      * Get the array which defines the exclude-value of each day of month.
78      * Only the first 31 elements of the array are relevant, with the 0 index
79      * element representing the first day of the month.
80      * </p>
81      */

82     public boolean[] getDaysExcluded() {
83         return excludeDays;
84     }
85
86     /**
87      * <p>
88      * Return true, if day is defined to be excluded.
89      * </p>
90      *
91      * @param day The day of the month (from 1 to 31) to check.
92      */

93     public boolean isDayExcluded(int day) {
94         if ((day < 1) || (day > MAX_DAYS_IN_MONTH)) {
95             throw new IllegalArgumentException JavaDoc(
96                 "The day parameter must be in the range of 1 to " + MAX_DAYS_IN_MONTH);
97         }
98         
99         return excludeDays[day - 1];
100     }
101
102     /**
103      * <p>
104      * Redefine the array of days excluded. The array must non-null and of size
105      * greater or equal to 31. The 0 index element represents the first day of
106      * the month.
107      * </p>
108      */

109     public void setDaysExcluded(boolean[] days) {
110         if (days == null) {
111             throw new IllegalArgumentException JavaDoc("The days parameter cannot be null.");
112         }
113
114         if (days.length < MAX_DAYS_IN_MONTH) {
115             throw new IllegalArgumentException JavaDoc(
116                 "The days parameter must have a length of at least " + MAX_DAYS_IN_MONTH + " elements.");
117         }
118
119         excludeDays = days;
120         excludeAll = areAllDaysExcluded();
121     }
122
123     /**
124      * <p>
125      * Redefine a certain day of the month to be excluded (true) or included
126      * (false).
127      * </p>
128      *
129      * @param day The day of the month (from 1 to 31) to set.
130      */

131     public void setDayExcluded(int day, boolean exclude) {
132         if ((day < 1) || (day > MAX_DAYS_IN_MONTH)) {
133             throw new IllegalArgumentException JavaDoc(
134                 "The day parameter must be in the range of 1 to " + MAX_DAYS_IN_MONTH);
135         }
136         
137         excludeDays[day - 1] = exclude;
138         excludeAll = areAllDaysExcluded();
139     }
140
141     /**
142      * <p>
143      * Check if all days are excluded. That is no day is included.
144      * </p>
145      */

146     public boolean areAllDaysExcluded() {
147         for (int i = 1; i <= MAX_DAYS_IN_MONTH; i++) {
148             if (isDayExcluded(i) == false) {
149                 return false;
150             }
151         }
152
153         return true;
154     }
155
156     /**
157      * <p>
158      * Determine whether the given time (in milliseconds) is 'included' by the
159      * Calendar.
160      * </p>
161      *
162      * <p>
163      * Note that this Calendar is only has full-day precision.
164      * </p>
165      */

166     public boolean isTimeIncluded(long timeStamp) {
167         if (excludeAll == true) {
168             return false;
169         }
170
171         // Test the base calendar first. Only if the base calendar not already
172
// excludes the time/date, continue evaluating this calendar instance.
173
if (super.isTimeIncluded(timeStamp) == false) { return false; }
174
175         java.util.Calendar JavaDoc cl = createJavaCalendar(timeStamp);
176         int day = cl.get(java.util.Calendar.DAY_OF_MONTH);
177
178         return !(isDayExcluded(day));
179     }
180
181     /**
182      * <p>
183      * Determine the next time (in milliseconds) that is 'included' by the
184      * Calendar after the given time. Return the original value if timeStamp is
185      * included. Return 0 if all days are excluded.
186      * </p>
187      *
188      * <p>
189      * Note that this Calendar is only has full-day precision.
190      * </p>
191      */

192     public long getNextIncludedTime(long timeStamp) {
193         if (excludeAll == true) {
194             return 0;
195         }
196
197         // Call base calendar implementation first
198
long baseTime = super.getNextIncludedTime(timeStamp);
199         if ((baseTime > 0) && (baseTime > timeStamp)) {
200             timeStamp = baseTime;
201         }
202
203         // Get timestamp for 00:00:00
204
java.util.Calendar JavaDoc cl = getStartOfDayJavaCalendar(timeStamp);
205         int day = cl.get(java.util.Calendar.DAY_OF_MONTH);
206
207         if (!isDayExcluded(day)) {
208             return timeStamp; // return the original value
209
}
210
211         while (isDayExcluded(day) == true) {
212             cl.add(java.util.Calendar.DATE, 1);
213             day = cl.get(java.util.Calendar.DAY_OF_WEEK);
214         }
215
216         return cl.getTime().getTime();
217     }
218 }
219
Popular Tags