forked from wupeixuan/JDKSourceCode1.8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharArrayIterator.java
More file actions
193 lines (166 loc) · 4.41 KB
/
CharArrayIterator.java
File metadata and controls
193 lines (166 loc) · 4.41 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
/*
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.awt.font;
import java.text.CharacterIterator;
class CharArrayIterator implements CharacterIterator {
private char[] chars;
private int pos;
private int begin;
CharArrayIterator(char[] chars) {
reset(chars, 0);
}
CharArrayIterator(char[] chars, int begin) {
reset(chars, begin);
}
/**
* Sets the position to getBeginIndex() and returns the character at that
* position.
* @return the first character in the text, or DONE if the text is empty
* @see getBeginIndex
*/
public char first() {
pos = 0;
return current();
}
/**
* Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
* and returns the character at that position.
* @return the last character in the text, or DONE if the text is empty
* @see getEndIndex
*/
public char last() {
if (chars.length > 0) {
pos = chars.length-1;
}
else {
pos = 0;
}
return current();
}
/**
* Gets the character at the current position (as returned by getIndex()).
* @return the character at the current position or DONE if the current
* position is off the end of the text.
* @see getIndex
*/
public char current() {
if (pos >= 0 && pos < chars.length) {
return chars[pos];
}
else {
return DONE;
}
}
/**
* Increments the iterator's index by one and returns the character
* at the new index. If the resulting index is greater or equal
* to getEndIndex(), the current index is reset to getEndIndex() and
* a value of DONE is returned.
* @return the character at the new position or DONE if the new
* position is off the end of the text range.
*/
public char next() {
if (pos < chars.length-1) {
pos++;
return chars[pos];
}
else {
pos = chars.length;
return DONE;
}
}
/**
* Decrements the iterator's index by one and returns the character
* at the new index. If the current index is getBeginIndex(), the index
* remains at getBeginIndex() and a value of DONE is returned.
* @return the character at the new position or DONE if the current
* position is equal to getBeginIndex().
*/
public char previous() {
if (pos > 0) {
pos--;
return chars[pos];
}
else {
pos = 0;
return DONE;
}
}
/**
* Sets the position to the specified position in the text and returns that
* character.
* @param position the position within the text. Valid values range from
* getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
* if an invalid value is supplied.
* @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
*/
public char setIndex(int position) {
position -= begin;
if (position < 0 || position > chars.length) {
throw new IllegalArgumentException("Invalid index");
}
pos = position;
return current();
}
/**
* Returns the start index of the text.
* @return the index at which the text begins.
*/
public int getBeginIndex() {
return begin;
}
/**
* Returns the end index of the text. This index is the index of the first
* character following the end of the text.
* @return the index after the last character in the text
*/
public int getEndIndex() {
return begin+chars.length;
}
/**
* Returns the current index.
* @return the current index.
*/
public int getIndex() {
return begin+pos;
}
/**
* Create a copy of this iterator
* @return A copy of this
*/
public Object clone() {
CharArrayIterator c = new CharArrayIterator(chars, begin);
c.pos = this.pos;
return c;
}
void reset(char[] chars) {
reset(chars, 0);
}
void reset(char[] chars, int begin) {
this.chars = chars;
this.begin = begin;
pos = 0;
}
}