Verified Owner
Top Description Fields Constructors Methods
org.jabref.model

public abstract Class TreeNode<T extends TreeNode<T>>

extends Object
Class Inheritance
Known Direct Subclasses
org.jabref.model.groups.GroupTreeNode
Annotations
@SuppressWarnings:unchecked
Type Parameters
<T>
the type of the class
Imports
java.util.ArrayList, .Comparator, .List, .Optional, java.util.function.Consumer, .Predicate, java.util.stream.Stream, javafx.collections.FXCollections, .ObservableList, org.jspecify.annotations.NonNull, .Nullable

Represents a node in a tree.

Usually, tree nodes have a value property which allows access to the value stored in the node. In contrast to this approach, the TreeNode<T> class is designed to be used as a base class which provides the tree traversing functionality via inheritance.

Example usage: private class BasicTreeNode extends TreeNode<BasicTreeNode> { public BasicTreeNode() { super(BasicTreeNode.class); } }

This class started out as a copy of javax.swing.tree.DefaultMutableTreeNode.

Field Summary

Modifier and TypeField and Description
private final ObservableList<T>
children

Array of children, may be empty if this node has no children (but never null)

private Consumer<T>
onDescendantChanged

The function which is invoked when something changed in the subtree.

private @Nullable T
parent

This node's parent, or null if this node has no parent

private final ObservableList<T>
unmodifiableChildren

Cached read-only view of children to avoid allocating a new wrapper on every getChildren() call.

Constructor Summary

AccessConstructor and Description
public
TreeNode(Class<T>
class deriving from TreeNode. It should always be "T.class". We need this parameter since it is hard to get this information by other means.
derivingClass
)

Constructs a tree node without parent and no children.

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

the child node
addChild
(T
the node to add
child
)

Adds the node at the end the children collection.

public T

Returns:

the child node
addChild
(@NonNull T
the node to add
child
,
int
the position where the node should be added
index
)

Adds the node at the given position in the children collection.

protected boolean

Returns:

returns true when index is at least 0 and less then the count of children
childIndexExists
(int
the index to be tested
index
)

Returns whether the specified index is a valid index for a child.

public abstract T

Returns:

a deep copy of this node
copyNode
()

Creates a copy of this node, completely separated from the tree (i.e. no children and no parent)

public T

Returns:

a deep copy of the subtree
copySubtree
()

Creates a deep copy of this node and all of its children.

public List<T>
findChildrenSatisfying(Predicate<T> matcher)

Returns the group and any of its children in the tree satisfying the given condition.

public Optional<T>

Returns:

the node in this node's children collection at the specified index, or an empty Optional if the index does not point to a child
getChildAt
(int
an index into this node's children collection
index
)

Returns the child at the specified index in this node's children collection.

public ObservableList<T>

Returns:

a list of this node's children
getChildren
()

Gets a forward-order list of this node's children.

protected ObservableList<T>
public Optional<T>

Returns:

descendant found by evaluating indexedPath
getDescendant
(List<Integer>
sequence of child indices that describe a path from this node to one of its descendants. Be aware that if indexedPath was obtained by getIndexedPathFromRoot(), this node should usually be the root node.
indexedPath
)

Get the descendant of this node as indicated by the indexedPath.

public Optional<T>

Returns:

the first child of this node
getFirstChild
()

Returns this node's first child if it exists (otherwise returns an empty Optional).

public List<Integer>

Returns:

a list of numbers which represent an indexed path from the root node to this node
getIndexedPathFromRoot
()

Get the path from the root node to this node.

public Optional<Integer>

Returns:

an integer giving the index of the node in this node's child list or an empty Optional if the specified node is a not a child of this node
getIndexOfChild
(@NonNull T
the node to search for among this node's children
childNode
)

Gets the index of the specified child in this node's child list.

public Optional<T>

Returns:

the last child of this node
getLastChild
()

Returns this node's last child if it exists (otherwise returns an empty Optional).

public int

Returns:

an int giving the number of levels above this node
getLevel
()

Gets the number of levels above this node, i.e. the distance from the root to this node.

public Optional<T>

Returns:

the sibling of this node that immediately follows this node
getNextSibling
()

Returns the next sibling of this node in the parent's children list.

public int

Returns:

an int giving the number of children of this node
getNumberOfChildren
()

Returns the number of children of this node.

public Optional<T>

Returns:

this node's parent T, or an empty Optional if this node has no parent
getParent
()

Returns this node's parent or an empty Optional if this node has no parent.

public List<T>

Returns:

a list of nodes giving the path, where the first element in the path is the root and the last element is this node.
getPathFromRoot
()

Returns the path from the root, to get to this node.

public int

Returns:

the child index of this node in its parent
getPositionInParent
()

Get the child index of this node in its parent.

public Optional<T>

Returns:

the sibling of this node that immediately precedes this node
getPreviousSibling
()

Returns the previous sibling of this node in the parent's children list.

private Optional<T>
getRelativeSibling(int shiftIndex)

Returns the sibling which is shiftIndex away from this node.

public T

Returns:

the root of the tree that contains this node
getRoot
()

Returns the root of the tree that contains this node.

public boolean

Returns:

true if anotherNode is a descendant of this node
isAncestorOf
(@NonNull T
node to test
anotherNode
)

Returns true if this node is an ancestor of the given node.

public boolean

Returns:

true if this node has no children
isLeaf
()

Returns true if this node has no children.

public boolean

Returns:

true if this node is an ancestor of anotherNode
isNodeDescendant
(@NonNull T
node to test as descendant of this node
anotherNode
)

Returns true if anotherNode is a descendant of this node -- if it is this node, one of this node's children, or a descendant of one of this node's children.

public boolean

Returns:

true if this node is the root of its tree
isRoot
()

Returns true if this node is the root of the tree.

public Stream<T>
public void
moveAllChildrenTo(T
the new parent
target
,
int
the position where the children should be inserted
targetIndex
)

Removes all children from this node and makes them a child of the specified node by adding it to the specified position in the children list.

public void
moveTo(@NonNull T
the new parent
target
)

Removes this node from its parent and makes it a child of the specified node by adding it to the end of children list.

public void
moveTo(@NonNull T
the new parent
target
,
int
the position where the children should be inserted
targetIndex
)

Removes this node from its parent and makes it a child of the specified node by adding it to the specified position in the children list.

protected void
notifyAboutDescendantChange(T
the node which changed
source
)

Helper method which notifies all subscribers about a change in the subtree and bubbles the event to all parents.

public void
removeAllChildren()

Removes all of this node's children, setting their parents to empty.

public void
removeChild(@NonNull T
a child of this node to remove
child
)

Removes the given child from this node's child list, giving it an empty parent.

public void
removeChild(int
the index in this node's child array of the child to remove
childIndex
)

Removes the child at the specified index from this node's children and sets that node's parent to empty.

public void
removeFromParent()

Removes the subtree rooted at this node from the tree, giving this node an empty parent.

protected void
setParent(@Nullable T
the new parent
parent
)

Sets the parent node of this node.

public void
sortChildren(@NonNull Comparator<? super T>
the comparator used to compare the child nodes
comparator
,
boolean
if true the whole subtree is sorted
recursive
)

Sorts the list of children according to the order induced by the specified Comparator.

public void
subscribeToDescendantChanged(Consumer<T>
function to be invoked upon a change
subscriber
)

Adds the given function to the list of subscribers which are notified when something changes in the subtree.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

childrenback to summary
private final ObservableList<T> children

Array of children, may be empty if this node has no children (but never null)

onDescendantChangedback to summary
private Consumer<T> onDescendantChanged

The function which is invoked when something changed in the subtree.

parentback to summary
private @Nullable T parent

This node's parent, or null if this node has no parent

unmodifiableChildrenback to summary
private final ObservableList<T> unmodifiableChildren

Cached read-only view of children to avoid allocating a new wrapper on every getChildren() call.

Constructor Detail

TreeNodeback to summary
public TreeNode(Class<T> derivingClass)

Constructs a tree node without parent and no children.

Parameters
derivingClass:Class<T>

class deriving from TreeNode. It should always be "T.class". We need this parameter since it is hard to get this information by other means.

Method Detail

addChildback to summary
public T addChild(T child)

Adds the node at the end the children collection. Also sets the parent of the given node to this node. The given node is not allowed to already be in a tree (i.e. it has to have no parent).

Parameters
child:T

the node to add

Returns:T

the child node

addChildback to summary
public T addChild(@NonNull T child, int index)

Adds the node at the given position in the children collection. Also sets the parent of the given node to this node. The given node is not allowed to already be in a tree (i.e. it has to have no parent).

Parameters
child:@NonNull T

the node to add

index:int

the position where the node should be added

Returns:T

the child node

Exceptions
IndexOutOfBoundsException:
if the index is out of range
childIndexExistsback to summary
protected boolean childIndexExists(int index)

Returns whether the specified index is a valid index for a child.

Parameters
index:int

the index to be tested

Returns:boolean

returns true when index is at least 0 and less then the count of children

copyNodeback to summary
public abstract T copyNode()

Creates a copy of this node, completely separated from the tree (i.e. no children and no parent)

Returns:T

a deep copy of this node

copySubtreeback to summary
public T copySubtree()

Creates a deep copy of this node and all of its children.

Returns:T

a deep copy of the subtree

findChildrenSatisfyingback to summary
public List<T> findChildrenSatisfying(Predicate<T> matcher)

Returns the group and any of its children in the tree satisfying the given condition.

getChildAtback to summary
public Optional<T> getChildAt(int index)

Returns the child at the specified index in this node's children collection.

Parameters
index:int

an index into this node's children collection

Returns:Optional<T>

the node in this node's children collection at the specified index, or an empty Optional if the index does not point to a child

getChildrenback to summary
public ObservableList<T> getChildren()

Gets a forward-order list of this node's children.

The returned list is unmodifiable - use the add and remove methods to modify the nodes children. However, changing the nodes children (for example by calling moveTo) is reflected in a change of the list returned by getChildren. In other words, getChildren provides a read-only view on the children but not a copy.

Returns:ObservableList<T>

a list of this node's children

getChildrenInternalback to summary
protected ObservableList<T> getChildrenInternal()
getDescendantback to summary
public Optional<T> getDescendant(List<Integer> indexedPath)

Get the descendant of this node as indicated by the indexedPath.

If the path could not be traversed completely (i.e. one of the child indices did not exist), an empty Optional will be returned.

Parameters
indexedPath:List<Integer>

sequence of child indices that describe a path from this node to one of its descendants. Be aware that if indexedPath was obtained by getIndexedPathFromRoot(), this node should usually be the root node.

Returns:Optional<T>

descendant found by evaluating indexedPath

getFirstChildback to summary
public Optional<T> getFirstChild()

Returns this node's first child if it exists (otherwise returns an empty Optional).

Returns:Optional<T>

the first child of this node

getIndexedPathFromRootback to summary
public List<Integer> getIndexedPathFromRoot()

Get the path from the root node to this node.

The elements in the returned list represent the child index of each node in the path, starting at the root. If this node is the root node, the returned list has zero elements.

Returns:List<Integer>

a list of numbers which represent an indexed path from the root node to this node

getIndexOfChildback to summary
public Optional<Integer> getIndexOfChild(@NonNull T childNode)

Gets the index of the specified child in this node's child list.

If the specified node is not a child of this node, returns an empty Optional. This method performs a linear search and is O(n) where n is the number of children.

Parameters
childNode:@NonNull T

the node to search for among this node's children

Returns:Optional<Integer>

an integer giving the index of the node in this node's child list or an empty Optional if the specified node is a not a child of this node

Exceptions
NullPointerException:
if childNode is null
getLastChildback to summary
public Optional<T> getLastChild()

Returns this node's last child if it exists (otherwise returns an empty Optional).

Returns:Optional<T>

the last child of this node

getLevelback to summary
public int getLevel()

Gets the number of levels above this node, i.e. the distance from the root to this node.

If this node is the root, returns 0.

Returns:int

an int giving the number of levels above this node

getNextSiblingback to summary
public Optional<T> getNextSibling()

Returns the next sibling of this node in the parent's children list. Returns an empty Optional if this node has no parent or if it is the parent's last child.

This method performs a linear search that is O(n) where n is the number of children. To traverse the entire children collection, use the parent's getChildren() instead.

Returns:Optional<T>

the sibling of this node that immediately follows this node

See Also
getChildren
getNumberOfChildrenback to summary
public int getNumberOfChildren()

Returns the number of children of this node.

Returns:int

an int giving the number of children of this node

getParentback to summary
public Optional<T> getParent()

Returns this node's parent or an empty Optional if this node has no parent.

Returns:Optional<T>

this node's parent T, or an empty Optional if this node has no parent

getPathFromRootback to summary
public List<T> getPathFromRoot()

Returns the path from the root, to get to this node. The last element in the path is this node.

Returns:List<T>

a list of nodes giving the path, where the first element in the path is the root and the last element is this node.

getPositionInParentback to summary
public int getPositionInParent()

Get the child index of this node in its parent.

If this node is a root, then an UnsupportedOperationException is thrown. Use the isRoot method to check for this case.

Returns:int

the child index of this node in its parent

getPreviousSiblingback to summary
public Optional<T> getPreviousSibling()

Returns the previous sibling of this node in the parent's children list. Returns an empty Optional if this node has no parent or is the parent's first child.

This method performs a linear search that is O(n) where n is the number of children.

Returns:Optional<T>

the sibling of this node that immediately precedes this node

See Also
getChildren
getRelativeSiblingback to summary
private Optional<T> getRelativeSibling(int shiftIndex)

Returns the sibling which is shiftIndex away from this node.

getRootback to summary
public T getRoot()

Returns the root of the tree that contains this node. The root is the ancestor with an empty parent. Thus a node without a parent is considered its own root.

Returns:T

the root of the tree that contains this node

isAncestorOfback to summary
public boolean isAncestorOf(@NonNull T anotherNode)

Returns true if this node is an ancestor of the given node.

A node is considered an ancestor of itself.

Parameters
anotherNode:@NonNull T

node to test

Returns:boolean

true if anotherNode is a descendant of this node

Exceptions
NullPointerException:
if anotherNode is null
See Also
isNodeDescendant
isLeafback to summary
public boolean isLeaf()

Returns true if this node has no children.

Returns:boolean

true if this node has no children

isNodeDescendantback to summary
public boolean isNodeDescendant(@NonNull T anotherNode)

Returns true if anotherNode is a descendant of this node -- if it is this node, one of this node's children, or a descendant of one of this node's children. Note that a node is considered a descendant of itself.

If anotherNode is null, an exception is thrown.

Parameters
anotherNode:@NonNull T

node to test as descendant of this node

Returns:boolean

true if this node is an ancestor of anotherNode

See Also
isAncestorOf
isRootback to summary
public boolean isRoot()

Returns true if this node is the root of the tree. The root is the only node in the tree with an empty parent; every tree has exactly one root.

Returns:boolean

true if this node is the root of its tree

iterateOverTreeback to summary
public Stream<T> iterateOverTree()
moveAllChildrenToback to summary
public void moveAllChildrenTo(T target, int targetIndex)

Removes all children from this node and makes them a child of the specified node by adding it to the specified position in the children list.

Parameters
target:T

the new parent

targetIndex:int

the position where the children should be inserted

Exceptions
NullPointerException:
if target is null
ArrayIndexOutOfBoundsException:
if targetIndex is out of bounds
UnsupportedOperationException:
if target is an descendant of one of the children of this node
moveToback to summary
public void moveTo(@NonNull T target)

Removes this node from its parent and makes it a child of the specified node by adding it to the end of children list. In this way the whole subtree based at this node is moved to the given node.

Parameters
target:@NonNull T

the new parent

Exceptions
NullPointerException:
if target is null
ArrayIndexOutOfBoundsException:
if targetIndex is out of bounds
UnsupportedOperationException:
if target is an descendant of this node
moveToback to summary
public void moveTo(@NonNull T target, int targetIndex)

Removes this node from its parent and makes it a child of the specified node by adding it to the specified position in the children list. In this way the whole subtree based at this node is moved to the given node.

Parameters
target:@NonNull T

the new parent

targetIndex:int

the position where the children should be inserted

Exceptions
NullPointerException:
if target is null
ArrayIndexOutOfBoundsException:
if targetIndex is out of bounds
UnsupportedOperationException:
if target is an descendant of this node
notifyAboutDescendantChangeback to summary
protected void notifyAboutDescendantChange(T source)

Helper method which notifies all subscribers about a change in the subtree and bubbles the event to all parents.

Parameters
source:T

the node which changed

removeAllChildrenback to summary
public void removeAllChildren()

Removes all of this node's children, setting their parents to empty. If this node has no children, this method does nothing.

removeChildback to summary
public void removeChild(@NonNull T child)

Removes the given child from this node's child list, giving it an empty parent.

Parameters
child:@NonNull T

a child of this node to remove

removeChildback to summary
public void removeChild(int childIndex)

Removes the child at the specified index from this node's children and sets that node's parent to empty.

Does nothing if the index does not point to a child.

Parameters
childIndex:int

the index in this node's child array of the child to remove

removeFromParentback to summary
public void removeFromParent()

Removes the subtree rooted at this node from the tree, giving this node an empty parent. Does nothing if this node is the root of it tree.

setParentback to summary
protected void setParent(@Nullable T parent)

Sets the parent node of this node.

This method does not add this node to the children collection of the new parent nor does it remove this node from the old parent. You should probably call moveTo or remove to change the tree.

Parameters
parent:@Nullable T

the new parent

sortChildrenback to summary
public void sortChildren(@NonNull Comparator<? super T> comparator, boolean recursive)

Sorts the list of children according to the order induced by the specified Comparator.

All children must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any children e1 and e2 in the list).

Parameters
comparator:@NonNull Comparator<? super T>

the comparator used to compare the child nodes

recursive:boolean

if true the whole subtree is sorted

Exceptions
NullPointerException:
if the comparator is null
subscribeToDescendantChangedback to summary
public void subscribeToDescendantChanged(Consumer<T> subscriber)

Adds the given function to the list of subscribers which are notified when something changes in the subtree.

The following events are supported (the text in parentheses specifies which node is passed as the source):

  • addChild (new parent)
  • removeChild (old parent)
  • move (old parent and new parent)
Parameters
subscriber:Consumer<T>

function to be invoked upon a change