001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: // NOTE: This class is identical to java.text.StringCharacterIterator
009: // in JDK 1.2. It's copied here because the JDK 1.1 version of
010: // StringCharacterIterator has a bug that prevents it from working
011: // right with RuleBasedBreakIterator. This class is unnecessary
012: // when using RuleBasedBreakIterator with JDK 1.2.
013: package com.ibm.icu.text;
014:
015: import java.text.CharacterIterator;
016:
017: /**
018: * <code>StringCharacterIterator</code> implements the
019: * <code>CharacterIterater</code> protocol for a <code>String</code>.
020: * The <code>StringCharacterIterator</code> class iterates over the
021: * entire <code>String</code>.
022: *
023: * @see CharacterIterator
024: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
025: */
026: ///CLOVER:OFF
027: public final class StringCharacterIterator implements CharacterIterator {
028: private String text;
029: private int begin;
030: private int end;
031: // invariant: begin <= pos <= end
032: private int pos;
033:
034: /**
035: * Constructs an iterator with an initial index of 0.
036: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
037: */
038: public StringCharacterIterator(String text) {
039: this (text, 0);
040: }
041:
042: /**
043: * Constructs an iterator with the specified initial index.
044: *
045: * @param text The String to be iterated over
046: * @param pos Initial iterator position
047: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
048: */
049: public StringCharacterIterator(String text, int pos) {
050: this (text, 0, text.length(), pos);
051: }
052:
053: /**
054: * Constructs an iterator over the given range of the given string, with the
055: * index set at the specified position.
056: *
057: * @param text The String to be iterated over
058: * @param begin Index of the first character
059: * @param end Index of the character following the last character
060: * @param pos Initial iterator position
061: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
062: */
063: public StringCharacterIterator(String text, int begin, int end,
064: int pos) {
065: if (text == null) {
066: throw new NullPointerException();
067: }
068: this .text = text;
069:
070: if (begin < 0 || begin > end || end > text.length()) {
071: throw new IllegalArgumentException(
072: "Invalid substring range");
073: }
074:
075: if (pos < begin || pos > end) {
076: throw new IllegalArgumentException("Invalid position");
077: }
078:
079: this .begin = begin;
080: this .end = end;
081: this .pos = pos;
082: }
083:
084: /**
085: * Reset this iterator to point to a new string. This package-visible
086: * method is used by other java.text classes that want to avoid allocating
087: * new StringCharacterIterator objects every time their setText method
088: * is called.
089: *
090: * @param text The String to be iterated over
091: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
092: */
093: public void setText(String text) {
094: if (text == null) {
095: throw new NullPointerException();
096: }
097: this .text = text;
098: this .begin = 0;
099: this .end = text.length();
100: this .pos = 0;
101: }
102:
103: /**
104: * Implements CharacterIterator.first() for String.
105: * @see CharacterIterator#first
106: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
107: */
108: public char first() {
109: pos = begin;
110: return current();
111: }
112:
113: /**
114: * Implements CharacterIterator.last() for String.
115: * @see CharacterIterator#last
116: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
117: */
118: public char last() {
119: if (end != begin) {
120: pos = end - 1;
121: } else {
122: pos = end;
123: }
124: return current();
125: }
126:
127: /**
128: * Implements CharacterIterator.setIndex() for String.
129: * @see CharacterIterator#setIndex
130: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
131: */
132: public char setIndex(int p) {
133: if (p < begin || p > end) {
134: throw new IllegalArgumentException("Invalid index");
135: }
136: pos = p;
137: return current();
138: }
139:
140: /**
141: * Implements CharacterIterator.current() for String.
142: * @see CharacterIterator#current
143: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
144: */
145: public char current() {
146: if (pos >= begin && pos < end) {
147: return text.charAt(pos);
148: } else {
149: return DONE;
150: }
151: }
152:
153: /**
154: * Implements CharacterIterator.next() for String.
155: * @see CharacterIterator#next
156: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
157: */
158: public char next() {
159: if (pos < end - 1) {
160: pos++;
161: return text.charAt(pos);
162: } else {
163: pos = end;
164: return DONE;
165: }
166: }
167:
168: /**
169: * Implements CharacterIterator.previous() for String.
170: * @see CharacterIterator#previous
171: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
172: */
173: public char previous() {
174: if (pos > begin) {
175: pos--;
176: return text.charAt(pos);
177: } else {
178: return DONE;
179: }
180: }
181:
182: /**
183: * Implements CharacterIterator.getBeginIndex() for String.
184: * @see CharacterIterator#getBeginIndex
185: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
186: */
187: public int getBeginIndex() {
188: return begin;
189: }
190:
191: /**
192: * Implements CharacterIterator.getEndIndex() for String.
193: * @see CharacterIterator#getEndIndex
194: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
195: */
196: public int getEndIndex() {
197: return end;
198: }
199:
200: /**
201: * Implements CharacterIterator.getIndex() for String.
202: * @see CharacterIterator#getIndex
203: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
204: */
205: public int getIndex() {
206: return pos;
207: }
208:
209: /**
210: * Compares the equality of two StringCharacterIterator objects.
211: * @param obj the StringCharacterIterator object to be compared with.
212: * @return true if the given obj is the same as this
213: * StringCharacterIterator object; false otherwise.
214: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
215: */
216: public boolean equals(Object obj) {
217: if (this == obj) {
218: return true;
219: }
220: if (!(obj instanceof StringCharacterIterator)) {
221: return false;
222: }
223:
224: StringCharacterIterator that = (StringCharacterIterator) obj;
225:
226: if (hashCode() != that.hashCode()) {
227: return false;
228: }
229: if (!text.equals(that.text)) {
230: return false;
231: }
232: if (pos != that.pos || begin != that.begin || end != that.end) {
233: return false;
234: }
235: return true;
236: }
237:
238: /**
239: * Computes a hashcode for this iterator.
240: * @return A hash code
241: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
242: */
243: public int hashCode() {
244: return text.hashCode() ^ pos ^ begin ^ end;
245: }
246:
247: /**
248: * Creates a copy of this iterator.
249: * @return A copy of this
250: * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
251: */
252: public Object clone() {
253: try {
254: StringCharacterIterator other = (StringCharacterIterator) super
255: .clone();
256: return other;
257: } catch (CloneNotSupportedException e) {
258: throw new IllegalStateException();
259: }
260: }
261:
262: }
263: ///CLOVER:ON
|