001: /*
002: *
003: * @(#)StringCharacterIterator.java 1.24 06/10/03
004: *
005: * Portions Copyright 2000-2006 Sun Microsystems, Inc. All Rights
006: * Reserved. Use is subject to license terms.
007: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License version
011: * 2 only, as published by the Free Software Foundation.
012: *
013: * This program is distributed in the hope that it will be useful, but
014: * WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License version 2 for more details (a copy is
017: * included at /legal/license.txt).
018: *
019: * You should have received a copy of the GNU General Public License
020: * version 2 along with this work; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022: * 02110-1301 USA
023: *
024: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
025: * Clara, CA 95054 or visit www.sun.com if you need additional
026: * information or have any questions.
027: */
028:
029: /*
030: * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
031: * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
032: *
033: * The original version of this source code and documentation
034: * is copyrighted and owned by Taligent, Inc., a wholly-owned
035: * subsidiary of IBM. These materials are provided under terms
036: * of a License Agreement between Taligent and Sun. This technology
037: * is protected by multiple US and International patents.
038: *
039: * This notice and attribution to Taligent may not be removed.
040: * Taligent is a registered trademark of Taligent, Inc.
041: *
042: */
043:
044: package java.text;
045:
046: /**
047: * <code>StringCharacterIterator</code> implements the
048: * <code>CharacterIterater</code> protocol for a <code>String</code>.
049: * The <code>StringCharacterIterator</code> class iterates over the
050: * entire <code>String</code>.
051: *
052: * @see CharacterIterator
053: */
054:
055: public final class StringCharacterIterator implements CharacterIterator {
056: private String text;
057: private int begin;
058: private int end;
059: // invariant: begin <= pos <= end
060: private int pos;
061:
062: /**
063: * Constructs an iterator with an initial index of 0.
064: */
065: public StringCharacterIterator(String text) {
066: this (text, 0);
067: }
068:
069: /**
070: * Constructs an iterator with the specified initial index.
071: *
072: * @param text The String to be iterated over
073: * @param pos Initial iterator position
074: */
075: public StringCharacterIterator(String text, int pos) {
076: this (text, 0, text.length(), pos);
077: }
078:
079: /**
080: * Constructs an iterator over the given range of the given string, with the
081: * index set at the specified position.
082: *
083: * @param text The String to be iterated over
084: * @param begin Index of the first character
085: * @param end Index of the character following the last character
086: * @param pos Initial iterator position
087: */
088: public StringCharacterIterator(String text, int begin, int end,
089: int pos) {
090: if (text == null)
091: throw new NullPointerException();
092: this .text = text;
093:
094: if (begin < 0 || begin > end || end > text.length())
095: throw new IllegalArgumentException(
096: "Invalid substring range");
097:
098: if (pos < begin || pos > end)
099: throw new IllegalArgumentException("Invalid position");
100:
101: this .begin = begin;
102: this .end = end;
103: this .pos = pos;
104: }
105:
106: /**
107: * Reset this iterator to point to a new string. This package-visible
108: * method is used by other java.text classes that want to avoid allocating
109: * new StringCharacterIterator objects every time their setText method
110: * is called.
111: *
112: * @param text The String to be iterated over
113: * @since 1.2
114: */
115: public void setText(String text) {
116: if (text == null)
117: throw new NullPointerException();
118: this .text = text;
119: this .begin = 0;
120: this .end = text.length();
121: this .pos = 0;
122: }
123:
124: /**
125: * Implements CharacterIterator.first() for String.
126: * @see CharacterIterator#first
127: */
128: public char first() {
129: pos = begin;
130: return current();
131: }
132:
133: /**
134: * Implements CharacterIterator.last() for String.
135: * @see CharacterIterator#last
136: */
137: public char last() {
138: if (end != begin) {
139: pos = end - 1;
140: } else {
141: pos = end;
142: }
143: return current();
144: }
145:
146: /**
147: * Implements CharacterIterator.setIndex() for String.
148: * @see CharacterIterator#setIndex
149: */
150: public char setIndex(int p) {
151: if (p < begin || p > end)
152: throw new IllegalArgumentException("Invalid index");
153: pos = p;
154: return current();
155: }
156:
157: /**
158: * Implements CharacterIterator.current() for String.
159: * @see CharacterIterator#current
160: */
161: public char current() {
162: if (pos >= begin && pos < end) {
163: return text.charAt(pos);
164: } else {
165: return DONE;
166: }
167: }
168:
169: /**
170: * Implements CharacterIterator.next() for String.
171: * @see CharacterIterator#next
172: */
173: public char next() {
174: if (pos < end - 1) {
175: pos++;
176: return text.charAt(pos);
177: } else {
178: pos = end;
179: return DONE;
180: }
181: }
182:
183: /**
184: * Implements CharacterIterator.previous() for String.
185: * @see CharacterIterator#previous
186: */
187: public char previous() {
188: if (pos > begin) {
189: pos--;
190: return text.charAt(pos);
191: } else {
192: return DONE;
193: }
194: }
195:
196: /**
197: * Implements CharacterIterator.getBeginIndex() for String.
198: * @see CharacterIterator#getBeginIndex
199: */
200: public int getBeginIndex() {
201: return begin;
202: }
203:
204: /**
205: * Implements CharacterIterator.getEndIndex() for String.
206: * @see CharacterIterator#getEndIndex
207: */
208: public int getEndIndex() {
209: return end;
210: }
211:
212: /**
213: * Implements CharacterIterator.getIndex() for String.
214: * @see CharacterIterator#getIndex
215: */
216: public int getIndex() {
217: return pos;
218: }
219:
220: /**
221: * Compares the equality of two StringCharacterIterator objects.
222: * @param obj the StringCharacterIterator object to be compared with.
223: * @return true if the given obj is the same as this
224: * StringCharacterIterator object; false otherwise.
225: */
226: public boolean equals(Object obj) {
227: if (this == obj)
228: return true;
229: if (!(obj instanceof StringCharacterIterator))
230: return false;
231:
232: StringCharacterIterator that = (StringCharacterIterator) obj;
233:
234: if (hashCode() != that.hashCode())
235: return false;
236: if (!text.equals(that.text))
237: return false;
238: if (pos != that.pos || begin != that.begin || end != that.end)
239: return false;
240: return true;
241: }
242:
243: /**
244: * Computes a hashcode for this iterator.
245: * @return A hash code
246: */
247: public int hashCode() {
248: return text.hashCode() ^ pos ^ begin ^ end;
249: }
250:
251: /**
252: * Creates a copy of this iterator.
253: * @return A copy of this
254: */
255: public Object clone() {
256: try {
257: StringCharacterIterator other = (StringCharacterIterator) super
258: .clone();
259: return other;
260: } catch (CloneNotSupportedException e) {
261: throw new InternalError();
262: }
263: }
264:
265: }
|