Path: blob/master/src/java.base/share/classes/java/text/CharacterIterator.java
41152 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* @since 1.1101* @see StringCharacterIterator102* @see AttributedCharacterIterator103*/104105public interface CharacterIterator extends Cloneable106{107108/**109* Constant that is returned when the iterator has reached either the end110* or the beginning of the text. The value is '\\uFFFF', the "not a111* character" value which should not occur in any valid Unicode string.112*/113public static final char DONE = '\uFFFF';114115/**116* Sets the position to getBeginIndex() and returns the character at that117* position.118* @return the first character in the text, or DONE if the text is empty119* @see #getBeginIndex()120*/121public char first();122123/**124* Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)125* and returns the character at that position.126* @return the last character in the text, or DONE if the text is empty127* @see #getEndIndex()128*/129public char last();130131/**132* Gets the character at the current position (as returned by getIndex()).133* @return the character at the current position or DONE if the current134* position is off the end of the text.135* @see #getIndex()136*/137public char current();138139/**140* Increments the iterator's index by one and returns the character141* at the new index. If the resulting index is greater or equal142* to getEndIndex(), the current index is reset to getEndIndex() and143* a value of DONE is returned.144* @return the character at the new position or DONE if the new145* position is off the end of the text range.146*/147public char next();148149/**150* Decrements the iterator's index by one and returns the character151* at the new index. If the current index is getBeginIndex(), the index152* remains at getBeginIndex() and a value of DONE is returned.153* @return the character at the new position or DONE if the current154* position is equal to getBeginIndex().155*/156public char previous();157158/**159* Sets the position to the specified position in the text and returns that160* character.161* @param position the position within the text. Valid values range from162* getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown163* if an invalid value is supplied.164* @return the character at the specified position or DONE if the specified position is equal to getEndIndex()165*/166public char setIndex(int position);167168/**169* Returns the start index of the text.170* @return the index at which the text begins.171*/172public int getBeginIndex();173174/**175* Returns the end index of the text. This index is the index of the first176* character following the end of the text.177* @return the index after the last character in the text178*/179public int getEndIndex();180181/**182* Returns the current index.183* @return the current index.184*/185public int getIndex();186187/**188* Create a copy of this iterator189* @return A copy of this190*/191public Object clone();192193}194195196