com.hedgehog.math.graph_tree
Class GraphTreeNode<NodeType>

java.lang.Object
  extended by com.hedgehog.HObject
      extended by com.hedgehog.math.graph_tree.GraphTreeNode<NodeType>
Type Parameters:
NodeType - Node type.
All Implemented Interfaces:
ModelObject, java.lang.Comparable, java.util.Comparator
Direct Known Subclasses:
UndirectedGraphTreeNode

public class GraphTreeNode<NodeType>
extends HObject
implements ModelObject

Title: GraphTreeNode - represents a node in a graph or tree.

Description: Models a node in a graph or tree as a node object and list of adjacent nodes.

Copyright: Copyright (c) Hedgehog Software 2007-2009.

Company: Hedgehog Software.

Since:
1.0
Version:
1.1

Field Summary
protected  java.util.ArrayList<GraphTreeNode<NodeType>> mAdjacentNodes
          List of adjacent nodes.
protected  NodeType nodeObject
          Node object.
 
Fields inherited from class com.hedgehog.HObject
mID, mName
 
Constructor Summary
GraphTreeNode()
          Default constructor.
GraphTreeNode(GraphTreeNode<NodeType> object)
          Copy constructor.
GraphTreeNode(NodeType nodeObject)
          Constructor.
GraphTreeNode(NodeType nodeObject, java.util.ArrayList<GraphTreeNode<NodeType>> adjacentNodes)
          Constructor.
 
Method Summary
 boolean addAdjacentNode(GraphTreeNode<NodeType> adjacentNode)
          Adds the specified adjacent node.
 int[] adjacentNodeIDs()
          Returns an integer array of the IDs of adjacent nodes to this node.
 int[] adjacentNodeObjectIDs()
          Returns an integer array of the IDs of the node objects of adjacent nodes to this node.
 java.util.Vector<ModelObject> childModelObjects()
          Returns a vector of this object's child ModelObject objects.
 java.sql.PreparedStatement databaseInsertStatement(java.sql.Connection connection, int modelID)
          Returns a prepared statement for inserting (INSERT command) a ModelObject object into a database.
 java.sql.PreparedStatement databaseUpdateStatement(java.sql.Connection connection, int modelID)
          Returns a prepared statement for updating (UPDATE command) a ModelObject object into a database.
 int degree()
          Returns the degree of a node; ie the number of adjacent nodes.
 void deleteAdjacencies()
          Deletes all adjacency relationships from this node.
 boolean deleteAdjacentNode(GraphTreeNode<NodeType> adjacentNode)
          Deletes the specified adjacent node.
 boolean equalNodeObjects(GraphTreeNode node)
          Tests whether or not two nodes are equal using the equals() method on the node objects.
 boolean equals(java.lang.Object object)
          Compares the specified object against this node.
 ModelObject fromDatabase(java.sql.Connection connection, Model model, int modelID, int objectID)
          Returns a model object from the specified database table.
 ModelObject fromXMLNode(org.w3c.dom.Document document, org.w3c.dom.Node documentRootNode, org.w3c.dom.Node node, Model model)
          Returns a model object from the specified xml node.
 java.util.ArrayList<GraphTreeNode<NodeType>> getAdjacentNodes()
          Returns the linked list of adjacnt nodes.
 java.awt.Color getNodeColour()
          Returns the node colour.
 int getNodeDepth()
          Returns the node depth.
 int getNodeFinish()
          Returns the node finish time.
 NodeType getNodeObject()
          Returns the node object.
 GraphTreeNode<NodeType> getNodePredecessor()
          Returns the predecessor node.
 java.lang.String getNodeType()
          Returns the string equivalent of the generic type of the node object; eg "ModelDouble" in GraphTreeNode.
 boolean hasAdjacentNodes()
          Tests whether or not a node has a list of adjacent nodes.
 int hashCode()
          Returns the hash code of this node.
 boolean hasMultipleArcs()
          Returns logical-true if a node has multiple arcs, ie the same node appears more than once in a node's adjacency list.
 boolean hasNodeObject()
          Tests whether or not a node has a node object.
 boolean isAdjacentNode(GraphTreeNode node)
          Returns logical-true if node is an adjacent node, else logical-false.
 boolean isLoopNode()
          Returns logical-true if the operated-on node is a loop node (i.e.
 boolean isSimple()
          Returns logical-true if a node is simple (i.e.
 int numberOfAdjacentNodes()
          Returns the number of adjacent nodes.
 boolean setAdjacentNodes(java.util.ArrayList<GraphTreeNode<NodeType>> newAdjacentNodes)
          Sets the adjacent nodes.
 boolean setNodeColour(java.awt.Color newColour)
          Sets the node colour used by breadth/depth first search iterators.
 boolean setNodeDepth(int newDepth)
          Sets the node depth.
 boolean setNodeFinish(int newFinish)
          Sets the node finish time.
 boolean setNodeObject(NodeType newNodeObject)
          Sets the node object.
 boolean setNodePredecessor(GraphTreeNode<NodeType> newPredecessor)
           
 java.lang.String toString()
          Returns a String representation of a GraphTreeNode object.
 X3DNode toX3DNode(X3DObject x3dObject)
          X3D support.
 org.w3c.dom.Element toXMLNode(org.w3c.dom.Document document, org.w3c.dom.Element documentRootNode)
          Returns the xml node of this object.
 
Methods inherited from class com.hedgehog.HObject
clone, compare, compareTo, copy, getID, getName, hasID, hasName, setID, setName
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

nodeObject

protected NodeType nodeObject
Node object.


mAdjacentNodes

protected java.util.ArrayList<GraphTreeNode<NodeType>> mAdjacentNodes
List of adjacent nodes.

Constructor Detail

GraphTreeNode

public GraphTreeNode()
Default constructor. Sets the node object to null and creates a new non-null adjacency list.


GraphTreeNode

public GraphTreeNode(NodeType nodeObject)
Constructor. Sets the node object to the specified object and creates a new non-null adjacency list.

Parameters:
nodeObject - Node object.

GraphTreeNode

public GraphTreeNode(NodeType nodeObject,
                     java.util.ArrayList<GraphTreeNode<NodeType>> adjacentNodes)
Constructor. Sets the node object and adjacency list to the specified objects.

Parameters:
nodeObject - Node object.
adjacentNodes - LinkedList of adjacent nodes.

GraphTreeNode

public GraphTreeNode(GraphTreeNode<NodeType> object)
Copy constructor.

Parameters:
object - Object to copy.
Method Detail

addAdjacentNode

public boolean addAdjacentNode(GraphTreeNode<NodeType> adjacentNode)
Adds the specified adjacent node.

Parameters:
adjacentNode - Adjacent node to be added.
Returns:
Logical-false if node added, else logical-false. Logical-false is also returned if the specified argument is null.

degree

public int degree()
Returns the degree of a node; ie the number of adjacent nodes.

Returns:
Returns the degree of a node.

deleteAdjacentNode

public boolean deleteAdjacentNode(GraphTreeNode<NodeType> adjacentNode)
Deletes the specified adjacent node.

Parameters:
adjacentNode - Adjacent node to be deleted from the operated-on node's adjacency list.
Returns:
Logical-false if adjacentNode is deleted, else logical-false. Logical-false is also returned if the specified argument is null.

deleteAdjacencies

public void deleteAdjacencies()
Deletes all adjacency relationships from this node.


getNodeObject

public NodeType getNodeObject()
Returns the node object.

Returns:
The node object.

getAdjacentNodes

public java.util.ArrayList<GraphTreeNode<NodeType>> getAdjacentNodes()
Returns the linked list of adjacnt nodes.

Returns:
The list of adjacent nodes.

adjacentNodeIDs

public int[] adjacentNodeIDs()
Returns an integer array of the IDs of adjacent nodes to this node.

Returns:
An integer array of the IDs of adjacent nodes to this node.

adjacentNodeObjectIDs

public int[] adjacentNodeObjectIDs()
Returns an integer array of the IDs of the node objects of adjacent nodes to this node.

Returns:
An integer array of the IDs of the node objects of adjacent nodes to this node.

hasNodeObject

public boolean hasNodeObject()
Tests whether or not a node has a node object.

Returns:
Logical-true if a node has a non-null node object, else logical-false.

hasAdjacentNodes

public boolean hasAdjacentNodes()
Tests whether or not a node has a list of adjacent nodes.

Returns:
Logical-true if a node has a non-null or non-empty list of adjacent nodes.

hasMultipleArcs

public boolean hasMultipleArcs()
Returns logical-true if a node has multiple arcs, ie the same node appears more than once in a node's adjacency list. Uses the equals() method to perform the node object comparison.

Returns:
Logical-true if a node has multiple arcs, else logical-false.

isLoopNode

public boolean isLoopNode()
Returns logical-true if the operated-on node is a loop node (i.e. an arc with identical end nodes, in other words the node is in its own adjacency list).

Returns:
Logical-true if node is a loop node, else logical-false.

isSimple

public boolean isSimple()
Returns logical-true if a node is simple (i.e. no multiple or self-loop arcs).

Returns:
Logical-true if node is a simple node, else logical-false.

equalNodeObjects

public boolean equalNodeObjects(GraphTreeNode node)
Tests whether or not two nodes are equal using the equals() method on the node objects.

Parameters:
node - Comparison node.
Returns:
Logiacl-true if the two nodes are the same, else logical-false.

equals

public boolean equals(java.lang.Object object)
Compares the specified object against this node. Calls equalNodeObjects() for th enode objects.

Specified by:
equals in interface java.util.Comparator
Overrides:
equals in class HObject
Parameters:
object - Object to compare.
Returns:
Logical-true if the specified object is equal to this object, else logical-false.

hashCode

public int hashCode()
Returns the hash code of this node. Simply returns the hash code of the encapsulatd node object.

Overrides:
hashCode in class HObject
Returns:
The hash code of this node.

isAdjacentNode

public boolean isAdjacentNode(GraphTreeNode node)
Returns logical-true if node is an adjacent node, else logical-false.

Parameters:
node - Test node.
Returns:
Logical-true if nde is adjacent, else logical-false.

numberOfAdjacentNodes

public int numberOfAdjacentNodes()
Returns the number of adjacent nodes.

Returns:
The number of adjacent nodes.

setNodeObject

public boolean setNodeObject(NodeType newNodeObject)
Sets the node object.

Parameters:
newNodeObject - New node object.
Returns:
Logical-rue if node object set, else logical-false.

setAdjacentNodes

public boolean setAdjacentNodes(java.util.ArrayList<GraphTreeNode<NodeType>> newAdjacentNodes)
Sets the adjacent nodes.

Parameters:
newAdjacentNodes - New linked list of adjacent nodes.
Returns:
Logical-true if new adjacency list set, else logical-false.

toString

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

Overrides:
toString in class java.lang.Object
Returns:
A String representation of a GraphTreeNode object.

getNodeDepth

public int getNodeDepth()
Returns the node depth.

Returns:
The node depth.

getNodeColour

public java.awt.Color getNodeColour()
Returns the node colour.

Returns:
The node colour.

getNodePredecessor

public GraphTreeNode<NodeType> getNodePredecessor()
Returns the predecessor node.

Returns:
The predecessor node.

setNodeDepth

public boolean setNodeDepth(int newDepth)
Sets the node depth.

Parameters:
newDepth - New depth.
Returns:
Logical-true if new depth set(>=0), else logical-false(<0).

getNodeFinish

public int getNodeFinish()
Returns the node finish time.

Returns:
The node finish time.

setNodeFinish

public boolean setNodeFinish(int newFinish)
Sets the node finish time.

Parameters:
newFinish - New finish time.
Returns:
Logical-true if new finish set(>=0), else logical-false(<0).

setNodeColour

public boolean setNodeColour(java.awt.Color newColour)
Sets the node colour used by breadth/depth first search iterators.

Parameters:
newColour - New colour value.
Returns:
Returns logical-true if new colour set, else logical-false.

setNodePredecessor

public boolean setNodePredecessor(GraphTreeNode<NodeType> newPredecessor)
Parameters:
newPredecessor - New predecessor. Can be null, indicating that a node does not have a predcessor.
Returns:
Logiacl-true if the predecessor node is set, else logical-false.

childModelObjects

public java.util.Vector<ModelObject> childModelObjects()
Returns a vector of this object's child ModelObject objects.

Specified by:
childModelObjects in interface ModelObject
Returns:
A vector consisting of this object's node object.

databaseInsertStatement

public java.sql.PreparedStatement databaseInsertStatement(java.sql.Connection connection,
                                                          int modelID)
                                                   throws java.sql.SQLException
Returns a prepared statement for inserting (INSERT command) a ModelObject object into a database.

Specified by:
databaseInsertStatement in interface ModelObject
Parameters:
connection - Database connection.
modelID - Model ID.
Returns:
A prepared statement for inserting a ModelObject object into a database.
Throws:
java.sql.SQLException - Thrown if an SQL exception occurs.

databaseUpdateStatement

public java.sql.PreparedStatement databaseUpdateStatement(java.sql.Connection connection,
                                                          int modelID)
                                                   throws java.sql.SQLException
Returns a prepared statement for updating (UPDATE command) a ModelObject object into a database.

Specified by:
databaseUpdateStatement in interface ModelObject
Parameters:
connection - Database connection.
modelID - Model ID.
Returns:
A prepared statement for inserting a ModelObject object into a database.
Throws:
java.sql.SQLException - Thrown if an SQL exception occurs.

fromDatabase

public ModelObject fromDatabase(java.sql.Connection connection,
                                Model model,
                                int modelID,
                                int objectID)
                         throws java.sql.SQLException
Returns a model object from the specified database table.

Specified by:
fromDatabase in interface ModelObject
Parameters:
connection - Database connection.
model - Model built from database.
modelID - Model ID.
objectID - Object ID in associated ModelObjects table.
Returns:
ModelObject corresponding to the specified db connection, model ID and obect ID. A null value is returned if no match found.
Throws:
java.sql.SQLException - Thrown if an SQL exception occurs.

getNodeType

public java.lang.String getNodeType()
Returns the string equivalent of the generic type of the node object; eg "ModelDouble" in GraphTreeNode. If the node object is null then a new non-null empty string is returned.

Returns:
The type (as a string) of the node object.

toXMLNode

public org.w3c.dom.Element toXMLNode(org.w3c.dom.Document document,
                                     org.w3c.dom.Element documentRootNode)
                              throws InvalidObjectException
Returns the xml node of this object.

Specified by:
toXMLNode in interface ModelObject
Parameters:
document - XML document.
documentRootNode - Document root node; ie Model node.
Returns:
The xml node of this object.
Throws:
InvalidObjectException - Thrown if this object's ID is equal to the default ID of -1.

fromXMLNode

public ModelObject fromXMLNode(org.w3c.dom.Document document,
                               org.w3c.dom.Node documentRootNode,
                               org.w3c.dom.Node node,
                               Model model)
                        throws InvalidObjectException
Returns a model object from the specified xml node.

Specified by:
fromXMLNode in interface ModelObject
Parameters:
document - XML document.
documentRootNode - Document root node; ie Model node.
node - XML node.
model - Model to which node object is added.
Returns:
ModelObject corresponding to the specified node. A null value is returned if an error occurred building the object.
Throws:
InvalidObjectException - Thrown if an error occurred building the object.

toX3DNode

public X3DNode toX3DNode(X3DObject x3dObject)
                  throws InvalidObjectException
X3D support. Returns null since no X3D equivalent.

Specified by:
toX3DNode in interface ModelObject
Parameters:
x3dObject - X3D object.
Returns:
X3D object or null if object has no visualisation.
Throws:
InvalidObjectException - Thrown if this object is invalid.