Java Reference
In-Depth Information
public abstract int depth();
public abstract boolean hasNext();
public abstract Object next();
}
The ComponentIterator class includes a visited set that can keep track of whether
an iterator has already traversed a node in a composite. For example, the aerial shell process
flow starts with a composite make step that contains a rework step that contains, again, the
make step. When you write an iterator for a composite that may contain cycles, you must
ensure that you do not step into an infinite loop.
As with composites, the terminal class for a composite iterator is comparatively simple:
package com.oozinoz.util;
import java.util.*;
public class LeafIterator extends ComponentIterator
{
public LeafIterator(Object node, Set visited)
{
super(node, visited);
}
public int depth()
{
return 0;
}
public boolean hasNext()
{
return !visited.contains(node);
}
public Object next()
{
if (visited.contains(node))
{
return null;
}
visited.add(node);
return node;
}
}
Note that the depth of an iterator is different from the depth of a node in a tree.
The depth of an iterator depends on the depth of its subiterator. Leaf iterators do not have
subiterators, so their depth is always 0. The value of depth() for a CompositeIterator
object is 1 plus the depth of its subiterator:
public int depth()
{
if (subiterator != null)
{
return subiterator.depth() + 1;
}
return 0;
}
Search WWH ::




Custom Search