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: */
021: package org.quartz.impl.calendar;
022:
023: import java.io.Serializable;
024: import java.util.Collections;
025: import java.util.Date;
026: import java.util.SortedSet;
027: import java.util.TimeZone;
028: import java.util.TreeSet;
029:
030: import org.quartz.Calendar;
031:
032: /**
033: * <p>
034: * This implementation of the Calendar stores a list of holidays (full days
035: * that are excluded from scheduling).
036: * </p>
037: *
038: * <p>
039: * The implementation DOES take the year into consideration, so if you want to
040: * exclude July 4th for the next 10 years, you need to add 10 entries to the
041: * exclude list.
042: * </p>
043: *
044: * @author Sharada Jambula
045: * @author Juergen Donnerstag
046: */
047: public class HolidayCalendar extends BaseCalendar implements Calendar,
048: Serializable {
049: static final long serialVersionUID = -7590908752291814693L;
050:
051: // A sorted set to store the holidays
052: private TreeSet dates = new TreeSet();
053:
054: public HolidayCalendar() {
055: }
056:
057: public HolidayCalendar(Calendar baseCalendar) {
058: super (baseCalendar);
059: }
060:
061: public HolidayCalendar(TimeZone timeZone) {
062: super (timeZone);
063: }
064:
065: public HolidayCalendar(Calendar baseCalendar, TimeZone timeZone) {
066: super (baseCalendar, timeZone);
067: }
068:
069: /**
070: * <p>
071: * Determine whether the given time (in milliseconds) is 'included' by the
072: * Calendar.
073: * </p>
074: *
075: * <p>
076: * Note that this Calendar is only has full-day precision.
077: * </p>
078: */
079: public boolean isTimeIncluded(long timeStamp) {
080: if (super .isTimeIncluded(timeStamp) == false) {
081: return false;
082: }
083:
084: Date lookFor = getStartOfDayJavaCalendar(timeStamp).getTime();
085:
086: return !(dates.contains(lookFor));
087: }
088:
089: /**
090: * <p>
091: * Determine the next time (in milliseconds) that is 'included' by the
092: * Calendar after the given time.
093: * </p>
094: *
095: * <p>
096: * Note that this Calendar is only has full-day precision.
097: * </p>
098: */
099: public long getNextIncludedTime(long timeStamp) {
100:
101: // Call base calendar implementation first
102: long baseTime = super .getNextIncludedTime(timeStamp);
103: if ((baseTime > 0) && (baseTime > timeStamp)) {
104: timeStamp = baseTime;
105: }
106:
107: // Get timestamp for 00:00:00
108: java.util.Calendar day = getStartOfDayJavaCalendar(timeStamp);
109: while (isTimeIncluded(day.getTime().getTime()) == false) {
110: day.add(java.util.Calendar.DATE, 1);
111: }
112:
113: return day.getTime().getTime();
114: }
115:
116: /**
117: * <p>
118: * Add the given Date to the list of excluded days. Only the month, day and
119: * year of the returned dates are significant.
120: * </p>
121: */
122: public void addExcludedDate(Date excludedDate) {
123: Date date = getStartOfDayJavaCalendar(excludedDate.getTime())
124: .getTime();
125: /*
126: * System.err.println( "HolidayCalendar.add(): date=" +
127: * excludedDate.toLocaleString());
128: */
129: this .dates.add(date);
130: }
131:
132: public void removeExcludedDate(Date dateToRemove) {
133: Date date = getStartOfDayJavaCalendar(dateToRemove.getTime())
134: .getTime();
135: dates.remove(date);
136: }
137:
138: /**
139: * <p>
140: * Returns a <code>SortedSet</code> of Dates representing the excluded
141: * days. Only the month, day and year of the returned dates are
142: * significant.
143: * </p>
144: */
145: public SortedSet getExcludedDates() {
146: return Collections.unmodifiableSortedSet(dates);
147: }
148: }
|