public class ObjectGraphIterator
extends java.lang.Object
implements java.util.Iterator
This iterator can extract multiple objects from a complex tree-like object graph.
The iteration starts from a single root object.
It uses a Transformer
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 -- LeafThe 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.
Constructor and Description |
---|
ObjectGraphIterator(java.util.Iterator rootIterator)
Constructs a ObjectGraphIterator that will handle an iterator of iterators.
|
ObjectGraphIterator(java.lang.Object root,
Transformer transformer)
Constructs an ObjectGraphIterator using a root object and transformer.
|
Modifier and Type | Method and Description |
---|---|
boolean |
hasNext()
Checks whether there are any more elements in the iteration to obtain.
|
java.lang.Object |
next()
Gets the next element of the iteration.
|
void |
remove()
Removes from the underlying collection the last element returned.
|
public ObjectGraphIterator(java.lang.Object root, Transformer transformer)
The root object can be an iterator, in which case it will be immediately looped around.
root
- the root object, null will result in an empty iteratortransformer
- the transformer to use, null will use a no effect transformerpublic ObjectGraphIterator(java.util.Iterator rootIterator)
This constructor exists for convenience to emphasise that this class can be used to iterate over nested iterators. That is to say that the iterator passed in here contains other iterators, which may in turn contain further iterators.
rootIterator
- the root iterator, null will result in an empty iteratorpublic boolean hasNext()
hasNext
in interface java.util.Iterator
public java.lang.Object next()
next
in interface java.util.Iterator
java.util.NoSuchElementException
- if all the Iterators are exhaustedpublic void remove()
This method calls remove() on the underlying Iterator and it may throw an UnsupportedOperationException if the underlying Iterator does not support this method.
remove
in interface java.util.Iterator
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."Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"