public class TreeNode<T extends TreeNode> extends Object implements Replicateable, Iterable<T>
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.
Constructor and Description |
---|
TreeNode()
Constructs a new TreeNode.
|
TreeNode(int size)
Constructs a new TreeNode.
|
Modifier and Type | Method and Description |
---|---|
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.
|
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.
|
Iterator<T> |
iterator() |
void |
print()
Prints this node and its descendants to standard out.
|
void |
print(PrintStream 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.
|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
public TreeNode()
public TreeNode(int size)
size
- the initial size of the node for accommodating childrenpublic String getName()
public void setName(String name)
name
- the node namepublic T find(String name)
name
- the name of the node to findpublic boolean isDescendant(T node)
node
- the TreeNode to testpublic List<T> getChildren()
public T getFirst()
NullPointerException
- if this node does not have any childrenpublic void add(T child) throws IllegalArgumentException
child
- the child to addIllegalArgumentException
- if the child is not supported by this nodepublic int size()
protected boolean isSupportedChild(T child)
add(TreeNode)
.child
- the node to testpublic void sort(Comparator<? super T> comparator)
comparator
- the Comparator to use for comparing nodespublic T clone()
Replicateable
clone
in interface Replicateable
clone
in class Object
public void updateReferences(Map<Object,Object> map)
Replicateable
updateReferences
in interface Replicateable
map
- the (identity) map of clones by prior object referencepublic void print()
public void print(PrintStream out)
protected void toParamString(StringBuilder s)
toString()
to append node parameters to the output.s
- the output to append