Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/CharacterIterator.java
38829 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved27* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved28*29* The original version of this source code and documentation30* is copyrighted and owned by Taligent, Inc., a wholly-owned31* subsidiary of IBM. These materials are provided under terms32* of a License Agreement between Taligent and Sun. This technology33* is protected by multiple US and International patents.34*35* This notice and attribution to Taligent may not be removed.36* Taligent is a registered trademark of Taligent, Inc.37*38*/3940package java.text;414243/**44* This interface defines a protocol for bidirectional iteration over text.45* The iterator iterates over a bounded sequence of characters. Characters46* are indexed with values beginning with the value returned by getBeginIndex() and47* continuing through the value returned by getEndIndex()-1.48* <p>49* Iterators maintain a current character index, whose valid range is from50* getBeginIndex() to getEndIndex(); the value getEndIndex() is included to allow51* handling of zero-length text ranges and for historical reasons.52* The current index can be retrieved by calling getIndex() and set directly53* by calling setIndex(), first(), and last().54* <p>55* The methods previous() and next() are used for iteration. They return DONE if56* they would move outside the range from getBeginIndex() to getEndIndex() -1,57* signaling that the iterator has reached the end of the sequence. DONE is58* also returned by other methods to indicate that the current index is59* outside this range.60*61* <P>Examples:<P>62*63* Traverse the text from start to finish64* <pre>{@code65* public void traverseForward(CharacterIterator iter) {66* for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {67* processChar(c);68* }69* }70* }</pre>71*72* Traverse the text backwards, from end to start73* <pre>{@code74* public void traverseBackward(CharacterIterator iter) {75* for(char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {76* processChar(c);77* }78* }79* }</pre>80*81* Traverse both forward and backward from a given position in the text.82* Calls to notBoundary() in this example represents some83* additional stopping criteria.84* <pre>{@code85* public void traverseOut(CharacterIterator iter, int pos) {86* for (char c = iter.setIndex(pos);87* c != CharacterIterator.DONE && notBoundary(c);88* c = iter.next()) {89* }90* int end = iter.getIndex();91* for (char c = iter.setIndex(pos);92* c != CharacterIterator.DONE && notBoundary(c);93* c = iter.previous()) {94* }95* int start = iter.getIndex();96* processSection(start, end);97* }98* }</pre>99*100* @see StringCharacterIterator101* @see AttributedCharacterIterator102*/103104public interface CharacterIterator extends Cloneable105{106107/**108* Constant that is returned when the iterator has reached either the end109* or the beginning of the text. The value is '\\uFFFF', the "not a110* character" value which should not occur in any valid Unicode string.111*/112public static final char DONE = '\uFFFF';113114/**115* Sets the position to getBeginIndex() and returns the character at that116* position.117* @return the first character in the text, or DONE if the text is empty118* @see #getBeginIndex()119*/120public char first();121122/**123* Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)124* and returns the character at that position.125* @return the last character in the text, or DONE if the text is empty126* @see #getEndIndex()127*/128public char last();129130/**131* Gets the character at the current position (as returned by getIndex()).132* @return the character at the current position or DONE if the current133* position is off the end of the text.134* @see #getIndex()135*/136public char current();137138/**139* Increments the iterator's index by one and returns the character140* at the new index. If the resulting index is greater or equal141* to getEndIndex(), the current index is reset to getEndIndex() and142* a value of DONE is returned.143* @return the character at the new position or DONE if the new144* position is off the end of the text range.145*/146public char next();147148/**149* Decrements the iterator's index by one and returns the character150* at the new index. If the current index is getBeginIndex(), the index151* remains at getBeginIndex() and a value of DONE is returned.152* @return the character at the new position or DONE if the current153* position is equal to getBeginIndex().154*/155public char previous();156157/**158* Sets the position to the specified position in the text and returns that159* character.160* @param position the position within the text. Valid values range from161* getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown162* if an invalid value is supplied.163* @return the character at the specified position or DONE if the specified position is equal to getEndIndex()164*/165public char setIndex(int position);166167/**168* Returns the start index of the text.169* @return the index at which the text begins.170*/171public int getBeginIndex();172173/**174* Returns the end index of the text. This index is the index of the first175* character following the end of the text.176* @return the index after the last character in the text177*/178public int getEndIndex();179180/**181* Returns the current index.182* @return the current index.183*/184public int getIndex();185186/**187* Create a copy of this iterator188* @return A copy of this189*/190public Object clone();191192}193194195