FileDocCategorySizeDatePackage
StringCharacterIterator.javaAPI DocJava SE 5 API6678Fri Aug 26 14:57:22 BST 2005java.text

StringCharacterIterator

public final class StringCharacterIterator extends Object implements CharacterIterator
StringCharacterIterator implements the CharacterIterater protocol for a String. The StringCharacterIterator class iterates over the entire String.
see
CharacterIterator

Fields Summary
private String
text
private int
begin
private int
end
private int
pos
Constructors Summary
public StringCharacterIterator(String text)
Constructs an iterator with an initial index of 0.

        this(text, 0);
    
public StringCharacterIterator(String text, int pos)
Constructs an iterator with the specified initial index.

param
text The String to be iterated over
param
pos Initial iterator position

    this(text, 0, text.length(), pos);
    
public StringCharacterIterator(String text, int begin, int end, int pos)
Constructs an iterator over the given range of the given string, with the index set at the specified position.

param
text The String to be iterated over
param
begin Index of the first character
param
end Index of the character following the last character
param
pos Initial iterator position

        if (text == null)
            throw new NullPointerException();
        this.text = text;

        if (begin < 0 || begin > end || end > text.length())
            throw new IllegalArgumentException("Invalid substring range");

        if (pos < begin || pos > end)
            throw new IllegalArgumentException("Invalid position");

        this.begin = begin;
        this.end = end;
        this.pos = pos;
    
Methods Summary
public java.lang.Objectclone()
Creates a copy of this iterator.

return
A copy of this

        try {
            StringCharacterIterator other
            = (StringCharacterIterator) super.clone();
            return other;
        }
        catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    
public charcurrent()
Implements CharacterIterator.current() for String.

see
CharacterIterator#current

        if (pos >= begin && pos < end) {
            return text.charAt(pos);
        }
        else {
            return DONE;
        }
    
public booleanequals(java.lang.Object obj)
Compares the equality of two StringCharacterIterator objects.

param
obj the StringCharacterIterator object to be compared with.
return
true if the given obj is the same as this StringCharacterIterator object; false otherwise.

        if (this == obj)
            return true;
        if (!(obj instanceof StringCharacterIterator))
            return false;

        StringCharacterIterator that = (StringCharacterIterator) obj;

        if (hashCode() != that.hashCode())
            return false;
        if (!text.equals(that.text))
            return false;
        if (pos != that.pos || begin != that.begin || end != that.end)
            return false;
        return true;
    
public charfirst()
Implements CharacterIterator.first() for String.

see
CharacterIterator#first

        pos = begin;
        return current();
    
public intgetBeginIndex()
Implements CharacterIterator.getBeginIndex() for String.

see
CharacterIterator#getBeginIndex

        return begin;
    
public intgetEndIndex()
Implements CharacterIterator.getEndIndex() for String.

see
CharacterIterator#getEndIndex

        return end;
    
public intgetIndex()
Implements CharacterIterator.getIndex() for String.

see
CharacterIterator#getIndex

        return pos;
    
public inthashCode()
Computes a hashcode for this iterator.

return
A hash code

        return text.hashCode() ^ pos ^ begin ^ end;
    
public charlast()
Implements CharacterIterator.last() for String.

see
CharacterIterator#last

        if (end != begin) {
            pos = end - 1;
        } else {
            pos = end;
        }
        return current();
     
public charnext()
Implements CharacterIterator.next() for String.

see
CharacterIterator#next

        if (pos < end - 1) {
            pos++;
            return text.charAt(pos);
        }
        else {
            pos = end;
            return DONE;
        }
    
public charprevious()
Implements CharacterIterator.previous() for String.

see
CharacterIterator#previous

        if (pos > begin) {
            pos--;
            return text.charAt(pos);
        }
        else {
            return DONE;
        }
    
public charsetIndex(int p)
Implements CharacterIterator.setIndex() for String.

see
CharacterIterator#setIndex

    if (p < begin || p > end)
            throw new IllegalArgumentException("Invalid index");
        pos = p;
        return current();
    
public voidsetText(java.lang.String text)
Reset this iterator to point to a new string. This package-visible method is used by other java.text classes that want to avoid allocating new StringCharacterIterator objects every time their setText method is called.

param
text The String to be iterated over
since
1.2

        if (text == null)
            throw new NullPointerException();
        this.text = text;
        this.begin = 0;
        this.end = text.length();
        this.pos = 0;