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: public class DateRule extends CalendarRule {
009: // private static final long serialVersionUID = 4624766643201543097L;
010:
011: private TimeZone _tz;
012:
013: // Start date of this rule
014: private GregorianCalendar _startDate;
015:
016: // End date of this rule. If it equals the start date, then this rule
017: // applies to a single date
018: private GregorianCalendar _endDate;
019:
020: private int _ruleLength;
021: private static final int MAX_HOUR = 23;
022: private static final int MAX_MINUTE = 59;
023:
024: public DateRule() {
025: }
026:
027: public DateRule(GregorianCalendar startDate, TimeZone tz) {
028: startDate.setTimeZone(tz);
029: _startDate = startDate;
030: _endDate = startDate;
031: _ruleLength = BusinessCalendarUtilities
032: .getIntervalsInDate(_startDate);
033: }
034:
035: public DateRule(GregorianCalendar startDate,
036: GregorianCalendar endDate, TimeZone tz) {
037:
038: _startDate = startDate;
039: _endDate = endDate;
040: _startDate.setTimeZone(tz);
041: _endDate.setTimeZone(tz);
042: _endDate.set(Calendar.HOUR, MAX_HOUR);
043: _endDate.set(Calendar.MINUTE, MAX_MINUTE);
044: _ruleLength = BusinessCalendarUtilities.countBitsForInterval(
045: _startDate.getTime(), _endDate.getTime(), tz) + 1;
046: }
047:
048: public TimeZone getTimeZone() {
049: return _tz;
050: }
051:
052: public GregorianCalendar getStartDate() {
053: return _startDate;
054: }
055:
056: public GregorianCalendar getEndDate() {
057: return _endDate;
058: }
059:
060: public void setTimeZone(TimeZone tz) {
061: _tz = tz;
062: }
063:
064: public void setStartDate(GregorianCalendar startDate) {
065: _startDate = startDate;
066: }
067:
068: public void setEndDate(GregorianCalendar endDate) {
069: _endDate = endDate;
070: }
071:
072: public void applyRule(BitSet mask, int length,
073: GregorianCalendar startDate, GregorianCalendar endDate,
074: TimeZone tz) {
075:
076: if (_startDate.after(endDate) || _endDate.before(startDate)) {
077: // This rule is not even in this calendar
078: // TODO: Does this need to throw an exception?
079: return;
080: }
081:
082: // Some part of this rule intersects with the calendar, now to figure
083: // out if it is a single date or a range.
084: if (_LOGGER.isDebugEnabled()) {
085: _LOGGER.debug("Interval index: "
086: + BusinessCalendarUtilities.getIntervalIndex(
087: startDate.getTime(), _startDate.getTime(),
088: tz) + " Interval length: " + _ruleLength);
089: }
090:
091: // This works for both a single date and a range of dates.
092: BusinessCalendarUtilities.setBitSetBits(mask,
093: BusinessCalendarUtilities.getIntervalIndex(startDate
094: .getTime(), _startDate.getTime(), tz),
095: _ruleLength);
096: }
097:
098: public String toString() {
099: return "DateRule[" + "_startDate=" + _startDate.getTime()
100: + ", _endDate=" + _endDate.getTime() + ']';
101: }
102: }
|