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
until nextIterator(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.
| Modifier and Type | Field and Description |
|---|---|
| private int | callCounter
The number of times |
| private boolean | chainExhausted
Indicates that the Iterator chain has been exhausted. |
| private Iterator | currentIterator
The current iterator. |
| private Iterator | lastUsedIterator
The "last used" Iterator is the Iterator upon which next() or hasNext() was most recently called used for the remove() operation only. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if elements remainImplements java. |
| public E | Returns: element from the current IteratorImplements java. |
| protected abstract Iterator | Returns: the next iterator, or null if there are no more.the number of time this method has been called (starts with 1) count)Gets the next iterator after the previous one has been exhausted. |
| public void | remove()
Overrides default java. |
| private void | updateCurrentIterator()
Updates the current iterator field to ensure that the current Iterator is not exhausted. |
| callCounter | back to summary |
|---|---|
| private int callCounter The number of times | |
| chainExhausted | back to summary |
|---|---|
| private boolean chainExhausted Indicates that the Iterator chain has been exhausted. | |
| currentIterator | back to summary |
|---|---|
| private Iterator<? extends E> currentIterator The current iterator. | |
| lastUsedIterator | back to summary |
|---|---|
| private Iterator<? extends E> lastUsedIterator The "last used" Iterator is the Iterator upon which next() or hasNext() was most recently called used for the remove() operation only. | |
| LazyIteratorChain | back to summary |
|---|---|
| public LazyIteratorChain() | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Return true if any Iterator in the chain has a remaining element.
| |
| next | back to summary |
|---|---|
| public E next() Implements java. Returns the next element of the current Iterator
| |
| nextIterator | back to summary |
|---|---|
| protected abstract Iterator Gets the next iterator after the previous one has been exhausted. This method MUST return null when there are no more iterators.
| |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. 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.
| |
| updateCurrentIterator | back to summary |
|---|---|
| private void updateCurrentIterator() Updates the current iterator field to ensure that the current Iterator is not exhausted. | |