org.tbull.util
Class Collections.LazyGrepIterator<E>

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

public static class Collections.LazyGrepIterator<E>
extends Object
implements IterableIterator<E>

An iterator that provides the grepLazy functionality.

The lazy iterator guarantees to fetch only so many elements from the input lists/iterators as are necessary to compute the result of a call to hasNext() or 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 grepLazy 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
grepLazy(grepper2, grepLazy(grepper1, 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
grepLazy(grepper2, grepLazy(grepper1, lists).iterator())
or by casting it to a pure Iterator
grepLazy(grepper2, (Iterator) grepLazy(grepper1, lists)).


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

Constructor Detail

Collections.LazyGrepIterator

public Collections.LazyGrepIterator(Grepper<? super E> grepper,
                                    Iterable<E>[] lists)
Constructs a LazyGrepIterator that iterates over the given iterables (lists).


Collections.LazyGrepIterator

public Collections.LazyGrepIterator(Grepper<? super E> grepper,
                                    Iterator<E>[] iterators)
Constructs a LazyGrepIterator that fetches from the given iterators.

Method Detail

hasNext

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

next

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

remove

public void remove()
            throws IllegalStateException,
                   UnsupportedOperationException
Always throws UnsupportedOperationException.

Specified by:
remove in interface Iterator<E>
Throws:
IllegalStateException
UnsupportedOperationException

iterator

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