com.aliasi.util
Class Iterators.Sequence<E>

java.lang.Object
  extended by com.aliasi.util.Iterators.Sequence<E>
All Implemented Interfaces:
Iterator<E>
Enclosing class:
Iterators

public static class Iterators.Sequence<E>
extends Object
implements Iterator<E>

An Iterators.Sequence iterates over the elements of an ordered sequence of iterators in turn. Each iterator is exhausted before moving to the next. These iterators may be supplied as a pair of iterators, as an array of iterators, or as an iterator of iterators. The sequence iterator delegates calls to next() and remove() to the relevant iterator in the underlying sequence of iterators. For next(), this is the current underlying iterator. For remove(), it's the last iterator whose next() element was called, and it throws an illegal state exception if there isn't one.

Implementation Note: Because of the requirements of Iterator.remove(), a reference to the last iterator is kept, as well as to the current iterator. Otherwise, the sequence iterator will release resources as soon as possible. If the supplied sequence is an array, the elements will not be automatically returned from that array; it is simply wrapped in an instance of Iterators.Array.

Since:
LingPipe1.0
Version:
3.0
Author:
Bob Carpenter

Constructor Summary
Iterators.Sequence(Iterator<? extends E>[] iterators)
          Construct a sequence iterator that calls the iterators in the specified array in the order they are given in the array.
Iterators.Sequence(Iterator<? extends E> iterator1, Iterator<? extends E> iterator2)
          Construct a sequenc iterator that calls the pair of iterators specified in turn.
Iterators.Sequence(Iterator<? extends Iterator<? extends E>> iteratorOfIterators)
          Construct a sequence iterator that calls the iterators returned by the iterator of iterators specified.
 
Method Summary
 boolean hasNext()
          Returns true if this iterator has another element.
 E next()
          Return the next element returned by the next iterator in the iterator sequence.
 void remove()
          Removes the last element returned by this iterator from the collection underlying the iterator from which it was returned.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Iterators.Sequence

public Iterators.Sequence(Iterator<? extends E> iterator1,
                          Iterator<? extends E> iterator2)
Construct a sequenc iterator that calls the pair of iterators specified in turn.

Parameters:
iterator1 - First iterator.
iterator2 - Second iterator.

Iterators.Sequence

public Iterators.Sequence(Iterator<? extends E>[] iterators)
Construct a sequence iterator that calls the iterators in the specified array in the order they are given in the array.

Parameters:
iterators - Sequence of iterators.

Iterators.Sequence

public Iterators.Sequence(Iterator<? extends Iterator<? extends E>> iteratorOfIterators)
Construct a sequence iterator that calls the iterators returned by the iterator of iterators specified. If one of the elements is not an iterator, hasNext() and next() will throw a ClassCastException.

Parameters:
iteratorOfIterators - Iterator of iterators.
Method Detail

hasNext

public boolean hasNext()
Returns true if this iterator has another element. This sequence of iterators has another element if it has another iterator that has another element.

Specified by:
hasNext in interface Iterator<E>
Returns:
true if this sequence of iterators has another iterator with another element.
Throws:
ClassCastException - If an object is returned by the iterator of iterators specified at construction time that is not an iterator.

next

public E next()
Return the next element returned by the next iterator in the iterator sequence.

Specified by:
next in interface Iterator<E>
Returns:
The next object in the iteration.
Throws:
ClassCastException - If an object is returned by the iterator of iterators specified at construction time that is not an iterator.

remove

public void remove()
Removes the last element returned by this iterator from the collection underlying the iterator from which it was returned. The method can only be called once per call to next().

Specified by:
remove in interface Iterator<E>
Throws:
IllegalStateException - If next() has not yet been called, or remove() method has been called after the last call to next().