com.google.common.collect
Class AbstractIterator<T>

java.lang.Object
  extended by com.google.common.collect.AbstractIterator<T>
All Implemented Interfaces:
Iterator<T>
Direct Known Subclasses:
AbstractRemovableIterator

public abstract class AbstractIterator<T>
extends Object
implements Iterator<T>

This class provides a skeletal implementation of the Iterator interface, to make this interface easier to implement for certain types of data sources.

Iterator requires its implementations to support querying the end-of-data status without changing the iterator's state, using the hasNext() method. But many data sources, such as Reader.read()), do not expose this information; the only way to discover whether there is any data left is by trying to retrieve it. These types of data sources are ordinarily difficult to write iterators for. But using this class, one must implement only the computeNext() method, and invoke the endOfData() method when appropriate.

Another example is an iterator that skips over null elements in a backing iterator. This could be implemented as:

  public static Iterator<String> skipNulls(
       final Iterator<String> in) {
     return new AbstractIterator<String>() {
       protected String computeNext() {
         while (in.hasNext()) {
           String s = in.next();
           if (s != null) {
             return s;
           }
         }
         endOfData();
         return null; // return value ignored
       }
     };
   }

Author:
Kevin Bourrillion

Constructor Summary
AbstractIterator()
           
 
Method Summary
protected abstract  T computeNext()
          Returns the next element.
protected  void endOfData()
          Implementors of computeNext must invoke this method when there is no data left.
 boolean hasNext()
           
 T next()
           
 void remove()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractIterator

public AbstractIterator()
Method Detail

computeNext

protected abstract T computeNext()
Returns the next element. Note: the implementor must call endOfData() when it has reached the end of the data. Failure to do so could result in an infinite loop.

This class invokes computeNext() during the caller's initial invocation of hasNext() or next, and on the first invocation of hasNext or next that follows each successful call to next. Once the implementor either invokes endOfData or throws any exception, computeNext is guaranteed to never be called again.

If this method throws an exception, it will propagate outward to the hasNext or next invocation that invoked this method. Any further attempts to use the iterator will result in IllegalStateException.

Returns:
the next element if there was one. null is a valid element value. If endOfData was called during execution, the return value will be ignored.

endOfData

protected final void endOfData()
Implementors of computeNext must invoke this method when there is no data left.


hasNext

public boolean hasNext()
Specified by:
hasNext in interface Iterator<T>

next

public T next()
Specified by:
next in interface Iterator<T>

remove

public void remove()
Specified by:
remove in interface Iterator<T>