org.jaffa.util
Class Node

java.lang.Object
  extended byorg.jaffa.util.Node

public class Node
extends Object

An instance of this class can represent a node in a tree. Each node will have a system generated unique identifier. It may have a name (which can be non-unique). It can be linked to a Parent node or be the Root of the tree. It can have child nodes. It will have a value Object. Additionally it can have a map of attributes.


Constructor Summary
Node()
          Creates new Node
Node(String name)
          Creates new Node specifying a name.
 
Method Summary
 void addChild(Node node)
          Adds a child node.
 Object getAttribute(Object key)
          Returns the attribute for the specified key.
 Map getAttributes()
          Getter for property attributes.
 Node getChildById(String id)
          Returns the child by id.
 Node getChildByName(String name)
          Returns the 1st child which matches the name.
 Collection getChildren()
          Returns a collection of child nodes.
 Node getFromFamilyById(String id)
          Returns a node from the family by id.
 String getId()
          Returns the unique identifier of the node.
 String getName()
          Getter for property name.
 Node getParent()
          Returns the Parent node, or null if this node has no parent.
 Node getRoot()
          Returns the root node of the tree to which this node belongs.
 Object getValue()
          Getter for property value.
 boolean hasChildren()
          Returns true if the node has childen.
 boolean isRoot()
          Returns true if the node is its own root.
static void main(String[] args)
          Test rig
 void makeRoot()
          Makes the node its own root.
 boolean parentHasMoreChildren()
          Returns true if the parent node has any more child nodes after the current node.
 void printNode(Writer writer)
          A helper routine to print the contents of a node.
 void printNode(Writer writer, String pad, String padIncrement)
          A helper routine to print the contents of a node.
 Object removeAttribute(Object key)
          Removes an attribute .
 boolean removeChild(Node node)
          Removes a child node.
 boolean removeChild(String name)
          Removes the 1st child that matches the name
 boolean removeChildren()
          Remove all children.
 Object setAttribute(Object key, Object value)
          Adds an attribute.
 void setAttributes(Map attributes)
          Setter for property attributes.
 void setName(String name)
          Setter for property name.
 void setValue(Object value)
          Setter for property value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Node

public Node()
Creates new Node


Node

public Node(String name)
Creates new Node specifying a name.

Parameters:
name - The node name.
Method Detail

getId

public String getId()
Returns the unique identifier of the node.

Returns:
the unique identifier of the node.

getParent

public Node getParent()
Returns the Parent node, or null if this node has no parent.

Returns:
the Parent node, or null if this node has no parent.

getRoot

public Node getRoot()
Returns the root node of the tree to which this node belongs.

Returns:
the root node of the tree to which this node belongs.

getChildren

public Collection getChildren()
Returns a collection of child nodes.

Returns:
a collection of child nodes.

getChildByName

public Node getChildByName(String name)
Returns the 1st child which matches the name.

Parameters:
name - The node name.
Returns:
the 1st child which matches the name.

getChildById

public Node getChildById(String id)
Returns the child by id.

Parameters:
id - The node identifier.
Returns:
the child by id.

getFromFamilyById

public Node getFromFamilyById(String id)
Returns a node from the family by id.

Parameters:
id - The node identifier.
Returns:
a node from the family by id.

getName

public String getName()
Getter for property name.

Returns:
Value of property name.

setName

public void setName(String name)
Setter for property name.

Parameters:
name - New value of property name.

getValue

public Object getValue()
Getter for property value.

Returns:
Value of property value.

setValue

public void setValue(Object value)
Setter for property value.

Parameters:
value - New value of property value.

getAttributes

public Map getAttributes()
Getter for property attributes.

Returns:
Value of property attributes.

setAttributes

public void setAttributes(Map attributes)
Setter for property attributes.

Parameters:
attributes - New value of property attributes.

getAttribute

public Object getAttribute(Object key)
Returns the attribute for the specified key.

Parameters:
key - The attribute key.
Returns:
the attribute for the specified key.

setAttribute

public Object setAttribute(Object key,
                           Object value)
Adds an attribute. Will replace an existing attribute having the same key.

Parameters:
key - The attribute key.
value - The attribute value.
Returns:
previous value associated with specified key, or null if there was no attribute for key.

removeAttribute

public Object removeAttribute(Object key)
Removes an attribute .

Parameters:
key - The attribute key.
Returns:
the attribute that was removed, or null if there was no such attribute.

isRoot

public boolean isRoot()
Returns true if the node is its own root.

Returns:
true if the node is its own root.

hasChildren

public boolean hasChildren()
Returns true if the node has childen.

Returns:
true if the node has childen.

parentHasMoreChildren

public boolean parentHasMoreChildren()
Returns true if the parent node has any more child nodes after the current node.

Returns:
true if the parent node has any more child nodes after the current node.

makeRoot

public void makeRoot()
Makes the node its own root. Will remove links from existing parent node, if any.


addChild

public void addChild(Node node)
Adds a child node. Will add a link both ways.

Parameters:
node - The child node.

removeChild

public boolean removeChild(Node node)
Removes a child node. Will add a link both ways.

Parameters:
node - The child node.
Returns:
true if the child node was removed.

removeChild

public boolean removeChild(String name)
Removes the 1st child that matches the name

Parameters:
name - The node name.
Returns:
true if the child node was removed.

removeChildren

public boolean removeChildren()
Remove all children.

Returns:
true if any child node was removed.

printNode

public void printNode(Writer writer)
               throws IOException
A helper routine to print the contents of a node.

Parameters:
writer - The writer to which the node contents will be printed.
Throws:
IOException - if any I/O error occurs.

printNode

public void printNode(Writer writer,
                      String pad,
                      String padIncrement)
               throws IOException
A helper routine to print the contents of a node.

Parameters:
writer - The writer to which the node contents will be printed.
pad - The pad string to be used.
padIncrement - The increment string to be appended to the pad string at each successive level of child nodes.
Throws:
IOException - if any I/O error occurs.

main

public static void main(String[] args)
Test rig

Parameters:
args - The arguments. Not used.


Copyright © 2002-2004 JAFFA Project.