001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.text;
019:
020: /**
021: * StringCharacterIterator is an implementation of CharacterIterator for
022: * Strings.
023: */
024: public final class StringCharacterIterator implements CharacterIterator {
025:
026: String string;
027:
028: int start, end, offset;
029:
030: /**
031: * Constructs a new StringCharacterIterator on the specified String. The
032: * begin and current indexes are set to the beginning of the String, the end
033: * index is set to the length of the String.
034: *
035: * @param value
036: * the new source String to iterate
037: */
038: public StringCharacterIterator(String value) {
039: string = value;
040: start = offset = 0;
041: end = string.length();
042: }
043:
044: /**
045: * Constructs a new StringCharacterIterator on the specified String with the
046: * current index set to the specified value. The begin index is set to the
047: * beginning of the String, the end index is set to the length of the String.
048: *
049: * @param value
050: * the new source String to iterate
051: * @param location
052: * the current index
053: *
054: * @exception IllegalArgumentException
055: * when the current index is less than zero or greater than
056: * the length of the String
057: */
058: public StringCharacterIterator(String value, int location) {
059: string = value;
060: start = 0;
061: end = string.length();
062: if (location < 0 || location > end) {
063: throw new IllegalArgumentException();
064: }
065: offset = location;
066: }
067:
068: /**
069: * Constructs a new StringCharacterIterator on the specified String with the
070: * begin, end and current index set to the specified values.
071: *
072: * @param value
073: * the new source String to iterate
074: * @param start
075: * the index of the first character to iterate
076: * @param end
077: * the index one past the last character to iterate
078: * @param location
079: * the current index
080: *
081: * @exception IllegalArgumentException
082: * when the begin index is less than zero, the end index is
083: * greater than the String length, the begin index is greater
084: * than the end index, the current index is less than the
085: * begin index or greater than the end index
086: */
087: public StringCharacterIterator(String value, int start, int end,
088: int location) {
089: string = value;
090: if (start < 0 || end > string.length() || start > end
091: || location < start || location > end) {
092: throw new IllegalArgumentException();
093: }
094: this .start = start;
095: this .end = end;
096: offset = location;
097: }
098:
099: /**
100: * Answers a new StringCharacterIterator with the same source String, begin,
101: * end, and current index as this StringCharacterIterator.
102: *
103: * @return a shallow copy of this StringCharacterIterator
104: *
105: * @see java.lang.Cloneable
106: */
107: @Override
108: public Object clone() {
109: try {
110: return super .clone();
111: } catch (CloneNotSupportedException e) {
112: return null;
113: }
114: }
115:
116: /**
117: * Answers the character at the current index in the source String.
118: *
119: * @return the current character, or DONE if the current index is past the
120: * end
121: */
122: public char current() {
123: if (offset == end) {
124: return DONE;
125: }
126: return string.charAt(offset);
127: }
128:
129: /**
130: * Compares the specified object to this StringCharacterIterator and answer
131: * if they are equal. The object must be a StringCharacterIterator iterating
132: * over the same sequence of characters with the same index.
133: *
134: * @param object
135: * the object to compare with this object
136: * @return true if the specified object is equal to this
137: * StringCharacterIterator, false otherwise
138: *
139: * @see #hashCode
140: */
141: @Override
142: public boolean equals(Object object) {
143: if (!(object instanceof StringCharacterIterator)) {
144: return false;
145: }
146: StringCharacterIterator it = (StringCharacterIterator) object;
147: return string.equals(it.string) && start == it.start
148: && end == it.end && offset == it.offset;
149: }
150:
151: /**
152: * Sets the current position to the begin index and answers the character at
153: * the begin index.
154: *
155: * @return the character at the begin index
156: */
157: public char first() {
158: if (start == end) {
159: return DONE;
160: }
161: offset = start;
162: return string.charAt(offset);
163: }
164:
165: /**
166: * Answers the begin index in the source String.
167: *
168: * @return the index of the first character to iterate
169: */
170: public int getBeginIndex() {
171: return start;
172: }
173:
174: /**
175: * Answers the end index in the source String.
176: *
177: * @return the index one past the last character to iterate
178: */
179: public int getEndIndex() {
180: return end;
181: }
182:
183: /**
184: * Answers the current index in the source String.
185: *
186: * @return the current index
187: */
188: public int getIndex() {
189: return offset;
190: }
191:
192: /**
193: * Answers an integer hash code for the receiver. Objects which are equal
194: * answer the same value for this method.
195: *
196: * @return the receiver's hash
197: *
198: * @see #equals
199: */
200: @Override
201: public int hashCode() {
202: return string.hashCode() + start + end + offset;
203: }
204:
205: /**
206: * Sets the current position to the end index - 1 and answers the character
207: * at the current position.
208: *
209: * @return the character before the end index
210: */
211: public char last() {
212: if (start == end) {
213: return DONE;
214: }
215: offset = end - 1;
216: return string.charAt(offset);
217: }
218:
219: /**
220: * Increments the current index and returns the character at the new index.
221: *
222: * @return the character at the next index, or DONE if the next index is
223: * past the end
224: */
225: public char next() {
226: if (offset >= (end - 1)) {
227: offset = end;
228: return DONE;
229: }
230: return string.charAt(++offset);
231: }
232:
233: /**
234: * Decrements the current index and returns the character at the new index.
235: *
236: * @return the character at the previous index, or DONE if the previous
237: * index is past the beginning
238: */
239: public char previous() {
240: if (offset == start) {
241: return DONE;
242: }
243: return string.charAt(--offset);
244: }
245:
246: /**
247: * Sets the current index in the source String.
248: *
249: * @return the character at the new index, or DONE if the index is past the
250: * end
251: *
252: * @exception IllegalArgumentException
253: * when the new index is less than the begin index or greater
254: * than the end index
255: */
256: public char setIndex(int location) {
257: if (location < start || location > end) {
258: throw new IllegalArgumentException();
259: }
260: offset = location;
261: if (offset == end) {
262: return DONE;
263: }
264: return string.charAt(offset);
265: }
266:
267: /**
268: * Sets the source String to iterate. The begin and end positions are set to
269: * the start and end of this String.
270: *
271: * @param value
272: * the new source String
273: */
274: public void setText(String value) {
275: string = value;
276: start = offset = 0;
277: end = value.length();
278: }
279: }
|