Package org.beanio.internal.util
Class TreeNode<T extends TreeNode>
- java.lang.Object
-
- org.beanio.internal.util.TreeNode<T>
-
- 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,
TreeNodeimplementsReplicateableso that an entire tree structure can be safely copied to support multiple independent clients if needed.- Since:
- 2.0
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(T child)Adds a child to this node.Tclone()Clones this node.Tfind(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.TgetFirst()Returns the first child of this node.java.lang.StringgetName()Returns the name of this node.booleanisDescendant(T node)Returns whether the given node is a descendant of this node or recursively one of its children.protected booleanisSupportedChild(T child)Returns whether a node is a supported child of this node.java.util.Iterator<T>iterator()voidprint()Prints this node and its descendants to standard out.voidprint(java.io.PrintStream out)voidsetName(java.lang.String name)Sets the name of this node.intsize()Returns the number of children this node has.voidsort(java.util.Comparator<? super T> comparator)Sorts all descendants of this node.protected voidtoParamString(java.lang.StringBuilder s)Called bytoString()to append node parameters to the output.java.lang.StringtoString()voidupdateReferences(java.util.Map<java.lang.Object,java.lang.Object> map)Updates a node's references to other nodes.
-
-
-
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()
-
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 byadd(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:ReplicateableClones this node.- Specified by:
clonein interfaceReplicateable- Overrides:
clonein classjava.lang.Object- Returns:
- the clone
-
updateReferences
public void updateReferences(java.util.Map<java.lang.Object,java.lang.Object> map)
Description copied from interface:ReplicateableUpdates a node's references to other nodes.- Specified by:
updateReferencesin interfaceReplicateable- 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:
toStringin classjava.lang.Object
-
toParamString
protected void toParamString(java.lang.StringBuilder s)
Called bytoString()to append node parameters to the output.- Parameters:
s- the output to append
-
-