jtulach@1334: /* jtulach@1334: * Copyright (c) 1996, 2000, Oracle and/or its affiliates. All rights reserved. jtulach@1334: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1334: * jtulach@1334: * This code is free software; you can redistribute it and/or modify it jtulach@1334: * under the terms of the GNU General Public License version 2 only, as jtulach@1334: * published by the Free Software Foundation. Oracle designates this jtulach@1334: * particular file as subject to the "Classpath" exception as provided jtulach@1334: * by Oracle in the LICENSE file that accompanied this code. jtulach@1334: * jtulach@1334: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1334: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1334: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1334: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1334: * accompanied this code). jtulach@1334: * jtulach@1334: * You should have received a copy of the GNU General Public License version jtulach@1334: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1334: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1334: * jtulach@1334: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1334: * or visit www.oracle.com if you need additional information or have any jtulach@1334: * questions. jtulach@1334: */ jtulach@1334: jtulach@1334: /* jtulach@1334: * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved jtulach@1334: * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved jtulach@1334: * jtulach@1334: * The original version of this source code and documentation jtulach@1334: * is copyrighted and owned by Taligent, Inc., a wholly-owned jtulach@1334: * subsidiary of IBM. These materials are provided under terms jtulach@1334: * of a License Agreement between Taligent and Sun. This technology jtulach@1334: * is protected by multiple US and International patents. jtulach@1334: * jtulach@1334: * This notice and attribution to Taligent may not be removed. jtulach@1334: * Taligent is a registered trademark of Taligent, Inc. jtulach@1334: * jtulach@1334: */ jtulach@1334: jtulach@1334: package java.text; jtulach@1334: jtulach@1334: jtulach@1334: /** jtulach@1334: * This interface defines a protocol for bidirectional iteration over text. jtulach@1334: * The iterator iterates over a bounded sequence of characters. Characters jtulach@1334: * are indexed with values beginning with the value returned by getBeginIndex() and jtulach@1334: * continuing through the value returned by getEndIndex()-1. jtulach@1334: *

jtulach@1334: * Iterators maintain a current character index, whose valid range is from jtulach@1334: * getBeginIndex() to getEndIndex(); the value getEndIndex() is included to allow jtulach@1334: * handling of zero-length text ranges and for historical reasons. jtulach@1334: * The current index can be retrieved by calling getIndex() and set directly jtulach@1334: * by calling setIndex(), first(), and last(). jtulach@1334: *

jtulach@1334: * The methods previous() and next() are used for iteration. They return DONE if jtulach@1334: * they would move outside the range from getBeginIndex() to getEndIndex() -1, jtulach@1334: * signaling that the iterator has reached the end of the sequence. DONE is jtulach@1334: * also returned by other methods to indicate that the current index is jtulach@1334: * outside this range. jtulach@1334: * jtulach@1334: *

Examples:

jtulach@1334: * jtulach@1334: * Traverse the text from start to finish jtulach@1334: *

jtulach@1334:  * public void traverseForward(CharacterIterator iter) {
jtulach@1334:  *     for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
jtulach@1334:  *         processChar(c);
jtulach@1334:  *     }
jtulach@1334:  * }
jtulach@1334:  * 
jtulach@1334: * jtulach@1334: * Traverse the text backwards, from end to start jtulach@1334: *
jtulach@1334:  * public void traverseBackward(CharacterIterator iter) {
jtulach@1334:  *     for(char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {
jtulach@1334:  *         processChar(c);
jtulach@1334:  *     }
jtulach@1334:  * }
jtulach@1334:  * 
jtulach@1334: * jtulach@1334: * Traverse both forward and backward from a given position in the text. jtulach@1334: * Calls to notBoundary() in this example represents some jtulach@1334: * additional stopping criteria. jtulach@1334: *
jtulach@1334:  * public void traverseOut(CharacterIterator iter, int pos) {
jtulach@1334:  *     for (char c = iter.setIndex(pos);
jtulach@1334:  *              c != CharacterIterator.DONE && notBoundary(c);
jtulach@1334:  *              c = iter.next()) {
jtulach@1334:  *     }
jtulach@1334:  *     int end = iter.getIndex();
jtulach@1334:  *     for (char c = iter.setIndex(pos);
jtulach@1334:  *             c != CharacterIterator.DONE && notBoundary(c);
jtulach@1334:  *             c = iter.previous()) {
jtulach@1334:  *     }
jtulach@1334:  *     int start = iter.getIndex();
jtulach@1334:  *     processSection(start, end);
jtulach@1334:  * }
jtulach@1334:  * 
jtulach@1334: * jtulach@1334: * @see StringCharacterIterator jtulach@1334: * @see AttributedCharacterIterator jtulach@1334: */ jtulach@1334: jtulach@1334: public interface CharacterIterator extends Cloneable jtulach@1334: { jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant that is returned when the iterator has reached either the end jtulach@1334: * or the beginning of the text. The value is '\\uFFFF', the "not a jtulach@1334: * character" value which should not occur in any valid Unicode string. jtulach@1334: */ jtulach@1334: public static final char DONE = '\uFFFF'; jtulach@1334: jtulach@1334: /** jtulach@1334: * Sets the position to getBeginIndex() and returns the character at that jtulach@1334: * position. jtulach@1334: * @return the first character in the text, or DONE if the text is empty jtulach@1334: * @see #getBeginIndex() jtulach@1334: */ jtulach@1334: public char first(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) jtulach@1334: * and returns the character at that position. jtulach@1334: * @return the last character in the text, or DONE if the text is empty jtulach@1334: * @see #getEndIndex() jtulach@1334: */ jtulach@1334: public char last(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the character at the current position (as returned by getIndex()). jtulach@1334: * @return the character at the current position or DONE if the current jtulach@1334: * position is off the end of the text. jtulach@1334: * @see #getIndex() jtulach@1334: */ jtulach@1334: public char current(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Increments the iterator's index by one and returns the character jtulach@1334: * at the new index. If the resulting index is greater or equal jtulach@1334: * to getEndIndex(), the current index is reset to getEndIndex() and jtulach@1334: * a value of DONE is returned. jtulach@1334: * @return the character at the new position or DONE if the new jtulach@1334: * position is off the end of the text range. jtulach@1334: */ jtulach@1334: public char next(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Decrements the iterator's index by one and returns the character jtulach@1334: * at the new index. If the current index is getBeginIndex(), the index jtulach@1334: * remains at getBeginIndex() and a value of DONE is returned. jtulach@1334: * @return the character at the new position or DONE if the current jtulach@1334: * position is equal to getBeginIndex(). jtulach@1334: */ jtulach@1334: public char previous(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Sets the position to the specified position in the text and returns that jtulach@1334: * character. jtulach@1334: * @param position the position within the text. Valid values range from jtulach@1334: * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown jtulach@1334: * if an invalid value is supplied. jtulach@1334: * @return the character at the specified position or DONE if the specified position is equal to getEndIndex() jtulach@1334: */ jtulach@1334: public char setIndex(int position); jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the start index of the text. jtulach@1334: * @return the index at which the text begins. jtulach@1334: */ jtulach@1334: public int getBeginIndex(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the end index of the text. This index is the index of the first jtulach@1334: * character following the end of the text. jtulach@1334: * @return the index after the last character in the text jtulach@1334: */ jtulach@1334: public int getEndIndex(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the current index. jtulach@1334: * @return the current index. jtulach@1334: */ jtulach@1334: public int getIndex(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Create a copy of this iterator jtulach@1334: * @return A copy of this jtulach@1334: */ jtulach@1334: public Object clone(); jtulach@1334: jtulach@1334: }