001 /*
002 * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.font;
027
028 import java.text.CharacterIterator;
029
030 class CharArrayIterator implements CharacterIterator {
031
032 private char[] chars;
033 private int pos;
034 private int begin;
035
036 CharArrayIterator(char[] chars) {
037
038 reset(chars, 0);
039 }
040
041 CharArrayIterator(char[] chars, int begin) {
042
043 reset(chars, begin);
044 }
045
046 /**
047 * Sets the position to getBeginIndex() and returns the character at that
048 * position.
049 * @return the first character in the text, or DONE if the text is empty
050 * @see getBeginIndex
051 */
052 public char first() {
053
054 pos = 0;
055 return current();
056 }
057
058 /**
059 * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
060 * and returns the character at that position.
061 * @return the last character in the text, or DONE if the text is empty
062 * @see getEndIndex
063 */
064 public char last() {
065
066 if (chars.length > 0) {
067 pos = chars.length - 1;
068 } else {
069 pos = 0;
070 }
071 return current();
072 }
073
074 /**
075 * Gets the character at the current position (as returned by getIndex()).
076 * @return the character at the current position or DONE if the current
077 * position is off the end of the text.
078 * @see getIndex
079 */
080 public char current() {
081
082 if (pos >= 0 && pos < chars.length) {
083 return chars[pos];
084 } else {
085 return DONE;
086 }
087 }
088
089 /**
090 * Increments the iterator's index by one and returns the character
091 * at the new index. If the resulting index is greater or equal
092 * to getEndIndex(), the current index is reset to getEndIndex() and
093 * a value of DONE is returned.
094 * @return the character at the new position or DONE if the new
095 * position is off the end of the text range.
096 */
097 public char next() {
098
099 if (pos < chars.length - 1) {
100 pos++;
101 return chars[pos];
102 } else {
103 pos = chars.length;
104 return DONE;
105 }
106 }
107
108 /**
109 * Decrements the iterator's index by one and returns the character
110 * at the new index. If the current index is getBeginIndex(), the index
111 * remains at getBeginIndex() and a value of DONE is returned.
112 * @return the character at the new position or DONE if the current
113 * position is equal to getBeginIndex().
114 */
115 public char previous() {
116
117 if (pos > 0) {
118 pos--;
119 return chars[pos];
120 } else {
121 pos = 0;
122 return DONE;
123 }
124 }
125
126 /**
127 * Sets the position to the specified position in the text and returns that
128 * character.
129 * @param position the position within the text. Valid values range from
130 * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
131 * if an invalid value is supplied.
132 * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
133 */
134 public char setIndex(int position) {
135
136 position -= begin;
137 if (position < 0 || position > chars.length) {
138 throw new IllegalArgumentException("Invalid index");
139 }
140 pos = position;
141 return current();
142 }
143
144 /**
145 * Returns the start index of the text.
146 * @return the index at which the text begins.
147 */
148 public int getBeginIndex() {
149 return begin;
150 }
151
152 /**
153 * Returns the end index of the text. This index is the index of the first
154 * character following the end of the text.
155 * @return the index after the last character in the text
156 */
157 public int getEndIndex() {
158 return begin + chars.length;
159 }
160
161 /**
162 * Returns the current index.
163 * @return the current index.
164 */
165 public int getIndex() {
166 return begin + pos;
167 }
168
169 /**
170 * Create a copy of this iterator
171 * @return A copy of this
172 */
173 public Object clone() {
174 CharArrayIterator c = new CharArrayIterator(chars, begin);
175 c.pos = this .pos;
176 return c;
177 }
178
179 void reset(char[] chars) {
180 reset(chars, 0);
181 }
182
183 void reset(char[] chars, int begin) {
184
185 this .chars = chars;
186 this .begin = begin;
187 pos = 0;
188 }
189 }
|