Class LazyIteratorChain<E>
- java.lang.Object
-
- org.apache.commons.collections4.iterators.LazyIteratorChain<E>
-
- All Implemented Interfaces:
java.util.Iterator<E>
public abstract class LazyIteratorChain<E> extends java.lang.Object implements java.util.Iterator<E>
An LazyIteratorChain is an Iterator that wraps a number of Iterators in a lazy manner.This class makes multiple iterators look like one to the caller. When any method from the Iterator interface is called, the LazyIteratorChain will delegate to a single underlying Iterator. The LazyIteratorChain will invoke the Iterators in sequence until all Iterators are exhausted.
The Iterators are provided by
nextIterator(int)
which has to be overridden by sub-classes and allows to lazily create the Iterators as they are accessed:return new LazyIteratorChain<String>() { protected Iterator<String> nextIterator(int count) { return count == 1 ? Arrays.asList("foo", "bar").iterator() : null; } };
Once the inner Iterator's
Iterator.hasNext()
method returns false,nextIterator(int)
will be called to obtain another iterator, and so on untilnextIterator(int)
returns null, indicating that the chain is exhausted.NOTE: The LazyIteratorChain may contain no iterators. In this case the class will function as an empty iterator.
- Since:
- 4.0
-
-
Constructor Summary
Constructors Constructor Description LazyIteratorChain()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
hasNext()
Return true if any Iterator in the chain has a remaining element.E
next()
Returns the next element of the current Iteratorvoid
remove()
Removes from the underlying collection the last element returned by the Iterator.
-
-
-
Method Detail
-
hasNext
public boolean hasNext()
Return true if any Iterator in the chain has a remaining element.- Specified by:
hasNext
in interfacejava.util.Iterator<E>
- Returns:
- true if elements remain
-
next
public E next()
Returns the next element of the current Iterator- Specified by:
next
in interfacejava.util.Iterator<E>
- Returns:
- element from the current Iterator
- Throws:
java.util.NoSuchElementException
- if all the Iterators are exhausted
-
remove
public void remove()
Removes from the underlying collection the last element returned by the Iterator.As with next() and hasNext(), this method calls remove() on the underlying Iterator. Therefore, this method may throw an UnsupportedOperationException if the underlying Iterator does not support this method.
- Specified by:
remove
in interfacejava.util.Iterator<E>
- Throws:
java.lang.UnsupportedOperationException
- if the remove operator is not supported by the underlying Iteratorjava.lang.IllegalStateException
- if the next method has not yet been called, or the remove method has already been called after the last call to the next method.
-
-