1 7 8 22 23 package java.text; 24 25 33 34 public final class StringCharacterIterator implements CharacterIterator 35 { 36 private String text; 37 private int begin; 38 private int end; 39 private int pos; 41 42 45 public StringCharacterIterator(String text) 46 { 47 this(text, 0); 48 } 49 50 56 public StringCharacterIterator(String text, int pos) 57 { 58 this(text, 0, text.length(), pos); 59 } 60 61 70 public StringCharacterIterator(String text, int begin, int end, int pos) { 71 if (text == null) 72 throw new NullPointerException (); 73 this.text = text; 74 75 if (begin < 0 || begin > end || end > text.length()) 76 throw new IllegalArgumentException ("Invalid substring range"); 77 78 if (pos < begin || pos > end) 79 throw new IllegalArgumentException ("Invalid position"); 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 this.text = text; 99 this.begin = 0; 100 this.end = text.length(); 101 this.pos = 0; 102 } 103 104 108 public char first() 109 { 110 pos = begin; 111 return current(); 112 } 113 114 118 public char last() 119 { 120 if (end != begin) { 121 pos = end - 1; 122 } else { 123 pos = end; 124 } 125 return current(); 126 } 127 128 132 public char setIndex(int p) 133 { 134 if (p < begin || p > end) 135 throw new IllegalArgumentException ("Invalid index"); 136 pos = p; 137 return current(); 138 } 139 140 144 public char current() 145 { 146 if (pos >= begin && pos < end) { 147 return text.charAt(pos); 148 } 149 else { 150 return DONE; 151 } 152 } 153 154 158 public char next() 159 { 160 if (pos < end - 1) { 161 pos++; 162 return text.charAt(pos); 163 } 164 else { 165 pos = end; 166 return DONE; 167 } 168 } 169 170 174 public char previous() 175 { 176 if (pos > begin) { 177 pos--; 178 return text.charAt(pos); 179 } 180 else { 181 return DONE; 182 } 183 } 184 185 189 public int getBeginIndex() 190 { 191 return begin; 192 } 193 194 198 public int getEndIndex() 199 { 200 return end; 201 } 202 203 207 public int getIndex() 208 { 209 return pos; 210 } 211 212 218 public boolean equals(Object obj) 219 { 220 if (this == obj) 221 return true; 222 if (!(obj instanceof StringCharacterIterator )) 223 return false; 224 225 StringCharacterIterator that = (StringCharacterIterator ) obj; 226 227 if (hashCode() != that.hashCode()) 228 return false; 229 if (!text.equals(that.text)) 230 return false; 231 if (pos != that.pos || begin != that.begin || end != that.end) 232 return false; 233 return true; 234 } 235 236 240 public int hashCode() 241 { 242 return text.hashCode() ^ pos ^ begin ^ end; 243 } 244 245 249 public Object clone() 250 { 251 try { 252 StringCharacterIterator other 253 = (StringCharacterIterator ) super.clone(); 254 return other; 255 } 256 catch (CloneNotSupportedException e) { 257 throw new InternalError (); 258 } 259 } 260 261 } 262 | Popular Tags |