KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

21 package org.quartz.impl.calendar;
22
23 import java.io.Serializable JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.SortedSet JavaDoc;
27 import java.util.TimeZone JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 import org.quartz.Calendar;
31
32 /**
33  * <p>
34  * This implementation of the Calendar stores a list of holidays (full days
35  * that are excluded from scheduling).
36  * </p>
37  *
38  * <p>
39  * The implementation DOES take the year into consideration, so if you want to
40  * exclude July 4th for the next 10 years, you need to add 10 entries to the
41  * exclude list.
42  * </p>
43  *
44  * @author Sharada Jambula
45  * @author Juergen Donnerstag
46  */

47 public class HolidayCalendar extends BaseCalendar implements Calendar,
48         Serializable JavaDoc {
49     static final long serialVersionUID = -7590908752291814693L;
50     
51     // A sorted set to store the holidays
52
private TreeSet JavaDoc dates = new TreeSet JavaDoc();
53
54     public HolidayCalendar() {
55     }
56
57     public HolidayCalendar(Calendar baseCalendar) {
58         super(baseCalendar);
59     }
60
61     public HolidayCalendar(TimeZone JavaDoc timeZone) {
62         super(timeZone);
63     }
64
65     public HolidayCalendar(Calendar baseCalendar, TimeZone JavaDoc timeZone) {
66         super(baseCalendar, timeZone);
67     }
68
69     /**
70      * <p>
71      * Determine whether the given time (in milliseconds) is 'included' by the
72      * Calendar.
73      * </p>
74      *
75      * <p>
76      * Note that this Calendar is only has full-day precision.
77      * </p>
78      */

79     public boolean isTimeIncluded(long timeStamp) {
80         if (super.isTimeIncluded(timeStamp) == false) {
81             return false;
82         }
83
84         Date JavaDoc lookFor = getStartOfDayJavaCalendar(timeStamp).getTime();
85
86         return !(dates.contains(lookFor));
87     }
88
89     /**
90      * <p>
91      * Determine the next time (in milliseconds) that is 'included' by the
92      * Calendar after the given time.
93      * </p>
94      *
95      * <p>
96      * Note that this Calendar is only has full-day precision.
97      * </p>
98      */

99     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 JavaDoc 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 JavaDoc excludedDate) {
123         Date JavaDoc date = getStartOfDayJavaCalendar(excludedDate.getTime()).getTime();
124         /*
125          * System.err.println( "HolidayCalendar.add(): date=" +
126          * excludedDate.toLocaleString());
127          */

128         this.dates.add(date);
129     }
130
131     public void removeExcludedDate(Date JavaDoc dateToRemove) {
132         Date JavaDoc date = getStartOfDayJavaCalendar(dateToRemove.getTime()).getTime();
133         dates.remove(date);
134     }
135
136     /**
137      * <p>
138      * Returns a <code>SortedSet</code> of Dates representing the excluded
139      * days. Only the month, day and year of the returned dates are
140      * significant.
141      * </p>
142      */

143     public SortedSet JavaDoc getExcludedDates() {
144         return Collections.unmodifiableSortedSet(dates);
145     }
146 }
147
Popular Tags