KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > text > StringCharacterIterator


1 /*
2  * @(#)StringCharacterIterator.java 1.21 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
10  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
11  *
12  * The original version of this source code and documentation
13  * is copyrighted and owned by Taligent, Inc., a wholly-owned
14  * subsidiary of IBM. These materials are provided under terms
15  * of a License Agreement between Taligent and Sun. This technology
16  * is protected by multiple US and International patents.
17  *
18  * This notice and attribution to Taligent may not be removed.
19  * Taligent is a registered trademark of Taligent, Inc.
20  *
21  */

22
23 package java.text;
24
25 /**
26  * <code>StringCharacterIterator</code> implements the
27  * <code>CharacterIterater</code> protocol for a <code>String</code>.
28  * The <code>StringCharacterIterator</code> class iterates over the
29  * entire <code>String</code>.
30  *
31  * @see CharacterIterator
32  */

33
34 public final class StringCharacterIterator implements CharacterIterator JavaDoc
35 {
36     private String JavaDoc text;
37     private int begin;
38     private int end;
39     // invariant: begin <= pos <= end
40
private int pos;
41
42     /**
43      * Constructs an iterator with an initial index of 0.
44      */

45     public StringCharacterIterator(String JavaDoc text)
46     {
47         this(text, 0);
48     }
49
50     /**
51      * Constructs an iterator with the specified initial index.
52      *
53      * @param text The String to be iterated over
54      * @param pos Initial iterator position
55      */

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

70     public StringCharacterIterator(String JavaDoc text, int begin, int end, int pos) {
71         if (text == null)
72             throw new NullPointerException JavaDoc();
73         this.text = text;
74
75         if (begin < 0 || begin > end || end > text.length())
76             throw new IllegalArgumentException JavaDoc("Invalid substring range");
77
78         if (pos < begin || pos > end)
79             throw new IllegalArgumentException JavaDoc("Invalid position");
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      * @since 1.2
94      */

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

108     public char first()
109     {
110         pos = begin;
111         return current();
112     }
113
114     /**
115      * Implements CharacterIterator.last() for String.
116      * @see CharacterIterator#last
117      */

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     /**
129      * Implements CharacterIterator.setIndex() for String.
130      * @see CharacterIterator#setIndex
131      */

132     public char setIndex(int p)
133     {
134     if (p < begin || p > end)
135             throw new IllegalArgumentException JavaDoc("Invalid index");
136         pos = p;
137         return current();
138     }
139
140     /**
141      * Implements CharacterIterator.current() for String.
142      * @see CharacterIterator#current
143      */

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     /**
155      * Implements CharacterIterator.next() for String.
156      * @see CharacterIterator#next
157      */

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     /**
171      * Implements CharacterIterator.previous() for String.
172      * @see CharacterIterator#previous
173      */

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     /**
186      * Implements CharacterIterator.getBeginIndex() for String.
187      * @see CharacterIterator#getBeginIndex
188      */

189     public int getBeginIndex()
190     {
191         return begin;
192     }
193
194     /**
195      * Implements CharacterIterator.getEndIndex() for String.
196      * @see CharacterIterator#getEndIndex
197      */

198     public int getEndIndex()
199     {
200         return end;
201     }
202
203     /**
204      * Implements CharacterIterator.getIndex() for String.
205      * @see CharacterIterator#getIndex
206      */

207     public int getIndex()
208     {
209         return pos;
210     }
211
212     /**
213      * Compares the equality of two StringCharacterIterator objects.
214      * @param obj the StringCharacterIterator object to be compared with.
215      * @return true if the given obj is the same as this
216      * StringCharacterIterator object; false otherwise.
217      */

218     public boolean equals(Object JavaDoc obj)
219     {
220         if (this == obj)
221             return true;
222         if (!(obj instanceof StringCharacterIterator JavaDoc))
223             return false;
224
225         StringCharacterIterator JavaDoc that = (StringCharacterIterator JavaDoc) 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     /**
237      * Computes a hashcode for this iterator.
238      * @return A hash code
239      */

240     public int hashCode()
241     {
242         return text.hashCode() ^ pos ^ begin ^ end;
243     }
244
245     /**
246      * Creates a copy of this iterator.
247      * @return A copy of this
248      */

249     public Object JavaDoc clone()
250     {
251         try {
252             StringCharacterIterator JavaDoc other
253             = (StringCharacterIterator JavaDoc) super.clone();
254             return other;
255         }
256         catch (CloneNotSupportedException JavaDoc e) {
257             throw new InternalError JavaDoc();
258         }
259     }
260
261 }
262
Popular Tags