KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.TimeZone JavaDoc;
31
32 import org.quartz.Calendar;
33
34 /**
35  * <p>
36  * This implementation of the Calendar excludes a set of days of the year. You
37  * may use it to exclude bank holidays which are on the same date every year.
38  * </p>
39  *
40  * @see org.quartz.Calendar
41  * @see org.quartz.impl.calendar.BaseCalendar
42  *
43  * @author Juergen Donnerstag
44  */

45 public class AnnualCalendar extends BaseCalendar implements Calendar,
46         Serializable JavaDoc {
47
48     static final long serialVersionUID = 7346867105876610961L;
49
50     private ArrayList JavaDoc excludeDays = new ArrayList JavaDoc();
51
52     // true, if excludeDays is sorted
53
private boolean dataSorted = false;
54
55     public AnnualCalendar() {
56     }
57
58     public AnnualCalendar(Calendar baseCalendar) {
59         super(baseCalendar);
60     }
61
62     public AnnualCalendar(TimeZone JavaDoc timeZone) {
63         super(timeZone);
64     }
65
66     public AnnualCalendar(Calendar baseCalendar, TimeZone JavaDoc timeZone) {
67         super(baseCalendar, timeZone);
68     }
69
70     /**
71      * <p>
72      * Get the array which defines the exclude-value of each day of month
73      * </p>
74      */

75     public ArrayList JavaDoc getDaysExcluded() {
76         return excludeDays;
77     }
78
79     /**
80      * <p>
81      * Return true, if day is defined to be exluded.
82      * </p>
83      */

84     public boolean isDayExcluded(java.util.Calendar JavaDoc day) {
85         if (day == null) {
86             throw new IllegalArgumentException JavaDoc(
87                     "Parameter day must not be null");
88         }
89
90         int dmonth = day.get(java.util.Calendar.MONTH);
91         int dday = day.get(java.util.Calendar.DAY_OF_MONTH);
92
93         if (dataSorted == false) {
94             Collections.sort(excludeDays, new CalendarComparator());
95             dataSorted = true;
96         }
97
98         Iterator JavaDoc iter = excludeDays.iterator();
99         while (iter.hasNext()) {
100             java.util.Calendar JavaDoc cl = (java.util.Calendar JavaDoc) iter.next();
101
102             // remember, the list is sorted
103
if (dmonth < cl.get(java.util.Calendar.MONTH)) {
104                 return false;
105             }
106
107             if (dday != cl.get(java.util.Calendar.DAY_OF_MONTH)) {
108                 continue;
109             }
110
111             if (dmonth != cl.get(java.util.Calendar.MONTH)) {
112                 continue;
113             }
114
115             return true;
116         }
117
118         return false;
119     }
120
121     /**
122      * <p>
123      * Redefine the array of days excluded. The array must of size greater or
124      * equal 31.
125      * </p>
126      */

127     public void setDaysExcluded(ArrayList JavaDoc days) {
128         if (days == null) {
129             excludeDays = new ArrayList JavaDoc();
130         }
131
132         excludeDays = days;
133         dataSorted = false;
134     }
135
136     /**
137      * <p>
138      * Redefine a certain day to be excluded (true) or included (false).
139      * </p>
140      */

141     public void setDayExcluded(java.util.Calendar JavaDoc day, boolean exclude) {
142         if (isDayExcluded(day)) {
143             return;
144         }
145
146         excludeDays.add(day);
147         dataSorted = false;
148     }
149
150     /**
151      * <p>
152      * Determine whether the given time (in milliseconds) is 'included' by the
153      * Calendar.
154      * </p>
155      *
156      * <p>
157      * Note that this Calendar is only has full-day precision.
158      * </p>
159      */

160     public boolean isTimeIncluded(long timeStamp) {
161         // Test the base calendar first. Only if the base calendar not already
162
// excludes the time/date, continue evaluating this calendar instance.
163
if (super.isTimeIncluded(timeStamp) == false) { return false; }
164
165         java.util.Calendar JavaDoc day = createJavaCalendar(timeStamp);
166
167         return !(isDayExcluded(day));
168     }
169
170     /**
171      * <p>
172      * Determine the next time (in milliseconds) that is 'included' by the
173      * Calendar after the given time. Return the original value if timeStamp is
174      * included. Return 0 if all days are excluded.
175      * </p>
176      *
177      * <p>
178      * Note that this Calendar is only has full-day precision.
179      * </p>
180      */

181     public long getNextIncludedTime(long timeStamp) {
182         // Call base calendar implementation first
183
long baseTime = super.getNextIncludedTime(timeStamp);
184         if ((baseTime > 0) && (baseTime > timeStamp)) {
185             timeStamp = baseTime;
186         }
187
188         // Get timestamp for 00:00:00
189
java.util.Calendar JavaDoc day = getStartOfDayJavaCalendar(timeStamp);
190         if (isDayExcluded(day) == false) {
191             return timeStamp; // return the original value
192
}
193
194         while (isDayExcluded(day) == true) {
195             day.add(java.util.Calendar.DATE, 1);
196         }
197
198         return day.getTime().getTime();
199     }
200 }
201
202 class CalendarComparator implements Comparator JavaDoc {
203     public CalendarComparator() {
204     }
205
206     /**
207      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
208      */

209     public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
210         java.util.Calendar JavaDoc c1 = (java.util.Calendar JavaDoc) arg0;
211         java.util.Calendar JavaDoc c2 = (java.util.Calendar JavaDoc) arg1;
212         
213         if(c1.before(c2)) {
214             return -1;
215         } else if(c1.after(c2)) {
216             return 1;
217         } else {
218             return 0;
219         }
220     }
221 }
222
Popular Tags