Class Iterators.Abstract<A>

java.lang.Object
io.atlassian.fugue.Iterators.Unmodifiable<A>
io.atlassian.fugue.Iterators.Abstract<A>
All Implemented Interfaces:
Iterator<A>
Direct Known Subclasses:
Iterables.CollectingIterable.Iter, Iterables.Cycle.Iter, Iterables.Drop.Iter, Iterables.Join.Iter, Iterables.Memoizer.Iter, Iterables.MergeSortedIterable.Iter, Iterables.Take.Iter, Iterables.UnfoldingIterable.Iter
Enclosing class:
Iterators

abstract static class Iterators.Abstract<A> extends Iterators.Unmodifiable<A>
A template implementation of the Iterator interface, so clients can more easily implement Iterator for some patterns of iteration.

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



  public static Iterator<String> filterNulls(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.

This class is a re-implentation of the Guava AbstractIterator class.

Since:
3.0
  • Field Details

  • Constructor Details

    • Abstract

      protected Abstract()
      Constructor for use by subclasses.
  • Method Details

    • computeNext

      protected abstract A computeNext()
      The next element.

      Note: the implementation must call endOfData() when there are no elements left in the iteration. Failure to do so could result in an infinite loop.

    • endOfData

      protected final A endOfData()
      Implementations of computeNext() must invoke this method when there are no elements left in the iteration.
      Returns:
      null; a convenience so your computeNext implementation can use the simple statement return endOfData();
    • hasNext

      public final boolean hasNext()
    • tryToComputeNext

      private boolean tryToComputeNext()
    • next

      public final A next()