public class IteratorChain extends Object implements Iterator
This class makes multiple iterators look like one to the caller When any method from the Iterator interface is called, the IteratorChain will delegate to a single underlying Iterator. The IteratorChain will invoke the Iterators in sequence until all Iterators are exhausted.
Under many circumstances, linking Iterators together in this manner is more efficient (and convenient) than reading out the contents of each Iterator into a List and creating a new Iterator.
Calling a method that adds new Iterator after a method in the Iterator interface has been called will result in an UnsupportedOperationException. Subclasses should take care to not alter the underlying List of Iterators.
NOTE: As from version 3.0, the IteratorChain may contain no iterators. In this case the class will function as an empty iterator.
Modifier and Type | Field and Description |
---|---|
protected Iterator |
currentIterator
The current iterator
|
protected int |
currentIteratorIndex
The index of the current iterator
|
protected boolean |
isLocked
ComparatorChain is "locked" after the first time compare(Object,Object)
is called
|
protected List |
iteratorChain
The chain of iterators
|
protected Iterator |
lastUsedIterator
The "last used" Iterator is the Iterator upon which next() or hasNext()
was most recently called used for the remove() operation only
|
Constructor and Description |
---|
IteratorChain()
Construct an IteratorChain with no Iterators.
|
IteratorChain(Collection iterators)
Constructs a new
IteratorChain over the collection of
iterators. |
IteratorChain(Iterator iterator)
Construct an IteratorChain with a single Iterator.
|
IteratorChain(Iterator[] iterators)
Constructs a new
IteratorChain over the array of
iterators. |
IteratorChain(Iterator a,
Iterator b)
Constructs a new
IteratorChain over the two given
iterators. |
Modifier and Type | Method and Description |
---|---|
void |
addIterator(Iterator iterator)
Add an Iterator to the end of the chain
|
List |
getIterators()
Get the list of Iterators (unmodifiable)
|
boolean |
hasNext()
Return true if any Iterator in the IteratorChain has a remaining element.
|
boolean |
isLocked()
Determine if modifications can still be made to the IteratorChain.
|
Object |
next()
Returns the next Object of the current Iterator
|
void |
remove()
Removes from the underlying collection the last element returned by the
Iterator.
|
void |
setIterator(int index,
Iterator iterator)
Set the Iterator at the given index
|
int |
size()
Number of Iterators in the current IteratorChain.
|
protected void |
updateCurrentIterator()
Updates the current iterator field to ensure that the current Iterator is
not exhausted
|
protected final List iteratorChain
protected int currentIteratorIndex
protected Iterator currentIterator
protected Iterator lastUsedIterator
protected boolean isLocked
public IteratorChain()
You will normally use addIterator(Iterator)
to add some
iterators after using this constructor.
public IteratorChain(Iterator iterator)
iterator
- first Iterator in the IteratorChainNullPointerException
- if the iterator is nullpublic IteratorChain(Iterator a, Iterator b)
IteratorChain
over the two given
iterators.a
- the first child iteratorb
- the second child iteratorNullPointerException
- if either iterator is nullpublic IteratorChain(Iterator[] iterators)
IteratorChain
over the array of
iterators.iterators
- the array of iteratorsNullPointerException
- if iterators array is or contains nullpublic IteratorChain(Collection iterators)
IteratorChain
over the collection of
iterators.iterators
- the collection of iteratorsNullPointerException
- if iterators collection is or contains nullClassCastException
- if iterators collection doesn't contain an iteratorpublic void addIterator(Iterator iterator)
iterator
- Iterator to addIllegalStateException
- if I've already started iteratingNullPointerException
- if the iterator is nullpublic void setIterator(int index, Iterator iterator) throws IndexOutOfBoundsException
index
- index of the Iterator to replaceiterator
- Iterator to place at the given indexIndexOutOfBoundsException
- if index < 0 or index > size()IllegalStateException
- if I've already started iteratingNullPointerException
- if the iterator is nullpublic List getIterators()
public int size()
public boolean isLocked()
protected void updateCurrentIterator()
public boolean hasNext()
public Object next()
next
in interface Iterator
NoSuchElementException
- if all the Iterators are exhaustedpublic void remove()
remove
in interface Iterator
UnsupportedOperationException
- if the remove operator is not supported by the underlying
IteratorIllegalStateException
- 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.Copyright © 2001-2015 JBoss by Red Hat. All Rights Reserved.