01: package org.obe.engine.calendar;
02:
03: import java.util.BitSet;
04: import java.util.Calendar;
05: import java.util.GregorianCalendar;
06: import java.util.TimeZone;
07:
08: /**
09: * Represents a single day of the week or a range of days of the week. The
10: * values defined in <code>java.util.Calendar</code> as definitions for each day.
11: * <p/>
12: * Examples:
13: * <pre>
14: * Exclude Saturday;
15: * Exclude Sunday;
16: * </pre>
17: *
18: * @see CalendarRule
19: */
20: public class DayRule extends CalendarRule {
21: // private static final long serialVersionUID = 1449572381790897741L;
22:
23: // Denotes the start of this rule
24: private int _startDay;
25:
26: // Denotes the end of this rule. If it is equal to _startDay, then this
27: // rule is for a single day
28: private int _endDay;
29:
30: public DayRule() {
31: }
32:
33: // public DayRule(int startDay) {
34: // _startDay = startDay;
35: // _endDay = startDay;
36: // }
37: //
38: // public DayRule(int startDay, int endDay) {
39: // _startDay = startDay;
40: // _endDay = endDay;
41: // }
42:
43: public int getStartDay() {
44: return _startDay;
45: }
46:
47: public void setStartDay(int startDay) {
48: _startDay = startDay;
49: }
50:
51: public int getEndDay() {
52: return _endDay;
53: }
54:
55: public void setEndDay(int endDay) {
56: _endDay = endDay;
57: }
58:
59: public void applyRule(BitSet mask, int length,
60: GregorianCalendar startDate, GregorianCalendar endDate,
61: TimeZone tz) {
62:
63: int dayCounter = startDate.get(Calendar.DAY_OF_WEEK);
64: GregorianCalendar start = (GregorianCalendar) startDate.clone();
65: int interval = BusinessCalendarUtilities
66: .getIntervalsInDate(start);
67:
68: boolean inRange = false;
69: for (int i = 0; i < length; i += interval) {
70: if (dayCounter == _startDay)
71: inRange = true;
72:
73: if (inRange || _startDay == _endDay)
74: BusinessCalendarUtilities.setBitSetBits(mask, i,
75: interval);
76:
77: if (dayCounter == _endDay)
78: inRange = false;
79:
80: // Wrap around the week
81: if (++dayCounter > 7)
82: dayCounter = 1;
83: start.add(Calendar.DATE, 1);
84: interval = BusinessCalendarUtilities
85: .getIntervalsInDate(start);
86: }
87: }
88:
89: public String toString() {
90: return "DayRule[" + "_startDay=" + _startDay + ", _endDay="
91: + _endDay + ']';
92: }
93: }
|