Package generic.util

Class FlattenedIterator<O,I>

java.lang.Object
generic.util.FlattenedIterator<O,I>
Type Parameters:
O - the type of elements in the outer iterator
I - the type of elements in the inner and flattened iterators
All Implemented Interfaces:
Iterator<I>
Direct Known Subclasses:
AddressIteratorAdapter

public class FlattenedIterator<O,I> extends Object implements Iterator<I>
Given an "outer" iterator and a mapping from its elements to "inner" iterators, this is a flattened iterator over elements from the inner iterators.
  • Field Details

    • outer

      protected final Iterator<O> outer
    • innerFactory

      protected final Function<O,Iterator<? extends I>> innerFactory
    • inner

      protected Iterator<? extends I> inner
    • preppedInner

      protected Iterator<? extends I> preppedInner
  • Constructor Details

  • Method Details

    • start

      public static <O, I> Iterator<I> start(Iterator<O> outer, Function<O,Iterator<? extends I>> innerFactory)
      Create a flattened iterator

      This iterates over each element of outer and applies the given innerFactory to generate an "inner" iterator. The returned iterator will produce elements from the inner iterators as if concatentated. This is essentially a flat-map operation on iterators. Note the innerFactory may return null to skip an outer element.

      Type Parameters:
      O - the type of elements in the outer iterator
      I - the type of elements in the inner and flattened iterators
      Parameters:
      outer - the outer iterator
      innerFactory - a mapping from outer elements to inner iterators
      Returns:
      the flattened iterator
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<O>
    • next

      public I next()
      Specified by:
      next in interface Iterator<O>
    • remove

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