public final class TreeNode<T> extends Object
Class for manually constructing a tree, or for copying an existing tree.
Constructor and Description |
---|
TreeNode(TreeNode<T> parent,
T content)
Creates a TreeNode with the given parent and content.
|
TreeNode(TreeNode<T> parent,
T content,
int childCapacity)
Creates a TreeNode with the given parent, content, and initial child capacity.
|
Modifier and Type | Method and Description |
---|---|
TreeNode<T> |
copy()
Creates a deep copy of this TreeNode.
|
static <T> TreeNode<T> |
copy(TreeDef<T> treeDef,
T root)
Creates a hierarchy of TreeNodes that copies the structure and content of the given tree.
|
static <T,R> TreeNode<R> |
copy(TreeDef<T> treeDef,
T root,
Function<? super T,? extends R> mapper)
Creates a hierarchy of TreeNodes that copies the structure and content of the given tree, using
mapper to calculate the content of the nodes. |
static TreeNode<String> |
createTestData(String... testData)
Creates a hierarchy of
TreeNode<String> using an easy-to-read array of strings. |
TreeNode<T> |
findByContent(T content)
Searches breadth-first for the TreeNode with the given content.
|
TreeNode<T> |
findByPath(List<T> path) |
TreeNode<T> |
findByPath(T... path)
Finds a child TreeNode based on its path.
|
List<TreeNode<T>> |
getChildren()
Returns the children of this TreeNode.
|
T |
getContent()
Returns the object which is encapsulated by this TreeNode.
|
TreeNode<T> |
getParent()
Returns the (possibly-null) parent of this TreeNode.
|
String |
getPath()
Returns the path of this node, using the content’s
toString() method and / as the delimiter. |
String |
getPath(Function<? super T,String> toString)
Returns the path of this node, using the given
toString method and / as the delimiter. |
String |
getPath(Function<? super T,String> toString,
String delimiter)
Returns the path of this node, using the given
toString method and delimiter . |
void |
removeFromParent()
Removes this TreeNode from its parent.
|
void |
setContent(T content)
Sets the object which is encapsulated by this TreeNode.
|
void |
setParent(TreeNode<T> parent)
Sets the parent for this TreeNode.
|
void |
sortChildrenByContent(Comparator<? super T> comparator)
Recursively sorts all children using the given comparator of their content.
|
void |
sortChildrenByNode(Comparator<TreeNode<T>> comparator)
Recursively sorts all children using the given comparator of TreeNode.
|
String |
toString() |
String |
toStringDeep()
Returns a “deep” toString, including the entire tree below this level.
|
static <T> TreeDef.Parented<TreeNode<T>> |
treeDef()
TreeDef.Parented for TreeNodes. |
public TreeNode(@Nullable TreeNode<T> parent, T content)
Creates a TreeNode with the given parent and content. Automatically adds itself as a child of its parent.
public T getContent()
Returns the object which is encapsulated by this TreeNode.
public void setContent(T content)
Sets the object which is encapsulated by this TreeNode.
public void removeFromParent()
Removes this TreeNode from its parent.
public String getPath()
Returns the path of this node, using the content’s toString()
method and /
as the delimiter.
public String getPath(Function<? super T,String> toString)
Returns the path of this node, using the given toString
method and /
as the delimiter.
public String getPath(Function<? super T,String> toString, String delimiter)
Returns the path of this node, using the given toString
method and delimiter
.
public String toStringDeep()
Returns a “deep” toString, including the entire tree below this level.
public void sortChildrenByContent(Comparator<? super T> comparator)
Recursively sorts all children using the given comparator of their content.
public void sortChildrenByNode(Comparator<TreeNode<T>> comparator)
Recursively sorts all children using the given comparator of TreeNode.
public static <T> TreeNode<T> copy(TreeDef<T> treeDef, T root)
Creates a hierarchy of TreeNodes that copies the structure and content of the given tree.
public static <T,R> TreeNode<R> copy(TreeDef<T> treeDef, T root, Function<? super T,? extends R> mapper)
Creates a hierarchy of TreeNodes that copies the structure and content of the given tree, using mapper
to calculate the content of the nodes.
public static <T> TreeDef.Parented<TreeNode<T>> treeDef()
TreeDef.Parented
for TreeNodes.
public static TreeNode<String> createTestData(String... testData)
Creates a hierarchy of TreeNode<String>
using an easy-to-read array of strings.
Spaces are used to represent parent / child relationships, e.g.
TreeNode<String> root = createTestData( “root”, “ bigNode1”, “ bigNode2”, “ child1”, “ child2”, “ bigNode3” );There can only be one root node, and that is the node that is returned.
public TreeNode<T> findByPath(T... path)
Finds a child TreeNode based on its path.
Searches the child nodes for the first element, then that node’s children for the second element, etc.
IllegalArgumentException
- if no such node existspublic TreeNode<T> findByPath(List<T> path)
findByPath(Object...)
public TreeNode<T> findByContent(T content)
Searches breadth-first for the TreeNode with the given content.
IllegalArgumentException
- if no such node exists