KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > RangeDateRule


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7
8 package com.ibm.icu.util;
9
10 import java.util.Date JavaDoc;
11 import java.util.Vector JavaDoc;
12
13 /**
14  * Implementation of DateRule that takes a range.
15  * @draft ICU 2.8 (retainAll)
16  * @provisional This API might change or be removed in a future release.
17  */

18 public class RangeDateRule implements DateRule {
19     /**
20      * @draft ICU 2.8
21      * @provisional This API might change or be removed in a future release.
22      */

23     public RangeDateRule() {
24     }
25
26     /**
27      * @internal
28      */

29     // Range is a package-private class so this should be package-private too, probably
30
// public RangeDateRule(Range[] ranges)
31
// {
32
// for (int i = 0; i < ranges.length; i++) {
33
// this.ranges.addElement(ranges[i]);
34
// }
35
// }
36

37     /**
38      * @draft ICU 2.8
39      * @provisional This API might change or be removed in a future release.
40      */

41     public void add(DateRule rule) {
42         add(new Date JavaDoc(Long.MIN_VALUE), rule);
43     }
44
45     /**
46      * @draft ICU 2.8
47      * @provisional This API might change or be removed in a future release.
48      */

49     public void add(Date JavaDoc start, DateRule rule) {
50         // TODO: Insert in the right place
51
// System.out.println("Add: " + start.toString());
52
ranges.addElement(new Range(start, rule));
53     }
54
55     //-----------------------------------------------------------------------
56

57     /**
58      * @draft ICU 2.8
59      * @provisional This API might change or be removed in a future release.
60      */

61     public Date JavaDoc firstAfter(Date JavaDoc start) {
62         // Find the range that I should look at
63
int index = startIndex(start);
64         if (index == ranges.size()) {
65             index = 0;
66         }
67         Date JavaDoc result = null;
68
69         Range r = rangeAt(index);
70         Range e = rangeAt(index+1);
71
72         if (r != null && r.rule != null)
73         {
74             if (e != null) {
75                 result = r.rule.firstBetween(start, e.start);
76             } else {
77                 result = r.rule.firstAfter(start);
78             }
79         }
80         return result;
81     }
82
83     /**
84      * @draft ICU 2.8
85      * @provisional This API might change or be removed in a future release.
86      */

87     public Date JavaDoc firstBetween(Date JavaDoc start, Date JavaDoc end) {
88         if (end == null) {
89             return firstAfter(start);
90         }
91         
92         // Find the range that I should look at
93
int index = startIndex(start);
94         Date JavaDoc result = null;
95
96         Range next = rangeAt(index);
97
98         while (result == null && next != null && !next.start.after(end))
99         {
100             Range r = next;
101             next = rangeAt(index+1);
102
103             if (r.rule != null) {
104                 Date JavaDoc e = (next != null && !next.start.after(end)) ? next.start
105                                                                   : end;
106                 result = r.rule.firstBetween(start, e);
107             }
108         }
109         return result;
110     }
111
112     /**
113      * @draft ICU 2.8
114      * @provisional This API might change or be removed in a future release.
115      */

116     public boolean isOn(Date JavaDoc date) {
117         Range r = rangeAt(startIndex(date));
118         return r != null && r.rule != null && r.rule.isOn(date);
119     }
120
121     /**
122      * Check whether this event occurs at least once between the two
123      * dates given.
124      * @draft ICU 2.8
125      * @provisional This API might change or be removed in a future release.
126      */

127     public boolean isBetween(Date JavaDoc start, Date JavaDoc end) {
128         return firstBetween(start,end) == null;
129     }
130
131     /*
132      * find the index of the last range whose start date is before "start"
133      * returns an index >= ranges.size() if there is none
134      */

135     private int startIndex(Date JavaDoc start) {
136         int lastIndex = ranges.size();
137
138         for (int i = 0; i < ranges.size(); i++) {
139             Range r = (Range) ranges.elementAt(i);
140             if (start.before(r.start)) {
141                 break;
142             }
143             lastIndex = i;
144         }
145         return lastIndex;
146     }
147
148     private Range rangeAt(int index) {
149        return (index < ranges.size()) ? (Range) ranges.elementAt(index)
150                                       : null;
151     }
152
153     Vector JavaDoc ranges = new Vector JavaDoc(2,2);
154 }
155
156 //-----------------------------------------------------------------------
157
// Privates
158
//
159

160 class Range {
161     public Range(Date JavaDoc start, DateRule rule) {
162         this.start = start;
163         this.rule = rule;
164     }
165     public Date JavaDoc start;
166     public DateRule rule;
167 }
168
169
Popular Tags