001: /*
002: *
003: * @(#)CharacterIterator.java 1.22 06/10/10
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: * This interface defines a protocol for bidirectional iteration over text.
048: * The iterator iterates over a bounded sequence of characters. Characters
049: * are indexed with values beginning with the value returned by getBeginIndex() and
050: * continuing through the value returned by getEndIndex()-1.
051: * <p>
052: * Iterators maintain a current character index, whose valid range is from
053: * getBeginIndex() to getEndIndex(); the value getEndIndex() is included to allow
054: * handling of zero-length text ranges and for historical reasons.
055: * The current index can be retrieved by calling getIndex() and set directly
056: * by calling setIndex(), first(), and last().
057: * <p>
058: * The methods previous() and next() are used for iteration. They return DONE if
059: * they would move outside the range from getBeginIndex() to getEndIndex() -1,
060: * signaling that the iterator has reached the end of the sequence. DONE is
061: * also returned by other methods to indicate that the current index is
062: * outside this range.
063: *
064: * <P>Examples:<P>
065: *
066: * Traverse the text from start to finish
067: * <pre>
068: * public void traverseForward(CharacterIterator iter) {
069: * for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
070: * processChar(c);
071: * }
072: * }
073: * </pre>
074: *
075: * Traverse the text backwards, from end to start
076: * <pre>
077: * public void traverseBackward(CharacterIterator iter) {
078: * for(char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {
079: * processChar(c);
080: * }
081: * }
082: * </pre>
083: *
084: * Traverse both forward and backward from a given position in the text.
085: * Calls to notBoundary() in this example represents some
086: * additional stopping criteria.
087: * <pre>
088: * public void traverseOut(CharacterIterator iter, int pos) {
089: * for (char c = iter.setIndex(pos);
090: * c != CharacterIterator.DONE && notBoundary(c);
091: * c = iter.next()) {
092: * }
093: * int end = iter.getIndex();
094: * for (char c = iter.setIndex(pos);
095: * c != CharacterIterator.DONE && notBoundary(c);
096: * c = iter.previous()) {
097: * }
098: * int start = iter.getIndex();
099: * processSection(start, end);
100: * }
101: * </pre>
102: *
103: * @see StringCharacterIterator
104: * @see AttributedCharacterIterator
105: */
106:
107: public interface CharacterIterator extends Cloneable {
108:
109: /**
110: * Constant that is returned when the iterator has reached either the end
111: * or the beginning of the text. The value is '\\uFFFF', the "not a
112: * character" value which should not occur in any valid Unicode string.
113: */
114: public static final char DONE = '\uFFFF';
115:
116: /**
117: * Sets the position to getBeginIndex() and returns the character at that
118: * position.
119: * @return the first character in the text, or DONE if the text is empty
120: * @see #getBeginIndex()
121: */
122: public char first();
123:
124: /**
125: * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
126: * and returns the character at that position.
127: * @return the last character in the text, or DONE if the text is empty
128: * @see #getEndIndex()
129: */
130: public char last();
131:
132: /**
133: * Gets the character at the current position (as returned by getIndex()).
134: * @return the character at the current position or DONE if the current
135: * position is off the end of the text.
136: * @see #getIndex()
137: */
138: public char current();
139:
140: /**
141: * Increments the iterator's index by one and returns the character
142: * at the new index. If the resulting index is greater or equal
143: * to getEndIndex(), the current index is reset to getEndIndex() and
144: * a value of DONE is returned.
145: * @return the character at the new position or DONE if the new
146: * position is off the end of the text range.
147: */
148: public char next();
149:
150: /**
151: * Decrements the iterator's index by one and returns the character
152: * at the new index. If the current index is getBeginIndex(), the index
153: * remains at getBeginIndex() and a value of DONE is returned.
154: * @return the character at the new position or DONE if the current
155: * position is equal to getBeginIndex().
156: */
157: public char previous();
158:
159: /**
160: * Sets the position to the specified position in the text and returns that
161: * character.
162: * @param position the position within the text. Valid values range from
163: * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
164: * if an invalid value is supplied.
165: * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
166: */
167: public char setIndex(int position);
168:
169: /**
170: * Returns the start index of the text.
171: * @return the index at which the text begins.
172: */
173: public int getBeginIndex();
174:
175: /**
176: * Returns the end index of the text. This index is the index of the first
177: * character following the end of the text.
178: * @return the index after the last character in the text
179: */
180: public int getEndIndex();
181:
182: /**
183: * Returns the current index.
184: * @return the current index.
185: */
186: public int getIndex();
187:
188: /**
189: * Create a copy of this iterator
190: * @return A copy of this
191: */
192: public Object clone();
193:
194: }
|