Top Description Fields Constructors Methods
io.github.kusoroadeolu.clique.components

public Class Tree

extends Object
implements Component
Class Inheritance
All Implemented Interfaces
io.github.kusoroadeolu.clique.components.Component
Annotations
@Stable
since:3.1.0
Imports
io.github.kusoroadeolu.clique.configuration.TreeConfiguration, io.github.kusoroadeolu.clique.internal.documentation.Stable, io.github.kusoroadeolu.clique.internal.utils.StringUtils, io.github.kusoroadeolu.clique.spi.AnsiCode, io.github.kusoroadeolu.clique.style.StyleBuilder, java.util.*

A tree component that renders hierarchical data as an indented, connector-linked structure.

Each Tree instance represents a node. Children are added via add(String) or add(Tree), and both return the child node, allowing subtrees to be built by chaining calls on the returned reference.

Rendering is performed from the root node via get(). Calling get() on a child node renders only the subtree rooted at that node.

Note

This class is not thread-safe.

Since
3.1.0

Field Summary

Modifier and TypeField and Description
private final List<Tree>
pack-priv static final String
pack-priv static final String
private final AnsiCode[]
pack-priv static final String
private final String
private Tree
pack-priv static final String
private final TreeConfiguration

Constructor Summary

AccessConstructor and Description
public
Tree(String label)

public
Tree(String label, TreeConfiguration treeConfiguration)

private
Tree(String label, TreeConfiguration treeConfiguration, Tree parent)

Method Summary

Modifier and TypeMethod and Description
public Tree

Returns:

the newly created child node
add
(String
the label for the child node; must not be null
label
)

Creates a new child node with the given label and appends it to this node's children.

public Tree

Returns:

the provided child node
add
(Tree
the tree to append as a child; must not be null or this node
tree
)

Appends an existing Tree as a child of this node.

pack-priv void
private void
buildTree(Tree node, String prefix, boolean isLast, StyleBuilder sb)

pack-priv List<Tree>
public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public String

Returns:

the rendered tree as a string
get
()

Implements io.github.kusoroadeolu.clique.components.Component.get.

Renders this node and all its descendants as a formatted tree string.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public Optional<Tree>

Returns:

an Optional containing the parent node, or empty if none
parent
()

Returns the parent of this node, or an empty Optional if this is the root.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

pack-priv void
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

childrenback to summary
private final List<Tree> children
CONNECTING_LINEback to summary
pack-priv static final String CONNECTING_LINE
CONNECTORback to summary
pack-priv static final String CONNECTOR
connectorColorback to summary
private final AnsiCode[] connectorColor
END_CONNECTORback to summary
pack-priv static final String END_CONNECTOR
labelback to summary
private final String label
parentback to summary
private Tree parent
SPACEback to summary
pack-priv static final String SPACE
treeConfigurationback to summary
private final TreeConfiguration treeConfiguration

Constructor Detail

Treeback to summary
public Tree(String label)
Treeback to summary
public Tree(String label, TreeConfiguration treeConfiguration)
Treeback to summary
private Tree(String label, TreeConfiguration treeConfiguration, Tree parent)

Method Detail

addback to summary
public Tree add(String label)

Creates a new child node with the given label and appends it to this node's children.

The child inherits this node's TreeConfiguration. The returned node is the newly created child, not this node.

Parameters
label:String

the label for the child node; must not be null

Returns:Tree

the newly created child node

Exceptions
NullPointerException:
if label is null
addback to summary
public Tree add(Tree tree)

Appends an existing Tree as a child of this node.

The provided tree is re-parented to this node. The returned node is the provided child, not this node.

Parameters
tree:Tree

the tree to append as a child; must not be null or this node

Returns:Tree

the provided child node

Exceptions
NullPointerException:
if tree is null
UnsupportedOperationException:
if tree is this node
assertNotSelfback to summary
pack-priv void assertNotSelf(Tree tree)
buildTreeback to summary
private void buildTree(Tree node, String prefix, boolean isLast, StyleBuilder sb)
childrenback to summary
pack-priv List<Tree> children()
equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
getback to summary
public String get()

Implements io.github.kusoroadeolu.clique.components.Component.get.

Renders this node and all its descendants as a formatted tree string.

If a parser is configured via TreeConfiguration, markup in node labels is resolved before the string is returned.

Returns:String

the rendered tree as a string

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
parentback to summary
public Optional<Tree> parent()

Returns the parent of this node, or an empty Optional if this is the root.

Returns:Optional<Tree>

an Optional containing the parent node, or empty if none

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
validateLabelback to summary
pack-priv void validateLabel(String label)