|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.apache.commons.collections.iterators.ObjectGraphIterator
public class ObjectGraphIterator
An Iterator that can traverse multiple iterators down an object graph.
This iterator can extract multiple objects from a complex tree-like object graph. The iteration starts from a single root object. It uses aTransformer to extract the iterators and elements.
Its main benefit is that no intermediate List is created.
For example, consider an object graph:
|- Branch -- Leaf
| \- Leaf
|- Tree | /- Leaf
| |- Branch -- Leaf
Forest | \- Leaf
| |- Branch -- Leaf
| | \- Leaf
|- Tree | /- Leaf
|- Branch -- Leaf
|- Branch -- Leaf
The following Transformer, used in this class, will extract all
the Leaf objects without creating a combined intermediate list:
public Object transform(Object input) {
if (input instanceof Forest) {
return ((Forest) input).treeIterator();
}
if (input instanceof Tree) {
return ((Tree) input).branchIterator();
}
if (input instanceof Branch) {
return ((Branch) input).leafIterator();
}
if (input instanceof Leaf) {
return input;
}
throw new ClassCastException();
}
Internally, iteration starts from the root object. When next is called,
the transformer is called to examine the object. The transformer will return
either an iterator or an object. If the object is an Iterator, the next element
from that iterator is obtained and the process repeats. If the element is an object
it is returned.
Under many circumstances, linking Iterators together in this manner is
more efficient (and convenient) than using nested for loops to extract a list.
Note: this class is not type-safe in Java 1.5 (no generics).
| Field Summary | |
|---|---|
protected Iterator |
currentIterator
The current iterator |
protected Object |
currentValue
The current value |
protected boolean |
hasNext
Whether there is another element in the iteration |
protected Iterator |
lastUsedIterator
The last used iterator, needed for remove() |
protected Object |
root
The root object in the tree |
protected ArrayStack |
stack
The stack of iterators |
protected Transformer |
transformer
The transformer to use |
| Constructor Summary | |
|---|---|
ObjectGraphIterator(Iterator rootIterator)
Constructs a ObjectGraphIterator that will handle an iterator of iterators. |
|
ObjectGraphIterator(Object root,
Transformer transformer)
Constructs an ObjectGraphIterator using a root object and transformer. |
|
| Method Summary | |
|---|---|
protected void |
findNext(Object value)
Finds the next object in the iteration given any start object. |
protected void |
findNextByIterator(Iterator iterator)
Finds the next object in the iteration given an iterator. |
boolean |
hasNext()
Checks whether there are any more elements in the iteration to obtain. |
Object |
next()
Gets the next element of the iteration. |
void |
remove()
Removes from the underlying collection the last element returned. |
protected void |
updateCurrentIterator()
Loops around the iterators to find the next value to return. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected final ArrayStack stack
protected Object root
protected Transformer transformer
protected boolean hasNext
protected Iterator currentIterator
protected Object currentValue
protected Iterator lastUsedIterator
| Constructor Detail |
|---|
public ObjectGraphIterator(Object root,
Transformer transformer)
root - the root object, null will result in an empty iteratortransformer - the transformer to use, null will use a no effect transformerpublic ObjectGraphIterator(Iterator rootIterator)
rootIterator - the root iterator, null will result in an empty iterator| Method Detail |
|---|
protected void updateCurrentIterator()
protected void findNext(Object value)
value - the value to start fromprotected void findNextByIterator(Iterator iterator)
iterator - the iterator to start frompublic boolean hasNext()
hasNext in interface Iteratorpublic Object next()
next in interface IteratorNoSuchElementException - if all the Iterators are exhaustedpublic void remove()
remove in interface IteratorUnsupportedOperationException - if the remove operator is not supported by the underlying Iterator
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.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||