org.tbull.util
Class Collections.LazyMapIterator<I,O>

Object
  extended by org.tbull.util.Collections.LazyMapIterator<I,O>
All Implemented Interfaces:
Iterable<O>, Iterator<O>, IterableIterator<O>
Enclosing class:
Collections

public static class Collections.LazyMapIterator<I,O>
extends Object
implements IterableIterator<O>

An iterator that provides the mapLazy functionality.

The lazy iterator guarantees to fetch only one element from the input lists/iterators at each call to next(). Also, an input list/iterator is not queried before the input before it is exhausted.

You can get the lazy iterator by direct instantiation or using one of the mapLazy functions.

TODO: remove

RuntimeExceptions from the input iterators are passed along. Notably, the iterators from the Collections framework throw ConcurrentModificationException when they detect you didn't synchronize properly.

This iterator implements Iterator and Iterable at the same time. In case you chain multiple grep/map invocations as in
mapLazy(mapper2, mapLazy(mapper1, lists))
there arises an ambiguity, because the functions come in an overloaded version for each of them to take as input, so the compiler will not know which version to choose. You can easily resolve the ambiguity by either invoking the iterator() method on the intermediate IterableIterator like
mapLazy(mapper2, mapLazy(mapper1, lists).iterator())
or by casting it to a pure Iterator
mapLazy(mapper2, (Iterator) mapLazy(mapper1, lists)).


Constructor Summary
Collections.LazyMapIterator(Mapper<? super I,? extends O> mapper, Iterable<I>[] lists)
          Constructs a LazyMapIterator that iterates over the given iterables (lists).
Collections.LazyMapIterator(Mapper<? super I,? extends O> mapper, Iterator<I>[] iterators)
          Constructs a LazyMapIterator that fetches from the given iterators.
 
Method Summary
 boolean hasNext()
           
 Iterator<O> iterator()
           
 O next()
           
 void remove()
          Always throws UnsupportedOperationException.
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Collections.LazyMapIterator

public Collections.LazyMapIterator(Mapper<? super I,? extends O> mapper,
                                   Iterable<I>[] lists)
Constructs a LazyMapIterator that iterates over the given iterables (lists).


Collections.LazyMapIterator

public Collections.LazyMapIterator(Mapper<? super I,? extends O> mapper,
                                   Iterator<I>[] iterators)
Constructs a LazyMapIterator that fetches from the given iterators.

Method Detail

hasNext

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

next

public O next()
       throws NoSuchElementException
Specified by:
next in interface Iterator<O>
Throws:
NoSuchElementException

remove

public void remove()
            throws UnsupportedOperationException
Always throws UnsupportedOperationException.

Specified by:
remove in interface Iterator<O>
Throws:
UnsupportedOperationException

iterator

public Iterator<O> iterator()
Specified by:
iterator in interface Iterable<O>