Class AbstractIterator<T>

  • All Implemented Interfaces:
    java.util.Iterator<T>
    Direct Known Subclasses:
    FileIOUtils.BurnOnCloseFileIterator, FileIOUtils.FileLineDifferenceIterator

    @GwtCompatible
    public abstract class AbstractIterator<T>
    extends UnmodifiableIterator<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;
               }
             }
             return endOfData();
           }
         };
       }

    This class supports iterators that include null elements.

    Since:
    2.0 (imported from Google Collections Library)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean hasNext()  
      T next()  
      T peek()
      Returns the next element in the iteration without advancing the iteration, according to the contract of PeekingIterator.peek().
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Method Detail

      • hasNext

        public final boolean hasNext()
      • next

        public final T next()
      • peek

        public final T peek()
        Returns the next element in the iteration without advancing the iteration, according to the contract of PeekingIterator.peek().

        Implementations of AbstractIterator that wish to expose this functionality should implement PeekingIterator.