001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.util;
009:
010: import java.util.Date;
011: import java.util.Vector;
012:
013: /**
014: * Implementation of DateRule that takes a range.
015: * @draft ICU 2.8 (retainAll)
016: * @provisional This API might change or be removed in a future release.
017: */
018: public class RangeDateRule implements DateRule {
019: /**
020: * @draft ICU 2.8
021: * @provisional This API might change or be removed in a future release.
022: */
023: public RangeDateRule() {
024: }
025:
026: /**
027: * @internal
028: */
029: // Range is a package-private class so this should be package-private too, probably
030: // public RangeDateRule(Range[] ranges)
031: // {
032: // for (int i = 0; i < ranges.length; i++) {
033: // this.ranges.addElement(ranges[i]);
034: // }
035: // }
036: /**
037: * @draft ICU 2.8
038: * @provisional This API might change or be removed in a future release.
039: */
040: public void add(DateRule rule) {
041: add(new Date(Long.MIN_VALUE), rule);
042: }
043:
044: /**
045: * @draft ICU 2.8
046: * @provisional This API might change or be removed in a future release.
047: */
048: public void add(Date start, DateRule rule) {
049: // TODO: Insert in the right place
050: // System.out.println("Add: " + start.toString());
051: ranges.addElement(new Range(start, rule));
052: }
053:
054: //-----------------------------------------------------------------------
055:
056: /**
057: * @draft ICU 2.8
058: * @provisional This API might change or be removed in a future release.
059: */
060: public Date firstAfter(Date start) {
061: // Find the range that I should look at
062: int index = startIndex(start);
063: if (index == ranges.size()) {
064: index = 0;
065: }
066: Date result = null;
067:
068: Range r = rangeAt(index);
069: Range e = rangeAt(index + 1);
070:
071: if (r != null && r.rule != null) {
072: if (e != null) {
073: result = r.rule.firstBetween(start, e.start);
074: } else {
075: result = r.rule.firstAfter(start);
076: }
077: }
078: return result;
079: }
080:
081: /**
082: * @draft ICU 2.8
083: * @provisional This API might change or be removed in a future release.
084: */
085: public Date firstBetween(Date start, Date end) {
086: if (end == null) {
087: return firstAfter(start);
088: }
089:
090: // Find the range that I should look at
091: int index = startIndex(start);
092: Date result = null;
093:
094: Range next = rangeAt(index);
095:
096: while (result == null && next != null && !next.start.after(end)) {
097: Range r = next;
098: next = rangeAt(index + 1);
099:
100: if (r.rule != null) {
101: Date e = (next != null && !next.start.after(end)) ? next.start
102: : end;
103: result = r.rule.firstBetween(start, e);
104: }
105: }
106: return result;
107: }
108:
109: /**
110: * @draft ICU 2.8
111: * @provisional This API might change or be removed in a future release.
112: */
113: public boolean isOn(Date date) {
114: Range r = rangeAt(startIndex(date));
115: return r != null && r.rule != null && r.rule.isOn(date);
116: }
117:
118: /**
119: * Check whether this event occurs at least once between the two
120: * dates given.
121: * @draft ICU 2.8
122: * @provisional This API might change or be removed in a future release.
123: */
124: public boolean isBetween(Date start, Date end) {
125: return firstBetween(start, end) == null;
126: }
127:
128: /*
129: * find the index of the last range whose start date is before "start"
130: * returns an index >= ranges.size() if there is none
131: */
132: private int startIndex(Date start) {
133: int lastIndex = ranges.size();
134:
135: for (int i = 0; i < ranges.size(); i++) {
136: Range r = (Range) ranges.elementAt(i);
137: if (start.before(r.start)) {
138: break;
139: }
140: lastIndex = i;
141: }
142: return lastIndex;
143: }
144:
145: private Range rangeAt(int index) {
146: return (index < ranges.size()) ? (Range) ranges
147: .elementAt(index) : null;
148: }
149:
150: Vector ranges = new Vector(2, 2);
151: }
152:
153: //-----------------------------------------------------------------------
154: // Privates
155: //
156:
157: class Range {
158: public Range(Date start, DateRule rule) {
159: this .start = start;
160: this .rule = rule;
161: }
162:
163: public Date start;
164: public DateRule rule;
165: }
|