|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.myarch.treeiterator.TreeIterator
Iterates over the tree. Preorder traversal is used, i.e.,
node is processed before its children.
Depth of traversal can be limited by setMaxDepth
.
Branches can be traversed selectively using skipChildren()
.
Unlike java.util.Iterator
, the tree iterator points directly at the element (tree node),
not BETWEEN elements. It means that, for example, if next()
is
called before processing, root won't be processed.
current()
can be used to obtain the current element at
any time (except right after moveToChildren()
was called).
Usage:
Instantiation:
TreeIterator tIterator = new TreeIterator ( rootNode , new DOMAdapter() );
Tree traversal INCLUDING the root:
for( Object node = tIterator.current(); node!=null; node=tIterator.next() ){
// processing...
}
while( (node=(Node)tIterator.next()) != null ) {
// processing...
}
hasNext()
:
while( tIterator.hasNext() ) {
Node node=(Node)tIterator.next();
// processing...
}
hasNext()
implementation is not very efficient for a
tree structure from the performance standpoint, so refrain from using it for large trees.
Fields inherited from interface com.myarch.treeiterator.TreeConstants |
DEPTH_UNLIMITED |
Constructor Summary | |
TreeIterator(java.lang.Object root,
TreeNodeAdapter nodeAdapter)
Creates iterator and sets the root of a tree and tree adapter. |
|
TreeIterator(java.lang.Object root,
TreeNodeAdapter nodeAdapter,
int maxDepth)
Creates iterator and sets the root of a tree, tree adapter and depth of the traversal. |
Method Summary | |
java.lang.Object |
current()
Returns the current node. |
int |
getDepth()
Returns the depth of the level being traversed. |
int |
getIndex()
Returns index of the of the current node among siblings. |
int |
getMaxDepth()
Returns the maximum depth of the traversal. |
boolean |
hasChildren()
Returns true if current node has children and iterator
is allowed to traverse them. |
boolean |
hasNext()
Returns true if not all elements of the tree have
been traversed.
|
boolean |
isMaxDepth()
Returns true if the maximum allowed level is reached. |
void |
moveToChildren()
Moves iterator to the children of the node regardless of current level depth setting. |
void |
moveToChildren(int depth)
Moves iterator to the children of the node regardless of current level depth setting. |
java.lang.Object |
next()
Returns the next element of a tree. |
void |
remove()
This method is defined to comply with Iterator but it is not supported. |
void |
setDepth(int depth)
Sets the initial depth value. |
void |
setLevelDepth(int depth)
Same with moveToChildren . |
void |
setMaxDepth(int maxDepth)
Sets the maximum depth of the traversal. |
void |
setRoot(java.lang.Object node)
Sets the root of the tree. |
void |
skipChildren()
After this method is called, next() will skip children of the current node
and move to the next sibling instead. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public TreeIterator(java.lang.Object root, TreeNodeAdapter nodeAdapter)
root
- root of the tree; note that next()
moves to the first child, not to
the rootnodeAdapter
- tree node adapterpublic TreeIterator(java.lang.Object root, TreeNodeAdapter nodeAdapter, int maxDepth)
root
- root of the treenodeAdapter
- tree node adaptermaxDepth
- depth of the traversal; 0 -- traverse only the root; 1-- traverse root and
childrenl; 2-- traverse root, children and grandchildren and so forthMethod Detail |
public void remove() throws java.lang.UnsupportedOperationException
Iterator
but it is not supported.remove
in interface java.util.Iterator
java.lang.UnsupportedOperationException
- whenever it is calledpublic void setRoot(java.lang.Object node)
root
- root of the tree; note that next()
moves to the first child, not to
the rootpublic java.lang.Object current()
next()
call moves current to the next node.public void setMaxDepth(int maxDepth)
maxDepth
- depth of the traversal; 0 -- traverse only the root; 1-- traverse root and
childrenl; 2-- traverse root, children and grandchildren and so forthpublic int getMaxDepth()
setMaxDepth()
.public void moveToChildren()
Iterator doesn't have the current node after this call.
It is positioned before the first child.
next()
must be called to position it to the first child.
The entire branch will be processed. Previous
max depth is restored after the branch is processed.
public void moveToChildren(int depth)
Iterator doesn't have current node after this call.
It is positioned before the first child.
Next()
must be called to position to the first child.
It is the alternative and more sophisticated approach to using skipChildren()
.
setLevelDepth
does the same thing.
depth
- New depth that applies to all children of the current node. Previous
depth is restored after the branch is processed. This depth setting is differeent
from the maximum depth of
the traversal as it only applies to the current branch. It overrides the maximum depth previously set
by setMaxDepth()
.public void setLevelDepth(int depth)
moveToChildren
.public void skipChildren()
next()
will skip children of the current node
and move to the next sibling instead.public boolean isMaxDepth()
true
if the maximum allowed level is reached.setMaxDepth()
public int getDepth()
public void setDepth(int depth)
depth
- initial depthpublic int getIndex()
public boolean hasNext()
true
if not all elements of the tree have
been traversed.
It is not recommended to use this method, since this check is
performed anyway inside next()
. It is more efficient to use
next()
and check for null
.
hasNext
in interface java.util.Iterator
true
if end of tree is not reachedpublic boolean hasChildren()
true
if current node has children and iterator
is allowed to traverse them.true
if current node has childrenpublic java.lang.Object next()
java.util.Iterator
it does not
raise exception when the end of the tree is reached, null
is
returned instead.next
in interface java.util.Iterator
null
when there is no more nodes to traverse
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |