KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > StringCharacterIterator


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7
8
9 // NOTE: This class is identical to java.text.StringCharacterIterator
10
// in JDK 1.2. It's copied here because the JDK 1.1 version of
11
// StringCharacterIterator has a bug that prevents it from working
12
// right with RuleBasedBreakIterator. This class is unnecessary
13
// when using RuleBasedBreakIterator with JDK 1.2.
14

15 package com.ibm.icu.text;
16 import java.text.CharacterIterator JavaDoc;
17
18 /**
19  * <code>StringCharacterIterator</code> implements the
20  * <code>CharacterIterater</code> protocol for a <code>String</code>.
21  * The <code>StringCharacterIterator</code> class iterates over the
22  * entire <code>String</code>.
23  *
24  * @see CharacterIterator
25  * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
26  */

27 ///CLOVER:OFF
28
public final class StringCharacterIterator implements CharacterIterator JavaDoc
29 {
30     private String JavaDoc text;
31     private int begin;
32     private int end;
33     // invariant: begin <= pos <= end
34
private int pos;
35
36     /**
37      * Constructs an iterator with an initial index of 0.
38      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
39      */

40     public StringCharacterIterator(String JavaDoc text)
41     {
42         this(text, 0);
43     }
44
45     /**
46      * Constructs an iterator with the specified initial index.
47      *
48      * @param text The String to be iterated over
49      * @param pos Initial iterator position
50      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
51      */

52     public StringCharacterIterator(String JavaDoc text, int pos)
53     {
54     this(text, 0, text.length(), pos);
55     }
56
57     /**
58      * Constructs an iterator over the given range of the given string, with the
59      * index set at the specified position.
60      *
61      * @param text The String to be iterated over
62      * @param begin Index of the first character
63      * @param end Index of the character following the last character
64      * @param pos Initial iterator position
65      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
66      */

67     public StringCharacterIterator(String JavaDoc text, int begin, int end, int pos) {
68         if (text == null) {
69             throw new NullPointerException JavaDoc();
70         }
71         this.text = text;
72
73         if (begin < 0 || begin > end || end > text.length()) {
74             throw new IllegalArgumentException JavaDoc("Invalid substring range");
75         }
76
77         if (pos < begin || pos > end) {
78             throw new IllegalArgumentException JavaDoc("Invalid position");
79         }
80
81         this.begin = begin;
82         this.end = end;
83         this.pos = pos;
84     }
85
86     /**
87      * Reset this iterator to point to a new string. This package-visible
88      * method is used by other java.text classes that want to avoid allocating
89      * new StringCharacterIterator objects every time their setText method
90      * is called.
91      *
92      * @param text The String to be iterated over
93      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
94      */

95     public void setText(String JavaDoc text) {
96         if (text == null) {
97             throw new NullPointerException JavaDoc();
98         }
99         this.text = text;
100         this.begin = 0;
101         this.end = text.length();
102         this.pos = 0;
103     }
104
105     /**
106      * Implements CharacterIterator.first() for String.
107      * @see CharacterIterator#first
108      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
109      */

110     public char first()
111     {
112         pos = begin;
113         return current();
114     }
115
116     /**
117      * Implements CharacterIterator.last() for String.
118      * @see CharacterIterator#last
119      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
120      */

121     public char last()
122     {
123         if (end != begin) {
124             pos = end - 1;
125         } else {
126             pos = end;
127         }
128         return current();
129      }
130
131     /**
132      * Implements CharacterIterator.setIndex() for String.
133      * @see CharacterIterator#setIndex
134      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
135      */

136     public char setIndex(int p)
137     {
138     if (p < begin || p > end) {
139             throw new IllegalArgumentException JavaDoc("Invalid index");
140     }
141         pos = p;
142         return current();
143     }
144
145     /**
146      * Implements CharacterIterator.current() for String.
147      * @see CharacterIterator#current
148      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
149      */

150     public char current()
151     {
152         if (pos >= begin && pos < end) {
153             return text.charAt(pos);
154         }
155         else {
156             return DONE;
157         }
158     }
159
160     /**
161      * Implements CharacterIterator.next() for String.
162      * @see CharacterIterator#next
163      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
164      */

165     public char next()
166     {
167         if (pos < end - 1) {
168             pos++;
169             return text.charAt(pos);
170         }
171         else {
172             pos = end;
173             return DONE;
174         }
175     }
176
177     /**
178      * Implements CharacterIterator.previous() for String.
179      * @see CharacterIterator#previous
180      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
181      */

182     public char previous()
183     {
184         if (pos > begin) {
185             pos--;
186             return text.charAt(pos);
187         }
188         else {
189             return DONE;
190         }
191     }
192
193     /**
194      * Implements CharacterIterator.getBeginIndex() for String.
195      * @see CharacterIterator#getBeginIndex
196      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
197      */

198     public int getBeginIndex()
199     {
200         return begin;
201     }
202
203     /**
204      * Implements CharacterIterator.getEndIndex() for String.
205      * @see CharacterIterator#getEndIndex
206      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
207      */

208     public int getEndIndex()
209     {
210         return end;
211     }
212
213     /**
214      * Implements CharacterIterator.getIndex() for String.
215      * @see CharacterIterator#getIndex
216      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
217      */

218     public int getIndex()
219     {
220         return pos;
221     }
222
223     /**
224      * Compares the equality of two StringCharacterIterator objects.
225      * @param obj the StringCharacterIterator object to be compared with.
226      * @return true if the given obj is the same as this
227      * StringCharacterIterator object; false otherwise.
228      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
229      */

230     public boolean equals(Object JavaDoc obj)
231     {
232         if (this == obj) {
233             return true;
234         }
235         if (!(obj instanceof StringCharacterIterator)) {
236             return false;
237         }
238
239         StringCharacterIterator that = (StringCharacterIterator) obj;
240
241         if (hashCode() != that.hashCode()) {
242             return false;
243         }
244         if (!text.equals(that.text)) {
245             return false;
246         }
247         if (pos != that.pos || begin != that.begin || end != that.end) {
248             return false;
249         }
250         return true;
251     }
252
253     /**
254      * Computes a hashcode for this iterator.
255      * @return A hash code
256      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
257      */

258     public int hashCode()
259     {
260         return text.hashCode() ^ pos ^ begin ^ end;
261     }
262
263     /**
264      * Creates a copy of this iterator.
265      * @return A copy of this
266      * @deprecated ICU 2.4. Use java.text.StringCharacterIterator instead.
267      */

268     public Object JavaDoc clone()
269     {
270         try {
271             StringCharacterIterator other
272             = (StringCharacterIterator) super.clone();
273             return other;
274         }
275         catch (CloneNotSupportedException JavaDoc e) {
276             throw new IllegalStateException JavaDoc();
277         }
278     }
279
280 }
281 ///CLOVER:ON
282
Popular Tags