dog.util
Class Tree

java.lang.Object
  |
  +--dog.util.Tree

public final class Tree
extends java.lang.Object

A utility class for manipulating objects in a tree structure.

This class provides a way to associate arbitrary objects with one another in a hierarchy.


Constructor Summary
Tree()
          Constructs an empty tree.
Tree(int increment)
          Constructs an empty tree with the specified capacity increment.
 
Method Summary
 void add(java.lang.Object object)
          Adds a root to the tree.
 void add(java.lang.Object[] objects)
          Adds roots to the tree.
 void add(java.lang.Object parent, java.lang.Object child)
          Adds a child to the specified parent in the tree.
 void add(java.lang.Object parent, java.lang.Object[] children)
          Adds children to the specified parent in the tree.
 void clear()
          Removes all objects from the tree.
 boolean contains(java.lang.Object object)
          Indicates whether the specified object is contained in the tree.
 int getChildCount(java.lang.Object parent)
          Returns the number of children of the specified object.
 java.lang.Object[] getChildren(java.lang.Object parent)
          Returns an array of children of the specified object, or an empty array if the object is not a parent in the tree.
 int getDepth(java.lang.Object object)
          Returns the depth of the specified object in the tree.
 java.lang.Object getParent(java.lang.Object child)
          Returns the parent of the specified object, or null if the object is a root or not a child in the tree.
 int getRootCount()
          Returns the number of roots in this tree.
 java.lang.Object[] getRoots()
          Returns the roots in this tree.
 java.lang.Object[] getTree()
          Returns all the objects in this tree in depth-first order.
 int getTreeCount()
          Returns the number of objects in this tree.
 boolean hasChildren()
          Indicates whether there are any children in this tree.
static void printArray(java.lang.String name, java.lang.Object[] array, int count)
           
 void printElements()
           
 void remove(java.lang.Object object)
          Removes the specified object and all its children from the tree.
 void remove(java.lang.Object[] objects)
          Removes the specified objects and all their children from the tree.
 void removeChildren(java.lang.Object parent)
          Recursively removes the specified object's children from the tree.
 void sort(ObjectCollator collator)
          Sorts the objects in the tree according to the specified object collator.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Tree

public Tree()
Constructs an empty tree.

Tree

public Tree(int increment)
Constructs an empty tree with the specified capacity increment.
Parameters:
increment - the amount by which the capacity is increased when an array overflows.
Method Detail

getParent

public java.lang.Object getParent(java.lang.Object child)
Returns the parent of the specified object, or null if the object is a root or not a child in the tree.

getChildren

public java.lang.Object[] getChildren(java.lang.Object parent)
Returns an array of children of the specified object, or an empty array if the object is not a parent in the tree.

getChildCount

public int getChildCount(java.lang.Object parent)
Returns the number of children of the specified object.

contains

public boolean contains(java.lang.Object object)
Indicates whether the specified object is contained in the tree.

add

public void add(java.lang.Object object)
Adds a root to the tree. Returns the root if it was added.

add

public void add(java.lang.Object[] objects)
Adds roots to the tree.

add

public void add(java.lang.Object parent,
                java.lang.Object child)
Adds a child to the specified parent in the tree.
Throws:
java.lang.IllegalArgumentException - if the parent does not exist in the tree or the child is the parent.

add

public void add(java.lang.Object parent,
                java.lang.Object[] children)
Adds children to the specified parent in the tree.
Throws:
java.lang.IllegalArgumentException - if the parent does not exist in the tree or one of the children is the parent.

remove

public void remove(java.lang.Object object)
Removes the specified object and all its children from the tree. Computationally pretty expensive.

remove

public void remove(java.lang.Object[] objects)
Removes the specified objects and all their children from the tree.
Throws:
NullPointerException - if an object does not exist in the tree.

removeChildren

public void removeChildren(java.lang.Object parent)
Recursively removes the specified object's children from the tree.
Throws:
NullPointerException - if the object does not exist in the tree.

clear

public void clear()
Removes all objects from the tree.

getDepth

public int getDepth(java.lang.Object object)
Returns the depth of the specified object in the tree. Roots have a depth of 1.

getRoots

public java.lang.Object[] getRoots()
Returns the roots in this tree.

getRootCount

public int getRootCount()
Returns the number of roots in this tree.

getTree

public java.lang.Object[] getTree()
Returns all the objects in this tree in depth-first order.

getTreeCount

public int getTreeCount()
Returns the number of objects in this tree.

hasChildren

public boolean hasChildren()
Indicates whether there are any children in this tree.

sort

public void sort(ObjectCollator collator)
Sorts the objects in the tree according to the specified object collator. This has no effect if a collator has not been defined.

printArray

public static void printArray(java.lang.String name,
                              java.lang.Object[] array,
                              int count)

printElements

public void printElements()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object