1 7 package com.ibm.icu.text; 8 9 10 import java.text.CharacterIterator ; 11 12 import com.ibm.icu.impl.CharacterIteratorWrapper; 13 import com.ibm.icu.impl.ReplaceableUCharacterIterator; 14 import com.ibm.icu.impl.UCharArrayIterator; 15 import com.ibm.icu.impl.UCharacterIteratorWrapper; 16 import com.ibm.icu.impl.UCharacterProperty; 17 18 19 33 public abstract class UCharacterIterator 34 implements Cloneable ,UForwardCharacterIterator { 35 36 40 protected UCharacterIterator(){ 41 } 42 43 45 53 public static final UCharacterIterator getInstance(Replaceable source){ 54 return new ReplaceableUCharacterIterator(source); 55 } 56 57 65 public static final UCharacterIterator getInstance(String source){ 66 return new ReplaceableUCharacterIterator(source); 67 } 68 69 77 public static final UCharacterIterator getInstance(char[] source){ 78 return getInstance(source,0,source.length); 79 } 80 81 89 public static final UCharacterIterator getInstance(char[] source, int start, int limit){ 90 return new UCharArrayIterator(source,start,limit); 91 } 92 100 public static final UCharacterIterator getInstance(StringBuffer source){ 101 return new ReplaceableUCharacterIterator(source); 102 } 103 104 112 public static final UCharacterIterator getInstance(CharacterIterator source){ 113 return new CharacterIteratorWrapper(source); 114 } 115 116 124 public CharacterIterator getCharacterIterator(){ 125 return new UCharacterIteratorWrapper(this); 126 } 127 128 134 public abstract int current(); 135 136 145 public int currentCodePoint(){ 146 int ch = current(); 147 if(UTF16.isLeadSurrogate((char)ch)){ 148 next(); 151 int ch2 = current(); 155 previous(); 158 159 if(UTF16.isTrailSurrogate((char)ch2)){ 160 return UCharacterProperty.getRawSupplementary( 163 (char)ch,(char)ch2 164 ); 165 } 166 } 167 return ch; 168 } 169 170 175 public abstract int getLength(); 176 177 178 183 public abstract int getIndex(); 184 185 186 195 public abstract int next(); 196 197 208 public int nextCodePoint(){ 209 int ch1 = next(); 210 if(UTF16.isLeadSurrogate((char)ch1)){ 211 int ch2 = next(); 212 if(UTF16.isTrailSurrogate((char)ch2)){ 213 return UCharacterProperty.getRawSupplementary((char)ch1, 214 (char)ch2); 215 }else if (ch2 != DONE) { 216 previous(); 218 } 219 } 220 return ch1; 221 } 222 223 232 public abstract int previous(); 233 234 235 246 public int previousCodePoint(){ 247 int ch1 = previous(); 248 if(UTF16.isTrailSurrogate((char)ch1)){ 249 int ch2 = previous(); 250 if(UTF16.isLeadSurrogate((char)ch2)){ 251 return UCharacterProperty.getRawSupplementary((char)ch2, 252 (char)ch1); 253 }else if (ch2 != DONE) { 254 next(); 256 } 257 } 258 return ch1; 259 } 260 261 268 public abstract void setIndex(int index); 269 270 274 public void setToLimit() { 275 setIndex(getLength()); 276 } 277 278 282 public void setToStart() { 283 setIndex(0); 284 } 285 286 321 public abstract int getText(char[] fillIn, int offset); 322 323 333 public final int getText(char[] fillIn) { 334 return getText(fillIn, 0); 335 } 336 337 342 public String getText() { 343 char[] text = new char[getLength()]; 344 getText(text); 345 return new String (text); 346 } 347 348 364 public int moveIndex(int delta) { 365 int x = Math.max(0, Math.min(getIndex() + delta, getLength())); 366 setIndex(x); 367 return x; 368 } 369 370 385 public int moveCodePointIndex(int delta){ 386 if(delta>0){ 387 while(delta>0 && nextCodePoint() != DONE){delta--;} 388 }else{ 389 while(delta<0 && previousCodePoint() != DONE){delta++;} 390 } 391 if(delta!=0){ 392 throw new IndexOutOfBoundsException (); 393 } 394 395 return getIndex(); 396 } 397 398 404 public Object clone() throws CloneNotSupportedException { 405 return super.clone(); 406 } 407 408 } 409 410 | Popular Tags |