Class Node

java.lang.Object
org.synchronoss.cpo.core.Node
All Implemented Interfaces:
Serializable, Cloneable, Comparable<Node>
Direct Known Subclasses:
BindableCpoWhere

public class Node extends Object implements Serializable, Cloneable, Comparable<Node>
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 Type
    Method
    Description
    boolean
    Implements the visitor pattern.
    void
    addChild(Node node)
    This function adds a child to the linked-list of children for this node.
    void
    This function adds a child to the linked-list of children for this node.
    void
    Adds a child to the linked-list of children for this node, inserting it in sorted order.
     
    int
    Default natural ordering: creation order.
    static Node
    createNode(int nodeType)
    This is the factory method for creating Node objects.
    boolean
     
    boolean
    Gets whether this node is allowed to have children.
    int
    Counts 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 node
    Gets the previous sibling for this node in the linked list of Nodes.
    int
     
    boolean
    Checks to see if this node has a parent.
    void
    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.
    void
    Inserts a new Parent into the tree structure and adds this node as its child.
    void
    Adds a Sibling immediately following this Node.
    void
    Inserts a Sibling into the linked list just prior to this Node
    boolean
    Checks to see if this node is a leaf node, that is, if it has no children.
    void
    Resets all the attributes to their default state.
    void
    Remove this node and all its children from the tree.
    boolean
    Searches for an immediate child node and if found removes it from the linked-list of children.
    void
    Remove just this node from the tree.
    void
    Sets the first child node in the linked-list of children.
    void
    Sets the NextSibling for this node.
    void
    Sets the Parent Node for this Node.
    void
    Sets the PrevSibling for this node.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • createNode

      public static Node createNode(int nodeType)
      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:
      true if this node may have children (it is a composite node), false if it is a leaf-only component node
    • setParent

      public void setParent(Node node)
      Sets the Parent Node for this Node.
      Parameters:
      node - The node that will become the parent.
      See Also:
    • setPrevSibling

      public void setPrevSibling(Node node)
      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

      public void setNextSibling(Node node)
      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

      public Node getParentNode()
      Gets the parent node for this node
      Returns:
      an Node representing the parent of this node or null if no parent exists.
    • getPrevSibling

      public Node 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

      public Node 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

      public void addChild(Node node) throws ChildNodeException
      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

      public void addChildSort(Node node) throws ChildNodeException
      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

      public void addChildSort(Node node, Comparator<Node> c) throws ChildNodeException
      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 node
      c - the comparator used to determine sort order, or null to use the nodes' natural ordering (compareTo(Node))
      Throws:
      ChildNodeException - throws an exception if this node is not allowed to have children
    • insertSiblingBefore

      public void insertSiblingBefore(Node node) throws ChildNodeException
      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

      public void insertSiblingAfter(Node node)
      Adds a Sibling immediately following this Node.
      Parameters:
      node - Node to be made the next sibling
    • insertParentBefore

      public void insertParentBefore(Node node) throws ChildNodeException
      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

      public void insertParentAfter(Node node) throws ChildNodeException
      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

      public boolean removeChild(Node node) throws ChildNodeException
      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

      public void removeChildNode() throws ChildNodeException
      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

      public void removeAll() throws ChildNodeException
      Remove this node and all its children from the tree.
      Throws:
      ChildNodeException - error removing the child node
    • getFirstChild

      public 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

      public void setFirstChild(Node node) throws ChildNodeException
      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

      public boolean acceptDFVisitor(NodeVisitor nv)
      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

      public List<Node> getChildList()
      Collects the immediate children of this node into a list, in sibling order.
      Returns:
      the list of child nodes
    • clone

      public Object clone() throws CloneNotSupportedException
      Throws:
      CloneNotSupportedException
    • compareTo

      public int compareTo(Node o)
      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 and addChildSort(Node) behave deterministically. Subclasses with a meaningful value ordering should override this.
      Specified by:
      compareTo in interface Comparable<Node>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object