BeanIO 2.0.4

org.beanio.internal.util
Class TreeNode<T extends TreeNode>

java.lang.Object
  extended by org.beanio.internal.util.TreeNode<T>
All Implemented Interfaces:
Cloneable, Iterable<T>, Replicateable
Direct Known Subclasses:
Component, ComponentConfig

public class TreeNode<T extends TreeNode>
extends Object
implements Replicateable, 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
Author:
Kevin Seim

Constructor Summary
TreeNode()
          Constructs a new TreeNode.
TreeNode(int size)
          Constructs a new TreeNode.
 
Method Summary
 void add(T child)
          Adds a child to this node.
 T clone()
          Clones this node.
 T find(String name)
          Recursively finds the first descendant with the given name.
 List<T> getChildren()
          Returns the immediate children of this node.
 T getFirst()
          Returns the first child of this node.
 String getName()
          Returns the name of this node.
protected  boolean isSupportedChild(T child)
          Returns whether a node is a supported child of this node.
 Iterator<T> iterator()
           
 void print()
          Prints this node and its descendants to standard out.
 void setName(String name)
          Sets the name of this node.
 int size()
          Returns the number of children this node has.
 void sort(Comparator<? super T> comparator)
          Sorts all descendants of this node.
protected  void toParamString(StringBuilder s)
          Called by toString() to append node parameters to the output.
 String toString()
           
 void updateReferences(Map<Object,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
 

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 String getName()
Returns the name of this node.

Returns:
the node name

setName

public void setName(String name)
Sets the name of this node.

Parameters:
name - the node name

find

public T find(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

getChildren

public List<T> getChildren()
Returns the immediate children of this node.

Returns:
the List of immediate children of this node

iterator

public Iterator<T> iterator()
Specified by:
iterator in interface Iterable<T extends TreeNode>

getFirst

public T getFirst()
Returns the first child of this node.

Returns:
the first child of this node
Throws:
NullPointerException - if this node does not have any children

add

public void add(T child)
         throws IllegalArgumentException
Adds a child to this node.

Parameters:
child - the child to add
Throws:
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(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 Object
Returns:
the clone

updateReferences

public void updateReferences(Map<Object,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.


toString

public String toString()
Overrides:
toString in class Object

toParamString

protected void toParamString(StringBuilder s)
Called by toString() to append node parameters to the output.

Parameters:
s - the output to append

BeanIO 2.0.4

Copyright © 2010-2013 Kevin Seim