1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 1996-2014, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 10 11 // NOTE: This class is identical to java.text.StringCharacterIterator 12 // in JDK 1.2. It's copied here because the JDK 1.1 version of 13 // StringCharacterIterator has a bug that prevents it from working 14 // right with RuleBasedBreakIterator. This class is unnecessary 15 // when using RuleBasedBreakIterator with JDK 1.2. 16 17 package com.ibm.icu.text; 18 import java.text.CharacterIterator; 19 20 import com.ibm.icu.util.ICUCloneNotSupportedException; 21 22 /** 23 * <code>StringCharacterIterator</code> implements the 24 * <code>CharacterIterater</code> protocol for a <code>String</code>. 25 * The <code>StringCharacterIterator</code> class iterates over the 26 * entire <code>String</code>. 27 * 28 * @see CharacterIterator 29 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 30 */ 31 @Deprecated 32 ///CLOVER:OFF 33 public final class StringCharacterIterator implements CharacterIterator 34 { 35 private String text; 36 private int begin; 37 private int end; 38 // invariant: begin <= pos <= end 39 private int pos; 40 41 /** 42 * Constructs an iterator with an initial index of 0. 43 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 44 */ 45 @Deprecated StringCharacterIterator(String text)46 public StringCharacterIterator(String text) 47 { 48 this(text, 0); 49 } 50 51 /** 52 * Constructs an iterator with the specified initial index. 53 * 54 * @param text The String to be iterated over 55 * @param pos Initial iterator position 56 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 57 */ 58 @Deprecated StringCharacterIterator(String text, int pos)59 public StringCharacterIterator(String text, int pos) 60 { 61 this(text, 0, text.length(), pos); 62 } 63 64 /** 65 * Constructs an iterator over the given range of the given string, with the 66 * index set at the specified position. 67 * 68 * @param text The String to be iterated over 69 * @param begin Index of the first character 70 * @param end Index of the character following the last character 71 * @param pos Initial iterator position 72 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 73 */ 74 @Deprecated StringCharacterIterator(String text, int begin, int end, int pos)75 public StringCharacterIterator(String text, int begin, int end, int pos) { 76 if (text == null) { 77 throw new NullPointerException(); 78 } 79 this.text = text; 80 81 if (begin < 0 || begin > end || end > text.length()) { 82 throw new IllegalArgumentException("Invalid substring range"); 83 } 84 85 if (pos < begin || pos > end) { 86 throw new IllegalArgumentException("Invalid position"); 87 } 88 89 this.begin = begin; 90 this.end = end; 91 this.pos = pos; 92 } 93 94 /** 95 * Reset this iterator to point to a new string. This package-visible 96 * method is used by other java.text classes that want to avoid allocating 97 * new StringCharacterIterator objects every time their setText method 98 * is called. 99 * 100 * @param text The String to be iterated over 101 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 102 */ 103 @Deprecated setText(String text)104 public void setText(String text) { 105 if (text == null) { 106 throw new NullPointerException(); 107 } 108 this.text = text; 109 this.begin = 0; 110 this.end = text.length(); 111 this.pos = 0; 112 } 113 114 /** 115 * Implements CharacterIterator.first() for String. 116 * @see CharacterIterator#first 117 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 118 */ 119 @Deprecated first()120 public char first() 121 { 122 pos = begin; 123 return current(); 124 } 125 126 /** 127 * Implements CharacterIterator.last() for String. 128 * @see CharacterIterator#last 129 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 130 */ 131 @Deprecated last()132 public char last() 133 { 134 if (end != begin) { 135 pos = end - 1; 136 } else { 137 pos = end; 138 } 139 return current(); 140 } 141 142 /** 143 * Implements CharacterIterator.setIndex() for String. 144 * @see CharacterIterator#setIndex 145 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 146 */ 147 @Deprecated setIndex(int p)148 public char setIndex(int p) 149 { 150 if (p < begin || p > end) { 151 throw new IllegalArgumentException("Invalid index"); 152 } 153 pos = p; 154 return current(); 155 } 156 157 /** 158 * Implements CharacterIterator.current() for String. 159 * @see CharacterIterator#current 160 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 161 */ 162 @Deprecated current()163 public char current() 164 { 165 if (pos >= begin && pos < end) { 166 return text.charAt(pos); 167 } 168 else { 169 return DONE; 170 } 171 } 172 173 /** 174 * Implements CharacterIterator.next() for String. 175 * @see CharacterIterator#next 176 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 177 */ 178 @Deprecated next()179 public char next() 180 { 181 if (pos < end - 1) { 182 pos++; 183 return text.charAt(pos); 184 } 185 else { 186 pos = end; 187 return DONE; 188 } 189 } 190 191 /** 192 * Implements CharacterIterator.previous() for String. 193 * @see CharacterIterator#previous 194 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 195 */ 196 @Deprecated previous()197 public char previous() 198 { 199 if (pos > begin) { 200 pos--; 201 return text.charAt(pos); 202 } 203 else { 204 return DONE; 205 } 206 } 207 208 /** 209 * Implements CharacterIterator.getBeginIndex() for String. 210 * @see CharacterIterator#getBeginIndex 211 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 212 */ 213 @Deprecated getBeginIndex()214 public int getBeginIndex() 215 { 216 return begin; 217 } 218 219 /** 220 * Implements CharacterIterator.getEndIndex() for String. 221 * @see CharacterIterator#getEndIndex 222 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 223 */ 224 @Deprecated getEndIndex()225 public int getEndIndex() 226 { 227 return end; 228 } 229 230 /** 231 * Implements CharacterIterator.getIndex() for String. 232 * @see CharacterIterator#getIndex 233 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 234 */ 235 @Deprecated getIndex()236 public int getIndex() 237 { 238 return pos; 239 } 240 241 /** 242 * Compares the equality of two StringCharacterIterator objects. 243 * @param obj the StringCharacterIterator object to be compared with. 244 * @return true if the given obj is the same as this 245 * StringCharacterIterator object; false otherwise. 246 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 247 */ 248 @Deprecated equals(Object obj)249 public boolean equals(Object obj) 250 { 251 if (this == obj) { 252 return true; 253 } 254 if (!(obj instanceof StringCharacterIterator)) { 255 return false; 256 } 257 258 StringCharacterIterator that = (StringCharacterIterator) obj; 259 260 if (hashCode() != that.hashCode()) { 261 return false; 262 } 263 if (!text.equals(that.text)) { 264 return false; 265 } 266 if (pos != that.pos || begin != that.begin || end != that.end) { 267 return false; 268 } 269 return true; 270 } 271 272 /** 273 * Computes a hashcode for this iterator. 274 * @return A hash code 275 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 276 */ 277 @Deprecated hashCode()278 public int hashCode() 279 { 280 return text.hashCode() ^ pos ^ begin ^ end; 281 } 282 283 /** 284 * Creates a copy of this iterator. 285 * @return A copy of this 286 * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead. 287 */ 288 @Deprecated clone()289 public Object clone() 290 { 291 try { 292 StringCharacterIterator other 293 = (StringCharacterIterator) super.clone(); 294 return other; 295 } 296 catch (CloneNotSupportedException e) { 297 throw new ICUCloneNotSupportedException(e); 298 } 299 } 300 301 } 302 ///CLOVER:ON 303