001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: * and Juergen Donnerstag (c) 2002, EDS 2002
021: */
022:
023: package org.quartz.impl.calendar;
024:
025: import java.io.Serializable;
026: import java.util.TimeZone;
027:
028: import org.quartz.Calendar;
029:
030: /**
031: * <p>
032: * This implementation of the Calendar excludes a set of days of the month. You
033: * may use it to exclude every 1. of each month for example. But you may define
034: * any day of a month.
035: * </p>
036: *
037: * @see org.quartz.Calendar
038: * @see org.quartz.impl.calendar.BaseCalendar
039: *
040: * @author Juergen Donnerstag
041: */
042: public class MonthlyCalendar extends BaseCalendar implements Calendar,
043: Serializable {
044:
045: static final long serialVersionUID = 419164961091807944L;
046:
047: private static final int MAX_DAYS_IN_MONTH = 31;
048:
049: // An array to store a months days which are to be excluded.
050: // java.util.Calendar.get( ) as index.
051: private boolean[] excludeDays = new boolean[MAX_DAYS_IN_MONTH];
052:
053: // Will be set to true, if all week days are excluded
054: private boolean excludeAll = false;
055:
056: public MonthlyCalendar() {
057: this (null, null);
058: }
059:
060: public MonthlyCalendar(Calendar baseCalendar) {
061: this (baseCalendar, null);
062: }
063:
064: public MonthlyCalendar(TimeZone timeZone) {
065: this (null, timeZone);
066: }
067:
068: public MonthlyCalendar(Calendar baseCalendar, TimeZone timeZone) {
069: super (baseCalendar, timeZone);
070:
071: // all days are included by default
072: excludeAll = areAllDaysExcluded();
073: }
074:
075: /**
076: * <p>
077: * Get the array which defines the exclude-value of each day of month.
078: * Only the first 31 elements of the array are relevant, with the 0 index
079: * element representing the first day of the month.
080: * </p>
081: */
082: public boolean[] getDaysExcluded() {
083: return excludeDays;
084: }
085:
086: /**
087: * <p>
088: * Return true, if day is defined to be excluded.
089: * </p>
090: *
091: * @param day The day of the month (from 1 to 31) to check.
092: */
093: public boolean isDayExcluded(int day) {
094: if ((day < 1) || (day > MAX_DAYS_IN_MONTH)) {
095: throw new IllegalArgumentException(
096: "The day parameter must be in the range of 1 to "
097: + MAX_DAYS_IN_MONTH);
098: }
099:
100: return excludeDays[day - 1];
101: }
102:
103: /**
104: * <p>
105: * Redefine the array of days excluded. The array must non-null and of size
106: * greater or equal to 31. The 0 index element represents the first day of
107: * the month.
108: * </p>
109: */
110: public void setDaysExcluded(boolean[] days) {
111: if (days == null) {
112: throw new IllegalArgumentException(
113: "The days parameter cannot be null.");
114: }
115:
116: if (days.length < MAX_DAYS_IN_MONTH) {
117: throw new IllegalArgumentException(
118: "The days parameter must have a length of at least "
119: + MAX_DAYS_IN_MONTH + " elements.");
120: }
121:
122: excludeDays = days;
123: excludeAll = areAllDaysExcluded();
124: }
125:
126: /**
127: * <p>
128: * Redefine a certain day of the month to be excluded (true) or included
129: * (false).
130: * </p>
131: *
132: * @param day The day of the month (from 1 to 31) to set.
133: */
134: public void setDayExcluded(int day, boolean exclude) {
135: if ((day < 1) || (day > MAX_DAYS_IN_MONTH)) {
136: throw new IllegalArgumentException(
137: "The day parameter must be in the range of 1 to "
138: + MAX_DAYS_IN_MONTH);
139: }
140:
141: excludeDays[day - 1] = exclude;
142: excludeAll = areAllDaysExcluded();
143: }
144:
145: /**
146: * <p>
147: * Check if all days are excluded. That is no day is included.
148: * </p>
149: */
150: public boolean areAllDaysExcluded() {
151: for (int i = 1; i <= MAX_DAYS_IN_MONTH; i++) {
152: if (isDayExcluded(i) == false) {
153: return false;
154: }
155: }
156:
157: return true;
158: }
159:
160: /**
161: * <p>
162: * Determine whether the given time (in milliseconds) is 'included' by the
163: * Calendar.
164: * </p>
165: *
166: * <p>
167: * Note that this Calendar is only has full-day precision.
168: * </p>
169: */
170: public boolean isTimeIncluded(long timeStamp) {
171: if (excludeAll == true) {
172: return false;
173: }
174:
175: // Test the base calendar first. Only if the base calendar not already
176: // excludes the time/date, continue evaluating this calendar instance.
177: if (super .isTimeIncluded(timeStamp) == false) {
178: return false;
179: }
180:
181: java.util.Calendar cl = createJavaCalendar(timeStamp);
182: int day = cl.get(java.util.Calendar.DAY_OF_MONTH);
183:
184: return !(isDayExcluded(day));
185: }
186:
187: /**
188: * <p>
189: * Determine the next time (in milliseconds) that is 'included' by the
190: * Calendar after the given time. Return the original value if timeStamp is
191: * included. Return 0 if all days are excluded.
192: * </p>
193: *
194: * <p>
195: * Note that this Calendar is only has full-day precision.
196: * </p>
197: */
198: public long getNextIncludedTime(long timeStamp) {
199: if (excludeAll == true) {
200: return 0;
201: }
202:
203: // Call base calendar implementation first
204: long baseTime = super .getNextIncludedTime(timeStamp);
205: if ((baseTime > 0) && (baseTime > timeStamp)) {
206: timeStamp = baseTime;
207: }
208:
209: // Get timestamp for 00:00:00
210: java.util.Calendar cl = getStartOfDayJavaCalendar(timeStamp);
211: int day = cl.get(java.util.Calendar.DAY_OF_MONTH);
212:
213: if (!isDayExcluded(day)) {
214: return timeStamp; // return the original value
215: }
216:
217: while (isDayExcluded(day) == true) {
218: cl.add(java.util.Calendar.DATE, 1);
219: day = cl.get(java.util.Calendar.DAY_OF_WEEK);
220: }
221:
222: return cl.getTime().getTime();
223: }
224: }
|