Package org.synchronoss.cpo.core
Class Node
java.lang.Object
org.synchronoss.cpo.core.Node
- All Implemented Interfaces:
Serializable,Cloneable,Comparable<Node>
- Direct Known Subclasses:
BindableCpoWhere
This is a general Node class to be used to build different types of trees. There are very few
rules in this class as they should be implemented by users of this class. This Object is the
basis for the CompositePattern. It can be both a composite or a component node. The isLeaf flag
determines how it treats itself. It is important for the inheriting classes to call setLeaf() to
tell the Node how to act.
- Version:
- 1.0
- Author:
- David E. Berry
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbooleanImplements the visitor pattern.voidThis function adds a child to the linked-list of children for this node.voidaddChildSort(Node node) This function adds a child to the linked-list of children for this node.voidaddChildSort(Node node, Comparator<Node> c) Adds a child to the linked-list of children for this node, inserting it in sorted order.clone()intDefault natural ordering: creation order.static NodecreateNode(int nodeType) This is the factory method for creating Node objects.booleanbooleanGets whether this node is allowed to have children.intCounts the immediate children of this node.Collects the immediate children of this node into a list, in sibling order.Gets the first child node in the linked-list of children.Gets the next sibling for this node in the linked list of Nodes.Gets the parent node for this nodeGets the previous sibling for this node in the linked list of Nodes.inthashCode()booleanChecks to see if this node has a parent.voidinsertParentAfter(Node node) Inserts a new Parent Node as a child of this node and moves all pre-existing children to be children of the new Parent Node.voidinsertParentBefore(Node node) Inserts a new Parent into the tree structure and adds this node as its child.voidinsertSiblingAfter(Node node) Adds a Sibling immediately following this Node.voidinsertSiblingBefore(Node node) Inserts a Sibling into the linked list just prior to this NodebooleanisLeaf()Checks to see if this node is a leaf node, that is, if it has no children.voidrelease()Resets all the attributes to their default state.voidRemove this node and all its children from the tree.booleanremoveChild(Node node) Searches for an immediate child node and if found removes it from the linked-list of children.voidRemove just this node from the tree.voidsetFirstChild(Node node) Sets the first child node in the linked-list of children.voidsetNextSibling(Node node) Sets the NextSibling for this node.voidSets the Parent Node for this Node.voidsetPrevSibling(Node node) Sets the PrevSibling for this node.
-
Method Details
-
createNode
This is the factory method for creating Node objects.- Parameters:
nodeType- nodeType can be one of two values:Node.ParentNode Node.ChildNode
- Returns:
- an Instance of an Node
-
release
public void release()Resets all the attributes to their default state. -
getAllowChildren
public boolean getAllowChildren()Gets whether this node is allowed to have children.- Returns:
trueif this node may have children (it is a composite node),falseif it is a leaf-only component node
-
setParent
Sets the Parent Node for this Node.- Parameters:
node- The node that will become the parent.- See Also:
-
setPrevSibling
Sets the PrevSibling for this node. It also sets the NextSibling of the previous node to insure that the doubly-linked list is maintained.- Parameters:
node- The node that will become the previous Sibling
-
setNextSibling
Sets the NextSibling for this node. It also sets the PrevSibling of the next node to insure that the doubly-linked list is maintained.- Parameters:
node- The node that will become the next Sibling
-
getParentNode
Gets the parent node for this node- Returns:
- an Node representing the parent of this node or null if no parent exists.
-
getPrevSibling
Gets the previous sibling for this node in the linked list of Nodes.- Returns:
- an Node that represents the previous sibling or null if no sibling exists.
-
getNextSibling
Gets the next sibling for this node in the linked list of Nodes.- Returns:
- an Node that represents the next sibling or null if no sibling exists.
-
hasParent
public boolean hasParent()Checks to see if this node has a parent.- Returns:
- boolean indicating true if this node has a parent, false if it has no parent.
-
isLeaf
public boolean isLeaf()Checks to see if this node is a leaf node, that is, if it has no children.- Returns:
- boolean indicating true if it is a leafNode, false if not
-
addChild
This function adds a child to the linked-list of children for this node. It adds the child to the end of the list.- Parameters:
node- Node that is the node to be added as a child of this Node.- Throws:
ChildNodeException- throws an exception if this child is not allowed to have children
-
addChildSort
This function adds a child to the linked-list of children for this node. It adds the child to the end of the list.- Parameters:
node- Node that is the node to be added as a child of this Node.- Throws:
ChildNodeException- throws an exception if this node is not allowed to have children
-
addChildSort
Adds a child to the linked-list of children for this node, inserting it in sorted order.- Parameters:
node- the node to be added as a child of this nodec- the comparator used to determine sort order, ornullto use the nodes' natural ordering (compareTo(Node))- Throws:
ChildNodeException- throws an exception if this node is not allowed to have children
-
insertSiblingBefore
Inserts a Sibling into the linked list just prior to this Node- Parameters:
node- Node to be made the prevSibling- Throws:
ChildNodeException- an exception inserting the child node
-
insertSiblingAfter
Adds a Sibling immediately following this Node.- Parameters:
node- Node to be made the next sibling
-
insertParentBefore
Inserts a new Parent into the tree structure and adds this node as its child.- Parameters:
node- Node that will become this nodes new Parent.- Throws:
ChildNodeException- an exception adding the parent
-
insertParentAfter
Inserts a new Parent Node as a child of this node and moves all pre-existing children to be children of the new Parent Node.- Parameters:
node- Node to become a child of this node and parent to all pre-existing children of this node.- Throws:
ChildNodeException- an exception adding the parent
-
removeChild
Searches for an immediate child node and if found removes it from the linked-list of children.- Parameters:
node- Node to be searched for and removed if found.- Returns:
- true if removed
- Throws:
ChildNodeException- throws an exception if this node is not allowed to have children
-
removeChildNode
Remove just this node from the tree. The children of this node get attached to the parent.- Throws:
ChildNodeException- error removing the child node
-
removeAll
Remove this node and all its children from the tree.- Throws:
ChildNodeException- error removing the child node
-
getFirstChild
Gets the first child node in the linked-list of children.- Returns:
- Node reference to the first child node in the linked-list of children
-
setFirstChild
Sets the first child node in the linked-list of children.- Parameters:
node- Node which will be made the first child node in the linked-list of children.- Throws:
ChildNodeException- throws if this node is not allowed to have children
-
acceptDFVisitor
Implements the visitor pattern. This is a Depth-based traversal that will call the INodeVisitor visitBegin(), visitMiddle(), and visitEnd() for parent nodes and will call visit() for leaf nodes.- Parameters:
nv- INodeVisitor to call upon reaching a node when traversing the tree.- Returns:
- false to cancel the visitor
- See Also:
-
getChildCount
public int getChildCount()Counts the immediate children of this node.- Returns:
- the count of children
-
getChildList
Collects the immediate children of this node into a list, in sibling order.- Returns:
- the list of child nodes
-
clone
- Throws:
CloneNotSupportedException
-
compareTo
Default natural ordering: creation order. Ordering by creation serial (rather than identity hash code) keeps the Comparable contract — it never reports two distinct live nodes as equal, so sorted collections andaddChildSort(Node)behave deterministically. Subclasses with a meaningful value ordering should override this.- Specified by:
compareToin interfaceComparable<Node>
-
equals
-
hashCode
public int hashCode()
-