]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/util/RangeDateRule.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / util / RangeDateRule.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 package com.ibm.icu.util;
9
10 import java.util.ArrayList;
11 import java.util.Date;
12 import java.util.List;
13
14 /**
15  * <b>Note:</b> The Holiday framework is a technology preview.
16  * Despite its age, is still draft API, and clients should treat it as such.
17  * 
18  * Implementation of DateRule that takes a range.
19  * @draft ICU 2.8 (retainAll)
20  * @provisional This API might change or be removed in a future release.
21  */
22 public class RangeDateRule implements DateRule {
23     /**
24      * @draft ICU 2.8
25      * @provisional This API might change or be removed in a future release.
26      */
27     public RangeDateRule() {
28     }
29
30     // Range is a package-private class so this should be package-private too, probably
31 //    public RangeDateRule(Range[] ranges)
32 //    {
33 //        for (int i = 0; i < ranges.length; i++) {
34 //            this.ranges.addElement(ranges[i]);
35 //        }
36 //    }
37
38     /**
39      * @draft ICU 2.8
40      * @provisional This API might change or be removed in a future release.
41      */
42     public void add(DateRule rule) {
43         add(new Date(Long.MIN_VALUE), rule);
44     }
45
46     /**
47      * @draft ICU 2.8
48      * @provisional This API might change or be removed in a future release.
49      */
50     public void add(Date start, DateRule rule) {
51         // TODO: Insert in the right place
52         // System.out.println("Add: " + start.toString());
53         ranges.add(new Range(start, rule));
54     }
55
56     //-----------------------------------------------------------------------
57
58     /**
59      * @draft ICU 2.8
60      * @provisional This API might change or be removed in a future release.
61      */
62     public Date firstAfter(Date start) {
63         // Find the range that I should look at
64         int index = startIndex(start);
65         if (index == ranges.size()) {
66             index = 0;
67         }
68         Date result = null;
69
70         Range r = rangeAt(index);
71         Range e = rangeAt(index+1);
72
73         if (r != null && r.rule != null)
74         {
75             if (e != null) {
76                 result = r.rule.firstBetween(start, e.start);
77             } else {
78                 result = r.rule.firstAfter(start);
79             }
80         }
81         return result;
82     }
83
84     /**
85      * @draft ICU 2.8
86      * @provisional This API might change or be removed in a future release.
87      */
88     public Date firstBetween(Date start, Date end) {
89         if (end == null) {
90             return firstAfter(start);
91         }
92         
93         // Find the range that I should look at
94         int index = startIndex(start);
95         Date result = null;
96
97         Range next = rangeAt(index);
98
99         while (result == null && next != null && !next.start.after(end))
100         {
101             Range r = next;
102             next = rangeAt(index+1);
103
104             if (r.rule != null) {
105                 Date e = (next != null && !next.start.after(end)) ? next.start
106                                                                   : end;
107                 result = r.rule.firstBetween(start, e);
108             }
109         }
110         return result;
111     }
112
113     /**
114      * @draft ICU 2.8
115      * @provisional This API might change or be removed in a future release.
116      */
117     public boolean isOn(Date date) {
118         Range r = rangeAt(startIndex(date));
119         return r != null && r.rule != null && r.rule.isOn(date);
120     }
121
122     /**
123      * Check whether this event occurs at least once between the two
124      * dates given.
125      * @draft ICU 2.8
126      * @provisional This API might change or be removed in a future release.
127      */
128     public boolean isBetween(Date start, Date end) {
129         return firstBetween(start,end) == null;
130     }
131
132     /*
133      * find the index of the last range whose start date is before "start"
134      * returns an index >= ranges.size() if there is none
135      */
136     private int startIndex(Date start) {
137         int lastIndex = ranges.size();
138
139         for (int i = 0; i < ranges.size(); i++) {
140             Range r = ranges.get(i);
141             if (start.before(r.start)) {
142                 break;
143             }
144             lastIndex = i;
145         }
146         return lastIndex;
147     }
148
149     private Range rangeAt(int index) {
150        return (index < ranges.size()) ? ranges.get(index)
151                                       : null;
152     }
153
154     List<Range> ranges = new ArrayList<Range>(2);
155 }
156
157 //-----------------------------------------------------------------------
158 // Privates
159 //
160
161 class Range {
162     public Range(Date start, DateRule rule) {
163         this.start = start;
164         this.rule = rule;
165     }
166     public Date     start;
167     public DateRule rule;
168 }
169