com.springsource.util.common
Class ThreadSafeArrayListTree<V>

java.lang.Object
  extended by com.springsource.util.common.ThreadSafeArrayListTree<V>
Type Parameters:
V - type of values in tree
All Implemented Interfaces:
Tree<V>

public final class ThreadSafeArrayListTree<V>
extends java.lang.Object
implements Tree<V>

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

Concurrent Semantics
This class is thread safe.

Since:
jersey
Author:
Glyn Normington

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.springsource.util.common.Tree
Tree.ExceptionThrowingTreeVisitor<V,E extends java.lang.Exception>, Tree.TreeVisitor<V>
 
Field Summary
private  java.util.List<ThreadSafeArrayListTree<V>> children
           
private  java.lang.Object monitor
           
private  Tree<V> parent
           
private static java.lang.Object tieMonitor
           
private  V value
           
 
Constructor Summary
protected ThreadSafeArrayListTree(Tree<V> tree, Tree<V> parent, java.lang.Object monitor)
          Construct a tree by deeply copying the given tree, using the given parent, and inheriting the given monitor.
  ThreadSafeArrayListTree(V value)
          Construct a tree with the given value, which may be null.
protected ThreadSafeArrayListTree(V value, java.lang.Object monitor)
           
 
Method Summary
 Tree<V> addChild(Tree<V> child)
          Adds a new child tree to this node's children.
 boolean equals(java.lang.Object obj)
          
private  ThreadSafeArrayListTree<V> getChild(int i)
           
 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.
 int hashCode()
          
private  int numChildren()
           
 boolean removeChild(Tree<V> child)
          Removes the first occurrence of the given child tree from this node's children.
private  void setParent(Tree<V> child, Tree<V> parent)
           
 int size()
          Returns the number of nodes in the tree.
 java.lang.String toString()
          
<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 ThreadSafeArrayListTree in preorder (see below) and call the visit method of the given Tree.TreeVisitor at each node.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

value

private volatile V value

monitor

private final java.lang.Object monitor

tieMonitor

private static final java.lang.Object tieMonitor

children

private final java.util.List<ThreadSafeArrayListTree<V>> children

parent

private Tree<V> parent
Constructor Detail

ThreadSafeArrayListTree

public ThreadSafeArrayListTree(V value)
Construct a tree with the given value, which may be null.

Parameters:
value - the value of the tree, which may be null

ThreadSafeArrayListTree

protected ThreadSafeArrayListTree(V value,
                                  java.lang.Object monitor)

ThreadSafeArrayListTree

protected ThreadSafeArrayListTree(Tree<V> tree,
                                  Tree<V> parent,
                                  java.lang.Object monitor)
Construct a tree by deeply copying the given tree, using the given parent, and inheriting the given monitor.

Parameters:
tree - the tree to copy
parent - the parent of the new tree or null
monitor - the monitor to inherit
Method Detail

getValue

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

Specified by:
getValue in interface Tree<V>
Returns:
the value, which may be null

getChildren

public 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 .

The returned list is synchronized to preserve thread safety, but may still result in ConcurrentModificationException being thrown.

Specified by:
getChildren in interface Tree<V>
Returns:
this tree's children

addChild

public 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. The copy shares this tree's monitor.

Specified by:
addChild in interface Tree<V>
Parameters:
child - the child tree to add
Returns:
the copy of the child tree

removeChild

public 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.

Specified by:
removeChild in interface Tree<V>
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)

setParent

private void setParent(Tree<V> child,
                       Tree<V> parent)

visit

public void visit(Tree.TreeVisitor<V> visitor)
Traverse this ThreadSafeArrayListTree 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.

Specified by:
visit in interface Tree<V>
Parameters:
visitor - a Tree.TreeVisitor

visit

public <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.

Specified by:
visit in interface Tree<V>
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

getChild

private ThreadSafeArrayListTree<V> getChild(int i)

numChildren

private int numChildren()

size

public 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.

Specified by:
size in interface Tree<V>
Returns:
the number of non-null nodes in the tree

hashCode

public int hashCode()

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)

Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object

getParent

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

Specified by:
getParent in interface Tree<V>
Returns:
this tree's parent