1 7 8 package com.ibm.icu.util; 9 10 import java.util.Date ; 11 import java.util.Vector ; 12 13 18 public class RangeDateRule implements DateRule { 19 23 public RangeDateRule() { 24 } 25 26 29 37 41 public void add(DateRule rule) { 42 add(new Date (Long.MIN_VALUE), rule); 43 } 44 45 49 public void add(Date start, DateRule rule) { 50 ranges.addElement(new Range(start, rule)); 53 } 54 55 57 61 public Date firstAfter(Date start) { 62 int index = startIndex(start); 64 if (index == ranges.size()) { 65 index = 0; 66 } 67 Date 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 87 public Date firstBetween(Date start, Date end) { 88 if (end == null) { 89 return firstAfter(start); 90 } 91 92 int index = startIndex(start); 94 Date 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 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 116 public boolean isOn(Date date) { 117 Range r = rangeAt(startIndex(date)); 118 return r != null && r.rule != null && r.rule.isOn(date); 119 } 120 121 127 public boolean isBetween(Date start, Date end) { 128 return firstBetween(start,end) == null; 129 } 130 131 135 private int startIndex(Date 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 ranges = new Vector (2,2); 154 } 155 156 160 class Range { 161 public Range(Date start, DateRule rule) { 162 this.start = start; 163 this.rule = rule; 164 } 165 public Date start; 166 public DateRule rule; 167 } 168 169 | Popular Tags |