com.huguesjohnson.tiamat
Class TreeNode

java.lang.Object
  extended by com.huguesjohnson.tiamat.TreeNode
All Implemented Interfaces:
Storable
Direct Known Subclasses:
NamedObjectTreeNode

public class TreeNode
extends java.lang.Object
implements Storable

TreeNode - stores object in a heirarchical (tree) structure

Author:
Hugues Johnson

Field Summary
static java.lang.String DEFAULT_PATH_DELIMITER
          Default path delimiter.
 
Constructor Summary
TreeNode(java.lang.String name)
          Creates a new instance of TreeNode, uses the default path delimiter.
TreeNode(java.lang.String name, java.lang.Object object)
          Creates a new instance of TreeNode, uses the default path delimiter.
TreeNode(java.lang.String name, java.lang.Object object, TreeNode parent)
          Creates a new instance of TreeNode, uses the default path delimiter.
TreeNode(java.lang.String name, java.lang.String pathDelimiter)
          Creates a new instance of TreeNode.
TreeNode(java.lang.String name, java.lang.String pathDelimiter, java.lang.Object object)
          Creates a new instance of TreeNode.
TreeNode(java.lang.String name, java.lang.String pathDelimiter, java.lang.Object object, TreeNode parent)
          Creates a new instance of TreeNode.
 
Method Summary
protected  void addChild(TreeNode childNode)
          Should be called by another node when it sets this node as the parent.
 TreeNode getChild(int index)
          Returns the child TreeNode at the specified index, null if index is out of bounds.
 int getChildLeafCount(boolean recursive)
          Returns the number of leaves for this node.
 TreeNode[] getChildLeaves(boolean recursive)
          Returns the all leaves for this node.
 TreeNode getChildNode(java.lang.String path, boolean recursive)
          Returns the child TreeNode with the specified path, null if it is not found.
 int getChildNodeCount(boolean recursive)
          Returns the number of children for this node.
 TreeNode[] getChildren()
          Returns the children of this TreeNode.
 java.lang.String getName()
          Returns the name of this TreeNode.
 java.lang.Object getObject()
          Returns the object this TreeNode is storing.
 TreeNode getParent()
          Returns the parent of this TreeNode.
 java.lang.String getPath()
          Returns the full path to this node.
 java.lang.String getPathDelimiter()
          Returns the character used to separate names in path, can only be changed in constructor.
 void insertChildNode(java.lang.String path, TreeNode node)
          Inserts a node at the specified path.
 boolean isLeaf()
          Returns true if no children.
 boolean isRoot()
          Returns true if parent is null.
 void onDeserialize(Formatter formatter, java.lang.String absoluteDir)
          The onDeserialize event should be fired after an object is de-serialized.
 boolean removeChildNode(java.lang.String path, boolean recursive)
          Removes the child TreeNode with the specified path.
 void setChild(int index, TreeNode child)
          Sets the child TreeNode at the specified index.
 void setChildren(TreeNode[] children)
          Sets the children of this TreeNode.
 void setName(java.lang.String name)
          Sets the name of this TreeNode.
 void setObject(java.lang.Object object)
          Sets the object this TreeNode is storing.
 void setParent(TreeNode parent)
          Sets the parent of this TreeNode.
 java.lang.String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_PATH_DELIMITER

public static final java.lang.String DEFAULT_PATH_DELIMITER
Default path delimiter.

See Also:
Constant Field Values
Constructor Detail

TreeNode

public TreeNode(java.lang.String name)
Creates a new instance of TreeNode, uses the default path delimiter.

Parameters:
name - The name of this TreeNode.

TreeNode

public TreeNode(java.lang.String name,
                java.lang.String pathDelimiter)
Creates a new instance of TreeNode.

Parameters:
name - The name of this TreeNode.
pathDelimiter - The path delimiter to use.

TreeNode

public TreeNode(java.lang.String name,
                java.lang.Object object)
Creates a new instance of TreeNode, uses the default path delimiter.

Parameters:
name - The name of this TreeNode.
object - The object this TreeNode is storing.

TreeNode

public TreeNode(java.lang.String name,
                java.lang.String pathDelimiter,
                java.lang.Object object)
Creates a new instance of TreeNode.

Parameters:
name - The name of this TreeNode.
pathDelimiter - The path delimiter to use.
object - The object this TreeNode is storing.

TreeNode

public TreeNode(java.lang.String name,
                java.lang.Object object,
                TreeNode parent)
Creates a new instance of TreeNode, uses the default path delimiter.

Parameters:
name - The name of this TreeNode.
object - The object this TreeNode is storing.
parent - The parent of this TreeNode.

TreeNode

public TreeNode(java.lang.String name,
                java.lang.String pathDelimiter,
                java.lang.Object object,
                TreeNode parent)
Creates a new instance of TreeNode.

Parameters:
name - The name of this TreeNode.
pathDelimiter - The path delimiter to use.
object - The object this TreeNode is storing.
parent - The parent of this TreeNode.
Method Detail

getPathDelimiter

public java.lang.String getPathDelimiter()
Returns the character used to separate names in path, can only be changed in constructor.

Returns:
The character used to separate names in path.

getParent

public TreeNode getParent()
Returns the parent of this TreeNode.

Returns:
The parent of this TreeNode.

setParent

public void setParent(TreeNode parent)
Sets the parent of this TreeNode.

Parameters:
parent - The new parent of this TreeNode.

setChildren

public void setChildren(TreeNode[] children)
Sets the children of this TreeNode.

Parameters:
children - The new children of this TreeNode.

getChildren

public TreeNode[] getChildren()
Returns the children of this TreeNode.

Returns:
All child TreeNodes.

setChild

public void setChild(int index,
                     TreeNode child)
Sets the child TreeNode at the specified index.

Parameters:
index - The index to set the child TreeNode at.
child - The TreeNode to set.

getChild

public TreeNode getChild(int index)
Returns the child TreeNode at the specified index, null if index is out of bounds.

Parameters:
index - The index of the child TreeNode to return.
Returns:
The child TreeNode at the specified index, null if index is out of bounds.

getObject

public java.lang.Object getObject()
Returns the object this TreeNode is storing.

Returns:
The object this TreeNode is storing.

setObject

public void setObject(java.lang.Object object)
Sets the object this TreeNode is storing.

Parameters:
object - The new object this TreeNode is storing.

getName

public java.lang.String getName()
Returns the name of this TreeNode.

Returns:
The name of this TreeNode.

setName

public void setName(java.lang.String name)
Sets the name of this TreeNode.

Parameters:
name - The new name of this TreeNode.

addChild

protected void addChild(TreeNode childNode)
Should be called by another node when it sets this node as the parent.

Parameters:
childNode - The child node to add.

getPath

public java.lang.String getPath()
Returns the full path to this node. If this is not a root node, dynamically generate the path by recursively calling parent.getPath().

Returns:
The full path to this node.

isRoot

public boolean isRoot()
Returns true if parent is null.

Returns:
True if parent is null.

isLeaf

public boolean isLeaf()
Returns true if no children.

Returns:
True if no children.

removeChildNode

public boolean removeChildNode(java.lang.String path,
                               boolean recursive)
Removes the child TreeNode with the specified path.

Parameters:
path - The path of the child to remove.
recursive - Set to true to recursively search through children, false to only check the direct children of this TreeNode.
Returns:
The success of the operation.

getChildNode

public TreeNode getChildNode(java.lang.String path,
                             boolean recursive)
Returns the child TreeNode with the specified path, null if it is not found.

Parameters:
path - The path of the child to remove.
recursive - Set to true to recursively search through children, false to only check the direct children of this TreeNode.
Returns:
The child TreeNode with the specified path, null if it is not found.

getChildNodeCount

public int getChildNodeCount(boolean recursive)
Returns the number of children for this node.

Parameters:
recursive - Set to true to recursively search through children, false to only check the direct children of this TreeNode.
Returns:
The number of children for this node.

getChildLeafCount

public int getChildLeafCount(boolean recursive)
Returns the number of leaves for this node.

Parameters:
recursive - Set to true to recursively search through children, false to only check the direct children of this TreeNode.
Returns:
The number of leaves for this node.

getChildLeaves

public TreeNode[] getChildLeaves(boolean recursive)
Returns the all leaves for this node.

Parameters:
recursive - Set to true to recursively search through children, false to only check the direct children of this TreeNode.
Returns:
All leaves for this node.

insertChildNode

public void insertChildNode(java.lang.String path,
                            TreeNode node)
Inserts a node at the specified path.

Parameters:
path - The path to insert the node at.
node - The TreeNode to insert.

toString

public java.lang.String toString()
Returns a string representation of the object.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of the object.

onDeserialize

public void onDeserialize(Formatter formatter,
                          java.lang.String absoluteDir)
The onDeserialize event should be fired after an object is de-serialized. This allows objects to load transient members, set their working directory, and load Storable members if needed.

Specified by:
onDeserialize in interface Storable
Parameters:
formatter - the formatter to use to load any storable members
absoluteDir - the working directory since all paths stored in the object should be relative