Documentation  › java.text  › CharacterIterator
 
 


  CharacterIterator
  public abstract interface

  Inherits From:   none
  Conforms To:   Cloneable
  Declared In:   java.text


Interface Description
 
This interface defines a protocol for bidirectional iteration over text. The iterator iterates over a bounded sequence of characters. Characters are indexed with values beginning with the value returned by getBeginIndex() and continuing through the value returned by getEndIndex()-1.

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

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

Examples:

Traverse the text from start to finish

 
public void traverseForward(CharacterIterator iter) { 
for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { 
processChar(c); 
} 
} 
Traverse the text backwards, from end to start
 
public void traverseBackward(CharacterIterator iter) { 
for(char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) { 
processChar(c); 
} 
} 
Traverse both forward and backward from a given position in the text. Calls to notBoundary() in this example represents some additional stopping criteria.
 
public void traverseOut(CharacterIterator iter, int pos) { 
for (char c = iter.setIndex(pos); 
c != CharacterIterator.DONE && notBoundary(c); 
c = iter.next()) { 
} 
int end = iter.getIndex(); 
for (char c = iter.setIndex(pos); 
c != CharacterIterator.DONE && notBoundary(c); 
c = iter.previous()) { 
} 
int start = iter.getIndex(); 
processSection(start, end); 
} 


Class Variables
 
DONE
public static final char

Constant that is returned when the iterator has reached either the end or the beginning of the text. The value is '\\uFFFF', the "not a character" value which should not occur in any valid Unicode string.


Instance Variables
 
None declared in this interface.


Constructors
 
None declared in this interface.


Class Methods
 
None declared in this interface.


Instance Methods
 
clone
public abstract Object clone( )

Create a copy of this iterator


current
public abstract char current( )

Gets the character at the current position (as returned by getIndex()).


first
public abstract char first( )

Sets the position to getBeginIndex() and returns the character at that position.


getBeginIndex
public abstract int getBeginIndex( )

Returns the start index of the text.


getEndIndex
public abstract int getEndIndex( )

Returns the end index of the text. This index is the index of the first character following the end of the text.


getIndex
public abstract int getIndex( )

Returns the current index.


last
public abstract char last( )

Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position.


next
public abstract char next( )

Increments the iterator's index by one and returns the character at the new index. If the resulting index is greater or equal to getEndIndex(), the current index is reset to getEndIndex() and a value of DONE is returned.


previous
public abstract char previous( )

Decrements the iterator's index by one and returns the character at the new index. If the current index is getBeginIndex(), the index remains at getBeginIndex() and a value of DONE is returned.


setIndex
public abstract char setIndex( int position )

Sets the position to the specified position in the text and returns that character.


Known Implementations
 
AttributedCharacterIterator, Segment, StringCharacterIterator



 
 
  dydoc
  3/10/05