001: package org.obe.engine.calendar;
002:
003: import java.util.BitSet;
004: import java.util.Calendar;
005: import java.util.GregorianCalendar;
006: import java.util.TimeZone;
007:
008: /**
009: * A rule that includes or excludes an interval of time within a single day.
010: * Hours are expressed in the 24 hour clock from 00 to 23.
011: * <p/>
012: * If a date value is specified, the rule applies just to that one day;
013: * otherwise, it applies to every day.
014: */
015: public class HourRule extends CalendarRule {
016: // private static final long serialVersionUID = 8762823343045505816L;
017:
018: /**
019: * Denotes the start of the timeframe for this rule.
020: */
021: private int _startHour;
022:
023: /**
024: * Denotes the end of the timeframe for this rule. If equal to _startHour,
025: * the applies to one hour.
026: */
027: private int _endHour;
028:
029: /**
030: * Limits the rule to a single day.
031: */
032: private GregorianCalendar _date;
033:
034: /**
035: * Number of intervals for this rule.
036: */
037: private int _ruleLength;
038:
039: public HourRule() {
040: }
041:
042: public HourRule(int startHour) {
043: _startHour = startHour;
044: _endHour = startHour;
045: _ruleLength = BusinessCalendar.INTERVALS_PER_HOUR;
046: }
047:
048: public HourRule(int startHour, int endHour) {
049: _startHour = startHour;
050: _endHour = endHour;
051: _ruleLength = BusinessCalendar.INTERVALS_PER_HOUR
052: * (endHour - startHour);
053: }
054:
055: public HourRule(int startHour, GregorianCalendar date) {
056: _startHour = startHour;
057: _endHour = startHour;
058: _date = date;
059: _ruleLength = BusinessCalendar.INTERVALS_PER_HOUR;
060: }
061:
062: public HourRule(int startHour, int endHour, GregorianCalendar date) {
063: _startHour = startHour;
064: _endHour = endHour;
065: _date = date;
066: _ruleLength = BusinessCalendar.INTERVALS_PER_HOUR
067: * (endHour - startHour);
068: }
069:
070: public long getStartHour() {
071: return _startHour;
072: }
073:
074: public void setStartHour(int startHour) {
075: _startHour = startHour;
076: }
077:
078: public long getEndHour() {
079: return _endHour;
080: }
081:
082: public void setEndHour(int endHour) {
083: _endHour = endHour;
084: }
085:
086: public GregorianCalendar getDate() {
087: return _date;
088: }
089:
090: public void setDate(GregorianCalendar date) {
091: _date = date;
092: }
093:
094: public void applyRule(BitSet mask, int length,
095: GregorianCalendar startDate, GregorianCalendar endDate,
096: TimeZone tz) {
097:
098: int startIndex = _startHour
099: * BusinessCalendar.INTERVALS_PER_HOUR;
100:
101: if (_LOGGER.isDebugEnabled()) {
102: _LOGGER.debug("Length: " + length + " Start Index: "
103: + startIndex + " Start: " + startDate.getTime()
104: + " End: " + endDate.getTime());
105: }
106:
107: // Applies to all days
108: if (_date == null) {
109: GregorianCalendar start = (GregorianCalendar) startDate
110: .clone();
111: int intervalsInDay;
112: for (int i = 0; i < length; i += intervalsInDay) {
113: BusinessCalendarUtilities.setBitSetBits(mask,
114: startIndex, _ruleLength);
115: start.add(Calendar.DATE, 1);
116: intervalsInDay = BusinessCalendarUtilities
117: .getIntervalsInDate(start);
118: startIndex += intervalsInDay;
119: }
120: } else {
121: int index = BusinessCalendarUtilities.getIntervalIndex(
122: startDate.getTime(), _date.getTime(), tz)
123: + startIndex;
124:
125: BusinessCalendarUtilities.setBitSetBits(mask, index,
126: _ruleLength);
127: }
128: }
129:
130: public String toString() {
131: return "HourRule[" + "_startHour=" + _startHour + ", _endHour="
132: + _endHour + ", _date=" + _date + ']';
133: }
134: }
|