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 week. You
033: * may use it to exclude weekends for example. But you may define any day of
034: * the week. By default it excludes SATURDAY and SUNDAY.
035: * </p>
036: *
037: * @see org.quartz.Calendar
038: * @see org.quartz.impl.calendar.BaseCalendar
039: *
040: * @author Juergen Donnerstag
041: */
042: public class WeeklyCalendar extends BaseCalendar implements Calendar,
043: Serializable {
044: static final long serialVersionUID = -6809298821229007586L;
045:
046: // An array to store the week days which are to be excluded.
047: // java.util.Calendar.MONDAY etc. are used as index.
048: private boolean[] excludeDays = new boolean[8];
049:
050: // Will be set to true, if all week days are excluded
051: private boolean excludeAll = false;
052:
053: public WeeklyCalendar() {
054: this (null, null);
055: }
056:
057: public WeeklyCalendar(Calendar baseCalendar) {
058: this (baseCalendar, null);
059: }
060:
061: public WeeklyCalendar(TimeZone timeZone) {
062: super (null, timeZone);
063: }
064:
065: public WeeklyCalendar(Calendar baseCalendar, TimeZone timeZone) {
066: super (baseCalendar, timeZone);
067:
068: excludeDays[java.util.Calendar.SUNDAY] = true;
069: excludeDays[java.util.Calendar.SATURDAY] = true;
070: excludeAll = areAllDaysExcluded();
071: }
072:
073: /**
074: * <p>
075: * Get the array with the week days
076: * </p>
077: */
078: public boolean[] getDaysExcluded() {
079: return excludeDays;
080: }
081:
082: /**
083: * <p>
084: * Return true, if wday (see Calendar.get()) is defined to be exluded. E. g.
085: * saturday and sunday.
086: * </p>
087: */
088: public boolean isDayExcluded(int wday) {
089: return excludeDays[wday];
090: }
091:
092: /**
093: * <p>
094: * Redefine the array of days excluded. The array must of size greater or
095: * equal 8. java.util.Calendar's constants like MONDAY should be used as
096: * index. A value of true is regarded as: exclude it.
097: * </p>
098: */
099: 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 isDayExcluded(java.util.Calendar.SUNDAY)
129: && isDayExcluded(java.util.Calendar.MONDAY)
130: && isDayExcluded(java.util.Calendar.TUESDAY)
131: && isDayExcluded(java.util.Calendar.WEDNESDAY)
132: && isDayExcluded(java.util.Calendar.THURSDAY)
133: && isDayExcluded(java.util.Calendar.FRIDAY)
134: && isDayExcluded(java.util.Calendar.SATURDAY);
135: }
136:
137: /**
138: * <p>
139: * Determine whether the given time (in milliseconds) is 'included' by the
140: * Calendar.
141: * </p>
142: *
143: * <p>
144: * Note that this Calendar is only has full-day precision.
145: * </p>
146: */
147: public boolean isTimeIncluded(long timeStamp) {
148: if (excludeAll == true) {
149: return false;
150: }
151:
152: // Test the base calendar first. Only if the base calendar not already
153: // excludes the time/date, continue evaluating this calendar instance.
154: if (super .isTimeIncluded(timeStamp) == false) {
155: return false;
156: }
157:
158: java.util.Calendar cl = createJavaCalendar(timeStamp);
159: int wday = cl.get(java.util.Calendar.DAY_OF_WEEK);
160:
161: return !(isDayExcluded(wday));
162: }
163:
164: /**
165: * <p>
166: * Determine the next time (in milliseconds) that is 'included' by the
167: * Calendar after the given time. Return the original value if timeStamp is
168: * included. Return 0 if all days are excluded.
169: * </p>
170: *
171: * <p>
172: * Note that this Calendar is only has full-day precision.
173: * </p>
174: */
175: public long getNextIncludedTime(long timeStamp) {
176: if (excludeAll == true) {
177: return 0;
178: }
179:
180: // Call base calendar implementation first
181: long baseTime = super .getNextIncludedTime(timeStamp);
182: if ((baseTime > 0) && (baseTime > timeStamp)) {
183: timeStamp = baseTime;
184: }
185:
186: // Get timestamp for 00:00:00
187: java.util.Calendar cl = getStartOfDayJavaCalendar(timeStamp);
188: int wday = cl.get(java.util.Calendar.DAY_OF_WEEK);
189:
190: if (!isDayExcluded(wday)) {
191: return timeStamp; // return the original value
192: }
193:
194: while (isDayExcluded(wday) == true) {
195: cl.add(java.util.Calendar.DATE, 1);
196: wday = cl.get(java.util.Calendar.DAY_OF_WEEK);
197: }
198:
199: return cl.getTime().getTime();
200: }
201: }
|