Class TreeNode<T extends TreeNode>

  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Iterable<T>, Replicateable
    Direct Known Subclasses:
    Component, ComponentConfig

    public class TreeNode<T extends TreeNode>
    extends java.lang.Object
    implements Replicateable, java.lang.Iterable<T>
    A basic tree node implementation. Each node holds references to its children, and not its parent, thereby allowing a node to have multiple parents.

    Subclasses can override isSupportedChild(TreeNode) to restrict a node's children.

    A tree node is not thread safe. Instead, TreeNode implements Replicateable so that an entire tree structure can be safely copied to support multiple independent clients if needed.

    Since:
    2.0
    • Constructor Summary

      Constructors 
      Constructor Description
      TreeNode()
      Constructs a new TreeNode.
      TreeNode​(int size)
      Constructs a new TreeNode.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(T child)
      Adds a child to this node.
      T clone()
      Clones this node.
      T find​(java.lang.String name)
      Recursively finds the first descendant with the given name.
      java.util.List<T> getChildren()
      Returns the immediate children of this node.
      T getFirst()
      Returns the first child of this node.
      java.lang.String getName()
      Returns the name of this node.
      boolean isDescendant​(T node)
      Returns whether the given node is a descendant of this node or recursively one of its children.
      protected boolean isSupportedChild​(T child)
      Returns whether a node is a supported child of this node.
      java.util.Iterator<T> iterator()  
      void print()
      Prints this node and its descendants to standard out.
      void print​(java.io.PrintStream out)  
      void setName​(java.lang.String name)
      Sets the name of this node.
      int size()
      Returns the number of children this node has.
      void sort​(java.util.Comparator<? super T> comparator)
      Sorts all descendants of this node.
      protected void toParamString​(java.lang.StringBuilder s)
      Called by toString() to append node parameters to the output.
      java.lang.String toString()  
      void updateReferences​(java.util.Map<java.lang.Object,​java.lang.Object> map)
      Updates a node's references to other nodes.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • TreeNode

        public TreeNode()
        Constructs a new TreeNode.
      • TreeNode

        public TreeNode​(int size)
        Constructs a new TreeNode.
        Parameters:
        size - the initial size of the node for accommodating children
    • Method Detail

      • getName

        public java.lang.String getName()
        Returns the name of this node.
        Returns:
        the node name
      • setName

        public void setName​(java.lang.String name)
        Sets the name of this node.
        Parameters:
        name - the node name
      • find

        public T find​(java.lang.String name)
        Recursively finds the first descendant with the given name. All descendants of a child are checked first before moving to the next child.
        Parameters:
        name - the name of the node to find
        Returns:
        the matched node, or null if not found
      • isDescendant

        public boolean isDescendant​(T node)
        Returns whether the given node is a descendant of this node or recursively one of its children.
        Parameters:
        node - the TreeNode to test
        Returns:
        true if the given node is a descendant, false otherwise
      • getChildren

        public java.util.List<T> getChildren()
        Returns the immediate children of this node.
        Returns:
        the List of immediate children of this node
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T extends TreeNode>
      • getFirst

        public T getFirst()
        Returns the first child of this node.
        Returns:
        the first child of this node
        Throws:
        java.lang.NullPointerException - if this node does not have any children
      • add

        public void add​(T child)
                 throws java.lang.IllegalArgumentException
        Adds a child to this node.
        Parameters:
        child - the child to add
        Throws:
        java.lang.IllegalArgumentException - if the child is not supported by this node
      • size

        public int size()
        Returns the number of children this node has.
        Returns:
        the number of children
      • isSupportedChild

        protected boolean isSupportedChild​(T child)
        Returns whether a node is a supported child of this node. Called by add(TreeNode).
        Parameters:
        child - the node to test
        Returns:
        true if the child is allowed
      • sort

        public void sort​(java.util.Comparator<? super T> comparator)
        Sorts all descendants of this node.
        Parameters:
        comparator - the Comparator to use for comparing nodes
      • clone

        public T clone()
        Description copied from interface: Replicateable
        Clones this node.
        Specified by:
        clone in interface Replicateable
        Overrides:
        clone in class java.lang.Object
        Returns:
        the clone
      • updateReferences

        public void updateReferences​(java.util.Map<java.lang.Object,​java.lang.Object> map)
        Description copied from interface: Replicateable
        Updates a node's references to other nodes.
        Specified by:
        updateReferences in interface Replicateable
        Parameters:
        map - the (identity) map of clones by prior object reference
      • print

        public void print()
        Prints this node and its descendants to standard out.
      • print

        public void print​(java.io.PrintStream out)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toParamString

        protected void toParamString​(java.lang.StringBuilder s)
        Called by toString() to append node parameters to the output.
        Parameters:
        s - the output to append