1 7 8 9 15 package com.ibm.icu.text; 16 import java.text.CharacterIterator ; 17 18 27 public final class StringCharacterIterator implements CharacterIterator 29 { 30 private String text; 31 private int begin; 32 private int end; 33 private int pos; 35 36 40 public StringCharacterIterator(String text) 41 { 42 this(text, 0); 43 } 44 45 52 public StringCharacterIterator(String text, int pos) 53 { 54 this(text, 0, text.length(), pos); 55 } 56 57 67 public StringCharacterIterator(String text, int begin, int end, int pos) { 68 if (text == null) { 69 throw new NullPointerException (); 70 } 71 this.text = text; 72 73 if (begin < 0 || begin > end || end > text.length()) { 74 throw new IllegalArgumentException ("Invalid substring range"); 75 } 76 77 if (pos < begin || pos > end) { 78 throw new IllegalArgumentException ("Invalid position"); 79 } 80 81 this.begin = begin; 82 this.end = end; 83 this.pos = pos; 84 } 85 86 95 public void setText(String text) { 96 if (text == null) { 97 throw new NullPointerException (); 98 } 99 this.text = text; 100 this.begin = 0; 101 this.end = text.length(); 102 this.pos = 0; 103 } 104 105 110 public char first() 111 { 112 pos = begin; 113 return current(); 114 } 115 116 121 public char last() 122 { 123 if (end != begin) { 124 pos = end - 1; 125 } else { 126 pos = end; 127 } 128 return current(); 129 } 130 131 136 public char setIndex(int p) 137 { 138 if (p < begin || p > end) { 139 throw new IllegalArgumentException ("Invalid index"); 140 } 141 pos = p; 142 return current(); 143 } 144 145 150 public char current() 151 { 152 if (pos >= begin && pos < end) { 153 return text.charAt(pos); 154 } 155 else { 156 return DONE; 157 } 158 } 159 160 165 public char next() 166 { 167 if (pos < end - 1) { 168 pos++; 169 return text.charAt(pos); 170 } 171 else { 172 pos = end; 173 return DONE; 174 } 175 } 176 177 182 public char previous() 183 { 184 if (pos > begin) { 185 pos--; 186 return text.charAt(pos); 187 } 188 else { 189 return DONE; 190 } 191 } 192 193 198 public int getBeginIndex() 199 { 200 return begin; 201 } 202 203 208 public int getEndIndex() 209 { 210 return end; 211 } 212 213 218 public int getIndex() 219 { 220 return pos; 221 } 222 223 230 public boolean equals(Object obj) 231 { 232 if (this == obj) { 233 return true; 234 } 235 if (!(obj instanceof StringCharacterIterator)) { 236 return false; 237 } 238 239 StringCharacterIterator that = (StringCharacterIterator) obj; 240 241 if (hashCode() != that.hashCode()) { 242 return false; 243 } 244 if (!text.equals(that.text)) { 245 return false; 246 } 247 if (pos != that.pos || begin != that.begin || end != that.end) { 248 return false; 249 } 250 return true; 251 } 252 253 258 public int hashCode() 259 { 260 return text.hashCode() ^ pos ^ begin ^ end; 261 } 262 263 268 public Object clone() 269 { 270 try { 271 StringCharacterIterator other 272 = (StringCharacterIterator) super.clone(); 273 return other; 274 } 275 catch (CloneNotSupportedException e) { 276 throw new IllegalStateException (); 277 } 278 } 279 280 } 281 | Popular Tags |