KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > font > CharArrayIterator


1 package java.awt.font;
2
3 import java.text.CharacterIterator JavaDoc;
4
5 class CharArrayIterator implements CharacterIterator JavaDoc {
6
7     private char[] chars;
8     private int pos;
9     private int begin;
10     
11     CharArrayIterator(char[] chars) {
12         
13         reset(chars, 0);
14     }
15     
16     CharArrayIterator(char[] chars, int begin) {
17         
18         reset(chars, begin);
19     }
20     
21     /**
22      * Sets the position to getBeginIndex() and returns the character at that
23      * position.
24      * @return the first character in the text, or DONE if the text is empty
25      * @see getBeginIndex
26      */

27     public char first() {
28         
29         pos = 0;
30         return current();
31     }
32
33     /**
34      * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
35      * and returns the character at that position.
36      * @return the last character in the text, or DONE if the text is empty
37      * @see getEndIndex
38      */

39     public char last() {
40         
41         if (chars.length > 0) {
42             pos = chars.length-1;
43         }
44         else {
45             pos = 0;
46         }
47         return current();
48     }
49     
50     /**
51      * Gets the character at the current position (as returned by getIndex()).
52      * @return the character at the current position or DONE if the current
53      * position is off the end of the text.
54      * @see getIndex
55      */

56     public char current() {
57         
58         if (pos >= 0 && pos < chars.length) {
59             return chars[pos];
60         }
61         else {
62             return DONE;
63         }
64     }
65
66     /**
67      * Increments the iterator's index by one and returns the character
68      * at the new index. If the resulting index is greater or equal
69      * to getEndIndex(), the current index is reset to getEndIndex() and
70      * a value of DONE is returned.
71      * @return the character at the new position or DONE if the new
72      * position is off the end of the text range.
73      */

74     public char next() {
75        
76         if (pos < chars.length-1) {
77             pos++;
78             return chars[pos];
79         }
80         else {
81             pos = chars.length;
82             return DONE;
83         }
84     }
85
86     /**
87      * Decrements the iterator's index by one and returns the character
88      * at the new index. If the current index is getBeginIndex(), the index
89      * remains at getBeginIndex() and a value of DONE is returned.
90      * @return the character at the new position or DONE if the current
91      * position is equal to getBeginIndex().
92      */

93     public char previous() {
94         
95         if (pos > 0) {
96             pos--;
97             return chars[pos];
98         }
99         else {
100             pos = 0;
101             return DONE;
102         }
103     }
104
105     /**
106      * Sets the position to the specified position in the text and returns that
107      * character.
108      * @param position the position within the text. Valid values range from
109      * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
110      * if an invalid value is supplied.
111      * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
112      */

113     public char setIndex(int position) {
114         
115         position -= begin;
116         if (position < 0 || position > chars.length) {
117             throw new IllegalArgumentException JavaDoc("Invalid index");
118         }
119         pos = position;
120         return current();
121     }
122
123     /**
124      * Returns the start index of the text.
125      * @return the index at which the text begins.
126      */

127     public int getBeginIndex() {
128         return begin;
129     }
130
131     /**
132      * Returns the end index of the text. This index is the index of the first
133      * character following the end of the text.
134      * @return the index after the last character in the text
135      */

136     public int getEndIndex() {
137         return begin+chars.length;
138     }
139
140     /**
141      * Returns the current index.
142      * @return the current index.
143      */

144     public int getIndex() {
145         return begin+pos;
146     }
147
148     /**
149      * Create a copy of this iterator
150      * @return A copy of this
151      */

152     public Object JavaDoc clone() {
153         CharArrayIterator JavaDoc c = new CharArrayIterator JavaDoc(chars, begin);
154         c.pos = this.pos;
155         return c;
156     }
157
158     void reset(char[] chars) {
159         reset(chars, 0);
160     }
161     
162     void reset(char[] chars, int begin) {
163         
164         this.chars = chars;
165         this.begin = begin;
166         pos = 0;
167     }
168 }
169
Popular Tags