1 7 package com.ibm.icu.impl; 8 9 import java.text.CharacterIterator ; 10 11 import com.ibm.icu.text.*; 12 13 18 19 public class CharacterIteratorWrapper extends UCharacterIterator { 20 21 private CharacterIterator iterator; 22 23 24 public CharacterIteratorWrapper(CharacterIterator iter){ 25 if(iter==null){ 26 throw new IllegalArgumentException (); 27 } 28 iterator = iter; 29 } 30 31 34 public int current() { 35 int c = iterator.current(); 36 if(c==CharacterIterator.DONE){ 37 return DONE; 38 } 39 return c; 40 } 41 42 45 public int getLength() { 46 return (iterator.getEndIndex() - iterator.getBeginIndex()); 47 } 48 49 52 public int getIndex() { 53 return iterator.getIndex(); 54 } 55 56 59 public int next() { 60 int i = iterator.current(); 61 iterator.next(); 62 if(i==CharacterIterator.DONE){ 63 return DONE; 64 } 65 return i; 66 } 67 68 71 public int previous() { 72 int i = iterator.previous(); 73 if(i==CharacterIterator.DONE){ 74 return DONE; 75 } 76 return i; 77 } 78 79 82 public void setIndex(int index) { 83 try{ 84 iterator.setIndex(index); 85 }catch(IllegalArgumentException e){ 86 throw new IndexOutOfBoundsException (); 87 } 88 } 89 90 93 public void setToLimit() { 94 iterator.setIndex(iterator.getEndIndex()); 95 } 96 97 100 public int getText(char[] fillIn, int offset){ 101 int length =iterator.getEndIndex() - iterator.getBeginIndex(); 102 int currentIndex = iterator.getIndex(); 103 if(offset < 0 || offset + length > fillIn.length){ 104 throw new IndexOutOfBoundsException (Integer.toString(length)); 105 } 106 107 for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) { 108 fillIn[offset++] = ch; 109 } 110 iterator.setIndex(currentIndex); 111 112 return length; 113 } 114 115 119 public Object clone(){ 120 try { 121 CharacterIteratorWrapper result = (CharacterIteratorWrapper) super.clone(); 122 result.iterator = (CharacterIterator )this.iterator.clone(); 123 return result; 124 } catch (CloneNotSupportedException e) { 125 return null; } 127 } 128 129 130 public int moveIndex(int delta){ 131 int length = iterator.getEndIndex() - iterator.getBeginIndex(); 132 int idx = iterator.getIndex()+delta; 133 134 if(idx < 0) { 135 idx = 0; 136 } else if(idx > length) { 137 idx = length; 138 } 139 return iterator.setIndex(idx); 140 } 141 142 145 public CharacterIterator getCharacterIterator(){ 146 return (CharacterIterator )iterator.clone(); 147 } 148 } 149 | Popular Tags |