1 7 package com.ibm.icu.text; 8 9 import java.util.*; 10 11 43 public class UnicodeSetIterator { 44 45 51 public static int IS_STRING = -1; 52 53 58 public int codepoint; 59 60 69 public int codepointEnd; 70 71 77 public String string; 78 79 84 public UnicodeSetIterator(UnicodeSet set) { 85 reset(set); 86 } 87 88 94 public UnicodeSetIterator() { 95 reset(new UnicodeSet()); 96 } 97 98 117 public boolean next() { 118 if (nextElement <= endElement) { 119 codepoint = codepointEnd = nextElement++; 120 return true; 121 } 122 if (range < endRange) { 123 loadRange(++range); 124 codepoint = codepointEnd = nextElement++; 125 return true; 126 } 127 128 130 if (stringIterator == null) return false; 131 codepoint = IS_STRING; string = (String )stringIterator.next(); 133 if (!stringIterator.hasNext()) stringIterator = null; 134 return true; 135 } 136 137 157 public boolean nextRange() { 158 if (nextElement <= endElement) { 159 codepointEnd = endElement; 160 codepoint = nextElement; 161 nextElement = endElement+1; 162 return true; 163 } 164 if (range < endRange) { 165 loadRange(++range); 166 codepointEnd = endElement; 167 codepoint = nextElement; 168 nextElement = endElement+1; 169 return true; 170 } 171 172 174 if (stringIterator == null) return false; 175 codepoint = IS_STRING; string = (String )stringIterator.next(); 177 if (!stringIterator.hasNext()) stringIterator = null; 178 return true; 179 } 180 181 188 public void reset(UnicodeSet set) { 189 this.set = set; 190 reset(); 191 } 192 193 197 public void reset() { 198 endRange = set.getRangeCount() - 1; 199 range = 0; 200 endElement = -1; 201 nextElement = 0; 202 if (endRange >= 0) { 203 loadRange(range); 204 } 205 stringIterator = null; 206 if (set.strings != null) { 207 stringIterator = set.strings.iterator(); 208 if (!stringIterator.hasNext()) stringIterator = null; 209 } 210 } 211 212 217 public String getString() { 218 if (codepoint != IS_STRING) { 219 return UTF16.valueOf(codepoint); 220 } 221 return string; 222 } 223 224 226 private UnicodeSet set; 227 private int endRange = 0; 228 private int range = 0; 229 233 protected int endElement; 234 238 protected int nextElement; 239 private Iterator stringIterator = null; 240 241 244 245 249 protected void loadRange(int range) { 250 nextElement = set.getRangeStart(range); 251 endElement = set.getRangeEnd(range); 252 } 253 } 254 | Popular Tags |