org.opengts.util
Class TreeNode

java.lang.Object
  extended by org.opengts.util.TreeNode

public class TreeNode
extends java.lang.Object

Tree Node


Nested Class Summary
static interface TreeNode.TreeNodeHandler
           
 
Field Summary
static java.lang.String JSON_Key_children
           
static java.lang.String JSON_Key_description
           
static java.lang.String JSON_Key_name
           
static java.lang.String SLASH_SEPARATOR
           
static char SLASH_SEPARATOR_CHAR
           
 
Constructor Summary
TreeNode()
          Constructor
TreeNode(java.lang.String name)
          Constructor
TreeNode(java.lang.String name, java.util.List<TreeNode> children)
          Constructor
 
Method Summary
protected  JSON._Object _setJsonNodeValues(JSON._Object jsonObj, java.lang.String name, java.lang.String desc, JSON._Array children)
          Overridable method for setting node JSON values
 TreeNode addChild(TreeNode node)
          Adds the specified TreeNode as a child to this node
 void addChildren(java.util.List<TreeNode> children)
          Adds all children in the specified java.util.List
static TreeNode createTreePath(TreeNode parent, java.lang.String[] name)
          Create named TreeNodes under specified parent
static TreeNode createTreePath(TreeNode parent, java.lang.String[] name, java.lang.Class<? extends TreeNode> treeNodeClass)
          Create named TreeNodes under specified parent
 boolean equals(java.lang.Object other)
          Returns true if the 'other' node is the same as 'this' node
 TreeNode findChildByName(java.lang.String name)
          Return first node with matching name
 TreeNode findChildByProperty(java.lang.String key, java.lang.Object value)
          Return first node with matching property key
static java.util.List<java.lang.String> flattenTree(java.util.List<java.lang.String> list, java.lang.String prefix, char sep, TreeNode parent)
          Flattens the specified tree
 TreeNode getChildAt(int ndx)
          Finds/Returns the named child Does not check grandchildren.
 TreeNode getChildByName(java.lang.String name)
          Finds/Returns the named child Does not check grandchildren.
 TreeNode getChildByPath(java.lang.String[] name)
          Finds/Returns the named child Does not check grandchildren.
 TreeNode getChildByValue(java.lang.Object val)
          Finds/Returns the child matching the specified value Does not check grandchildren.
 java.util.List<TreeNode> getChildren()
          Returns the collection of children nodes
 java.lang.String getDescription()
          Gets the description of this TreeNode
 JSON._Array getJsonChildrenArray()
          Returns a JSON array of this nodes children (or null if there are no children)
 JSON._Object getJsonObject()
          Returns a JSON object for this TreeNode
 int getLevel()
          Calculates/Returns the level of this node (root node is level '0')
 int getLevel(TreeNode parent)
          Calculates/Returns the level of this node (root node is level '0')
 java.lang.String getName()
          Gets the name of this TreeNode
 TreeNode getNextSibling()
          Return the 'next' sibling node of this node
 java.lang.Object getObject()
          Gets the node leaf object
protected  TreeNode getParent()
          Gets the parent of this TreeNode
 TreeNode[] getPath()
          Returns the path of this TreeNode
 TreeNode[] getPath(TreeNode parent)
          Returns the path of this TreeNode
 java.lang.String getPathDescription(java.lang.String sep)
          Returns the path name of this TreeNode
 java.lang.String getPathDescription(TreeNode parent, java.lang.String sep)
          Returns the path name of this TreeNode
 java.lang.String[] getPathDescriptions()
          Returns the path name of this TreeNode
 java.lang.String[] getPathDescriptions(TreeNode parent)
          Returns the path name of this TreeNode
 java.lang.String getPathName(java.lang.String sep)
          Returns the path name of this TreeNode
 java.lang.String getPathName(TreeNode parent, java.lang.String sep)
          Returns the path name of this TreeNode
 java.lang.String[] getPathNames()
          Returns the path name of this TreeNode
 java.lang.String[] getPathNames(TreeNode parent)
          Returns the path name of this TreeNode
 TreeNode getPreviousSibling()
          Return the 'previous' sibling node of this node
 RTProperties getProperties()
          Get node properties
 java.lang.Object getProperty(java.lang.String key)
          Gets the specified node property value
 java.lang.Object getProperty(java.lang.String key, java.lang.Object dft)
          Gets the specified node property value
static TreeNode getTreePath(TreeNode parent, java.lang.String[] name)
          Gets/Returns named TreeNodes under specified parent
 int getType()
          Gets the type of this TreeNode
 boolean hasChildren()
          Returns true if this node has children
 boolean hasNextSibling()
          Return true if this node has a 'next' sibling
 boolean hasObject()
          Returns true if this node has an object value
 boolean hasParent()
          Returns true if this TreeNode has a parent
 boolean hasPreviousSibling()
          Return true if this node has a 'previous' sibling
 boolean hasProperties()
          Returns true if properties have been defined for this node
 boolean hasProperty(java.lang.String key)
          Returns true if the specified property key is defined in this node
 boolean isAncestor(TreeNode node)
          Returns true if the specified node is the same as this node, or is an ancestor
 boolean isLeaf()
          Returns true if this node has an object value
 boolean isOffspring(TreeNode node)
          Returns true if the specified node is an offspring of this node
 boolean isRootNode()
          Returns true if this node does not have a parent
static void main(java.lang.String[] argv)
           
 void printChildren()
          Prints the children nodes
 void printChildren(int lvl)
          Prints the children nodes
static
<TN extends TreeNode>
void
printTree(TN parent)
          Prints the specified Tree
 boolean removeChild(TreeNode tn)
          Remove the specified child
 boolean removeFromParent()
          Remove this node from it's parent
 void setDescription(java.lang.String desc)
          Sets the description of this TreeNode
 void setName(java.lang.String name)
          Sets the name of this TreeNode
 void setObject(java.lang.Object val)
          Sets a node leaf object
protected  void setParent(TreeNode node)
          Sets the parent of this TreeNode
 void setProperty(java.lang.String key, java.lang.Object val)
          Sets the specified node property value
 void setType(int type)
          Sets the type of this TreeNode (usage defined by caller)
 int size()
          Returns number of children in this node
 java.lang.String toString()
          Gets the String representation of this TreeNode
 java.lang.String toString(java.lang.String sep)
          Gets the String representation of this TreeNode
 TreeNode traverseTree(TreeNode.TreeNodeHandler tnh)
          Prefix traversal of TreeNodes
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SLASH_SEPARATOR

public static final java.lang.String SLASH_SEPARATOR
See Also:
Constant Field Values

SLASH_SEPARATOR_CHAR

public static final char SLASH_SEPARATOR_CHAR
See Also:
Constant Field Values

JSON_Key_name

public static final java.lang.String JSON_Key_name
See Also:
Constant Field Values

JSON_Key_description

public static final java.lang.String JSON_Key_description
See Also:
Constant Field Values

JSON_Key_children

public static final java.lang.String JSON_Key_children
See Also:
Constant Field Values
Constructor Detail

TreeNode

public TreeNode()
Constructor


TreeNode

public TreeNode(java.lang.String name)
Constructor


TreeNode

public TreeNode(java.lang.String name,
                java.util.List<TreeNode> children)
Constructor

Method Detail

createTreePath

public static TreeNode createTreePath(TreeNode parent,
                                      java.lang.String[] name)
Create named TreeNodes under specified parent


createTreePath

public static TreeNode createTreePath(TreeNode parent,
                                      java.lang.String[] name,
                                      java.lang.Class<? extends TreeNode> treeNodeClass)
Create named TreeNodes under specified parent


getTreePath

public static TreeNode getTreePath(TreeNode parent,
                                   java.lang.String[] name)
Gets/Returns named TreeNodes under specified parent


flattenTree

public static java.util.List<java.lang.String> flattenTree(java.util.List<java.lang.String> list,
                                                           java.lang.String prefix,
                                                           char sep,
                                                           TreeNode parent)
Flattens the specified tree


printTree

public static <TN extends TreeNode> void printTree(TN parent)
Prints the specified Tree


setName

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


getName

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


setType

public void setType(int type)
Sets the type of this TreeNode (usage defined by caller)


getType

public int getType()
Gets the type of this TreeNode


setDescription

public void setDescription(java.lang.String desc)
Sets the description of this TreeNode


getDescription

public java.lang.String getDescription()
Gets the description of this TreeNode


getChildren

public java.util.List<TreeNode> getChildren()
Returns the collection of children nodes


hasChildren

public boolean hasChildren()
Returns true if this node has children


size

public int size()
Returns number of children in this node


addChildren

public void addChildren(java.util.List<TreeNode> children)
Adds all children in the specified java.util.List


addChild

public TreeNode addChild(TreeNode node)
Adds the specified TreeNode as a child to this node


getChildAt

public TreeNode getChildAt(int ndx)
Finds/Returns the named child Does not check grandchildren.


getChildByName

public TreeNode getChildByName(java.lang.String name)
Finds/Returns the named child Does not check grandchildren.


getChildByPath

public TreeNode getChildByPath(java.lang.String[] name)
Finds/Returns the named child Does not check grandchildren.


getChildByValue

public TreeNode getChildByValue(java.lang.Object val)
Finds/Returns the child matching the specified value Does not check grandchildren.


removeChild

public boolean removeChild(TreeNode tn)
Remove the specified child


removeFromParent

public boolean removeFromParent()
Remove this node from it's parent


hasNextSibling

public boolean hasNextSibling()
Return true if this node has a 'next' sibling


getNextSibling

public TreeNode getNextSibling()
Return the 'next' sibling node of this node


hasPreviousSibling

public boolean hasPreviousSibling()
Return true if this node has a 'previous' sibling


getPreviousSibling

public TreeNode getPreviousSibling()
Return the 'previous' sibling node of this node


setParent

protected void setParent(TreeNode node)
Sets the parent of this TreeNode


getParent

protected TreeNode getParent()
Gets the parent of this TreeNode


hasParent

public boolean hasParent()
Returns true if this TreeNode has a parent


isRootNode

public boolean isRootNode()
Returns true if this node does not have a parent


isAncestor

public boolean isAncestor(TreeNode node)
Returns true if the specified node is the same as this node, or is an ancestor


isOffspring

public boolean isOffspring(TreeNode node)
Returns true if the specified node is an offspring of this node


getLevel

public int getLevel()
Calculates/Returns the level of this node (root node is level '0')


getLevel

public int getLevel(TreeNode parent)
Calculates/Returns the level of this node (root node is level '0')


getPath

public TreeNode[] getPath()
Returns the path of this TreeNode


getPath

public TreeNode[] getPath(TreeNode parent)
Returns the path of this TreeNode


getPathNames

public java.lang.String[] getPathNames()
Returns the path name of this TreeNode


getPathNames

public java.lang.String[] getPathNames(TreeNode parent)
Returns the path name of this TreeNode


getPathName

public java.lang.String getPathName(java.lang.String sep)
Returns the path name of this TreeNode


getPathName

public java.lang.String getPathName(TreeNode parent,
                                    java.lang.String sep)
Returns the path name of this TreeNode


getPathDescriptions

public java.lang.String[] getPathDescriptions()
Returns the path name of this TreeNode


getPathDescriptions

public java.lang.String[] getPathDescriptions(TreeNode parent)
Returns the path name of this TreeNode


getPathDescription

public java.lang.String getPathDescription(java.lang.String sep)
Returns the path name of this TreeNode


getPathDescription

public java.lang.String getPathDescription(TreeNode parent,
                                           java.lang.String sep)
Returns the path name of this TreeNode


traverseTree

public TreeNode traverseTree(TreeNode.TreeNodeHandler tnh)
Prefix traversal of TreeNodes


findChildByName

public TreeNode findChildByName(java.lang.String name)
Return first node with matching name


findChildByProperty

public TreeNode findChildByProperty(java.lang.String key,
                                    java.lang.Object value)
Return first node with matching property key


getJsonChildrenArray

public JSON._Array getJsonChildrenArray()
Returns a JSON array of this nodes children (or null if there are no children)

Returns:
A JSON array of this nodes children (or null if there are no children)

getJsonObject

public JSON._Object getJsonObject()
Returns a JSON object for this TreeNode

Returns:
A JSON object for this TreeNode

_setJsonNodeValues

protected JSON._Object _setJsonNodeValues(JSON._Object jsonObj,
                                          java.lang.String name,
                                          java.lang.String desc,
                                          JSON._Array children)
Overridable method for setting node JSON values

Parameters:
jsonObj - The JSON object for this not (never null)
name - The default node name
desc - The default node description
children - A JSON array of children JSON nodes (may be null)
Returns:
The jsonObj passed to this method

toString

public java.lang.String toString()
Gets the String representation of this TreeNode

Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(java.lang.String sep)
Gets the String representation of this TreeNode


printChildren

public void printChildren()
Prints the children nodes


printChildren

public void printChildren(int lvl)
Prints the children nodes


equals

public boolean equals(java.lang.Object other)
Returns true if the 'other' node is the same as 'this' node

Overrides:
equals in class java.lang.Object

getProperties

public RTProperties getProperties()
Get node properties

Returns:
Node properties

hasProperties

public boolean hasProperties()
Returns true if properties have been defined for this node

Returns:
True if properties have been defined for this node

hasProperty

public boolean hasProperty(java.lang.String key)
Returns true if the specified property key is defined in this node

Parameters:
key - The property key
Returns:
True if the specified property key is defined in this node

getProperty

public java.lang.Object getProperty(java.lang.String key,
                                    java.lang.Object dft)
Gets the specified node property value

Parameters:
key - The property key
dft - The default return value
Returns:
The property value

getProperty

public java.lang.Object getProperty(java.lang.String key)
Gets the specified node property value

Parameters:
key - The property key
Returns:
The property value

setProperty

public void setProperty(java.lang.String key,
                        java.lang.Object val)
Sets the specified node property value

Parameters:
key - The property key
val - The property value

setObject

public void setObject(java.lang.Object val)
Sets a node leaf object


getObject

public java.lang.Object getObject()
Gets the node leaf object


hasObject

public boolean hasObject()
Returns true if this node has an object value


isLeaf

public boolean isLeaf()
Returns true if this node has an object value


main

public static void main(java.lang.String[] argv)