com.springsource.util.common
Interface Tree<V>

Type Parameters:
V - type of values in tree nodes
All Known Implementing Classes:
ThreadSafeArrayListTree

public interface Tree<V>

Tree is a value with an ordered collection of subtrees of the same type as the main tree.

Concurrent Semantics
Implementations of this interface may or may not be thread safe.

Since:
jersey
Author:
Glyn Normington

Nested Class Summary
static interface Tree.ExceptionThrowingTreeVisitor<V,E extends java.lang.Exception>
          An ExceptionThrowingTreeVisitor is used to visit a tree when the Tree.ExceptionThrowingTreeVisitor.visit(Tree) implementation needs to be able to throw a checked Exception.
static interface Tree.TreeVisitor<V>
          Tree.TreeVisitor is an interface used to visit a tree and, at the option of the visitor, its children and so on recursively.
 
Method Summary
 Tree<V> addChild(Tree<V> child)
          Adds a new child tree to this node's children.
 java.util.List<Tree<V>> getChildren()
          Returns a list of this tree's children (not copies of the children).
 Tree<V> getParent()
          Returns this tree's parent.
 V getValue()
          Returns the tree's value.
 boolean removeChild(Tree<V> child)
          Removes the first occurrence of the given child tree from this node's children.
 int size()
          Returns the number of nodes in the tree.
<E extends java.lang.Exception>
void
visit(Tree.ExceptionThrowingTreeVisitor<V,E> visitor)
          Traverse this Tree in preorder (see below) and call the visit method of the given Tree.ExceptionThrowingTreeVisitor at each node.
 void visit(Tree.TreeVisitor<V> visitor)
          Traverse this Tree in preorder (see below) and call the visit method of the given Tree.TreeVisitor at each node.
 

Method Detail

getValue

V getValue()
Returns the tree's value. If there is no value associated with this tree, returns null.

Returns:
the value, which may be null

getChildren

java.util.List<Tree<V>> getChildren()
Returns a list of this tree's children (not copies of the children). If the tree has no children, returns an empty list. Never returns null .

Returns:
this tree's children

addChild

Tree<V> addChild(Tree<V> child)
Adds a new child tree to this node's children. The child tree is copied, although its values are not.

Parameters:
child - the child tree to add
Returns:
the copy of the child tree

removeChild

boolean removeChild(Tree<V> child)
Removes the first occurrence of the given child tree from this node's children. Returns true if the child was found and removed, otherwise false.

Parameters:
child - the child tree to remove
Returns:
true if the child tree was removed successfully, otherwise false.
See Also:
List.remove(java.lang.Object)

getParent

Tree<V> getParent()
Returns this tree's parent. If this tree does not have a parent, returns null.

Returns:
this tree's parent

visit

void visit(Tree.TreeVisitor<V> visitor)
Traverse this Tree in preorder (see below) and call the visit method of the given Tree.TreeVisitor at each node. The visitor determines whether the children of each visited tree should also be visited.

Preorder traversal visits the tree and then visits, in preorder, each child of the tree.

Parameters:
visitor - a Tree.TreeVisitor

visit

<E extends java.lang.Exception> void visit(Tree.ExceptionThrowingTreeVisitor<V,E> visitor)
           throws E extends java.lang.Exception
Traverse this Tree in preorder (see below) and call the visit method of the given Tree.ExceptionThrowingTreeVisitor at each node. The visitor determines whether the children of each visited tree should also be visited.

Preorder traversal visits the tree and then visits, in preorder, each child of the tree.

Type Parameters:
E - type of exception possibly thrown
Parameters:
visitor - the tree's visitor
Throws:
E - if an error occurs when visiting the tree
E extends java.lang.Exception

size

int size()
Returns the number of nodes in the tree. This is one plus the sum of the number of nodes in each of the children.

If there are more than Integer.MAX_VALUE node, the return value is undefined and the user should seek professional help.

Returns:
the number of non-null nodes in the tree