-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCharacterIterator.java
More file actions
279 lines (253 loc) · 6.55 KB
/
StringCharacterIterator.java
File metadata and controls
279 lines (253 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (c) 1996, 2002, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
/*
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
*
* The original version of this source code and documentation
* is copyrighted and owned by Taligent, Inc., a wholly-owned
* subsidiary of IBM. These materials are provided under terms
* of a License Agreement between Taligent and Sun. This technology
* is protected by multiple US and International patents.
*
* This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
*/
package java.text;
/**
* <code>StringCharacterIterator</code> implements the
* <code>CharacterIterator</code> protocol for a <code>String</code>.
* The <code>StringCharacterIterator</code> class iterates over the
* entire <code>String</code>.
*
* @see CharacterIterator
*/
public final class StringCharacterIterator implements CharacterIterator
{
private String text;
private int begin;
private int end;
// invariant: begin <= pos <= end
private int pos;
/**
* Constructs an iterator with an initial index of 0.
*/
public StringCharacterIterator(String text)
{
this(text, 0);
}
/**
* Constructs an iterator with the specified initial index.
*
* @param text The String to be iterated over
* @param pos Initial iterator position
*/
public StringCharacterIterator(String text, int pos)
{
this(text, 0, text.length(), pos);
}
/**
* Constructs an iterator over the given range of the given string, with the
* index set at the specified position.
*
* @param text The String to be iterated over
* @param begin Index of the first character
* @param end Index of the character following the last character
* @param pos Initial iterator position
*/
public StringCharacterIterator(String text, int begin, int end, int pos) {
if (text == null)
throw new NullPointerException();
this.text = text;
if (begin < 0 || begin > end || end > text.length())
throw new IllegalArgumentException("Invalid substring range");
if (pos < begin || pos > end)
throw new IllegalArgumentException("Invalid position");
this.begin = begin;
this.end = end;
this.pos = pos;
}
/**
* Reset this iterator to point to a new string. This package-visible
* method is used by other java.text classes that want to avoid allocating
* new StringCharacterIterator objects every time their setText method
* is called.
*
* @param text The String to be iterated over
* @since 1.2
*/
public void setText(String text) {
if (text == null)
throw new NullPointerException();
this.text = text;
this.begin = 0;
this.end = text.length();
this.pos = 0;
}
/**
* Implements CharacterIterator.first() for String.
* @see CharacterIterator#first
*/
public char first()
{
pos = begin;
return current();
}
/**
* Implements CharacterIterator.last() for String.
* @see CharacterIterator#last
*/
public char last()
{
if (end != begin) {
pos = end - 1;
} else {
pos = end;
}
return current();
}
/**
* Implements CharacterIterator.setIndex() for String.
* @see CharacterIterator#setIndex
*/
public char setIndex(int p)
{
if (p < begin || p > end)
throw new IllegalArgumentException("Invalid index");
pos = p;
return current();
}
/**
* Implements CharacterIterator.current() for String.
* @see CharacterIterator#current
*/
public char current()
{
if (pos >= begin && pos < end) {
return text.charAt(pos);
}
else {
return DONE;
}
}
/**
* Implements CharacterIterator.next() for String.
* @see CharacterIterator#next
*/
public char next()
{
if (pos < end - 1) {
pos++;
return text.charAt(pos);
}
else {
pos = end;
return DONE;
}
}
/**
* Implements CharacterIterator.previous() for String.
* @see CharacterIterator#previous
*/
public char previous()
{
if (pos > begin) {
pos--;
return text.charAt(pos);
}
else {
return DONE;
}
}
/**
* Implements CharacterIterator.getBeginIndex() for String.
* @see CharacterIterator#getBeginIndex
*/
public int getBeginIndex()
{
return begin;
}
/**
* Implements CharacterIterator.getEndIndex() for String.
* @see CharacterIterator#getEndIndex
*/
public int getEndIndex()
{
return end;
}
/**
* Implements CharacterIterator.getIndex() for String.
* @see CharacterIterator#getIndex
*/
public int getIndex()
{
return pos;
}
/**
* Compares the equality of two StringCharacterIterator objects.
* @param obj the StringCharacterIterator object to be compared with.
* @return true if the given obj is the same as this
* StringCharacterIterator object; false otherwise.
*/
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (!(obj instanceof StringCharacterIterator))
return false;
StringCharacterIterator that = (StringCharacterIterator) obj;
if (hashCode() != that.hashCode())
return false;
if (!text.equals(that.text))
return false;
if (pos != that.pos || begin != that.begin || end != that.end)
return false;
return true;
}
/**
* Computes a hashcode for this iterator.
* @return A hash code
*/
public int hashCode()
{
return text.hashCode() ^ pos ^ begin ^ end;
}
/**
* Creates a copy of this iterator.
* @return A copy of this
*/
public Object clone()
{
try {
StringCharacterIterator other
= (StringCharacterIterator) super.clone();
return other;
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}