- java.lang.Object
-
- eu.ess.xaos.ui.control.tree.TreeItemWalker<T>
-
- Type Parameters:
T
- The type of theTreeItem
s.
public class TreeItemWalker<T> extends Object implements Iterator<TreeItem<T>>, Iterable<TreeItem<T>>
Walks aTreeItem
s tree depth-first. It supports streaming, visitor pattern and iterator pattern.Important Note #1: Once used, a
TreeItemWalker
cannot be reused. A new instance has to be created instead.Important Note #2:
TreeItem
s added to the tree being walked will be walked only if they are part of the sub-tree not yet visited. Generally speaking no changes should be done to the tree structure during a walk.Important Note #3: This implementation is not synchronized. Calling
getDepth()
,hasNext()
,next()
and/orstream()
's methods from different threads concurrently can produce odd results. CallbuildSynchronized
to get a synchronized version of the walker.- Author:
- claudio.rosati@esss.se
- See Also:
- Iterate TreeView nodes
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
TreeItemWalker(TreeItem<T> root)
Initialize the walker with the givenroot
item.protected
TreeItemWalker(TreeTableView<T> view)
Initialize the walker with theTreeTableView.getRoot()
from the givenview
.protected
TreeItemWalker(TreeView<T> view)
Initialize the walker with theTreeView.getRoot()
from the givenview
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> TreeItemWalker<T>
build(TreeItem<T> root)
Returns a walker initialized with the givenroot
item.static <T> TreeItemWalker<T>
build(TreeTableView<T> view)
Returns a walker initialized with theTreeTableView.getRoot()
from the givenview
.static <T> TreeItemWalker<T>
build(TreeView<T> view)
Returns a walker initialized with theTreeView.getRoot()
from the givenview
.static <T> TreeItemWalker<T>
buildSynchronized(TreeItem<T> root)
Returns a synchronized walker initialized with the givenroot
item.static <T> TreeItemWalker<T>
buildSynchronized(TreeTableView<T> view)
Returns a synchronized walker initialized with theTreeTableView.getRoot()
from the givenview
.static <T> TreeItemWalker<T>
buildSynchronized(TreeView<T> view)
Returns a synchronized walker initialized with theTreeView.getRoot()
from the givenview
.protected Stream<TreeItem<T>>
createStream()
int
getDepth()
boolean
hasNext()
Iterator<TreeItem<T>>
iterator()
TreeItem<T>
next()
Spliterator<TreeItem<T>>
spliterator()
Stream<TreeItem<T>>
stream()
static <T> void
visit(TreeItem<T> root, BiConsumer<TreeItem<T>,Integer> visitor)
Walks over the given treeroot
and calls the consumer for each tree item.static <T> void
visit(TreeItem<T> root, Consumer<TreeItem<T>> visitor)
Walks over the given treeroot
and calls the consumer for each tree item.static <T> void
visit(TreeTableView<T> tree, BiConsumer<TreeItem<T>,Integer> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item.static <T> void
visit(TreeTableView<T> tree, Consumer<TreeItem<T>> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item.static <T> void
visit(TreeView<T> tree, BiConsumer<TreeItem<T>,Integer> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item.static <T> void
visit(TreeView<T> tree, Consumer<TreeItem<T>> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item.static <T> void
visitValue(TreeItem<T> root, BiConsumer<T,Integer> visitor)
Walks over the given treeroot
and calls the consumer for each tree item's value.static <T> void
visitValue(TreeItem<T> root, Consumer<T> visitor)
Walks over the given treeroot
and calls the consumer for each tree item's value.static <T> void
visitValue(TreeTableView<T> tree, BiConsumer<T,Integer> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item's value.static <T> void
visitValue(TreeTableView<T> tree, Consumer<T> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item's value.static <T> void
visitValue(TreeView<T> tree, BiConsumer<T,Integer> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item's value.static <T> void
visitValue(TreeView<T> tree, Consumer<T> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item's value.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
-
-
-
Constructor Detail
-
TreeItemWalker
protected TreeItemWalker(TreeItem<T> root)
Initialize the walker with the givenroot
item.- Parameters:
root
- TheTreeItem
being the root to be walked depth-first. Can benull
, in which case an empty walker will be created.
-
TreeItemWalker
protected TreeItemWalker(TreeView<T> view)
Initialize the walker with theTreeView.getRoot()
from the givenview
.- Parameters:
view
- TheTreeView
whose root element has to be walked depth-first. Can benull
, or having anull
root element, in which case an empty walker will be created.
-
TreeItemWalker
protected TreeItemWalker(TreeTableView<T> view)
Initialize the walker with theTreeTableView.getRoot()
from the givenview
.- Parameters:
view
- TheTreeTableView
whose root element has to be walked depth-first. Can benull
, or having anull
root element, in which case an empty walker will be created.
-
-
Method Detail
-
build
public static <T> TreeItemWalker<T> build(TreeItem<T> root)
Returns a walker initialized with the givenroot
item.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
root
- TheTreeItem
being the root to be walked depth-first. Can benull
, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalker
initialized with the givenroot
parameter.
-
build
public static <T> TreeItemWalker<T> build(TreeView<T> view)
Returns a walker initialized with theTreeView.getRoot()
from the givenview
.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
view
- TheTreeView
whose root element has to be walked depth-first. Can benull
, or having anull
root element, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalker
initialized with the givenview
parameter.
-
build
public static <T> TreeItemWalker<T> build(TreeTableView<T> view)
Returns a walker initialized with theTreeTableView.getRoot()
from the givenview
.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
view
- TheTreeTableView
whose root element has to be walked depth-first. Can benull
, or having anull
root element, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalker
initialized with the givenview
parameter.
-
buildSynchronized
public static <T> TreeItemWalker<T> buildSynchronized(TreeItem<T> root)
Returns a synchronized walker initialized with the givenroot
item.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
root
- TheTreeItem
being the root to be walked depth-first. Can benull
, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalker
initialized with the givenroot
parameter.
-
buildSynchronized
public static <T> TreeItemWalker<T> buildSynchronized(TreeView<T> view)
Returns a synchronized walker initialized with theTreeView.getRoot()
from the givenview
.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
view
- TheTreeView
whose root element has to be walked depth-first. Can benull
, or having anull
root element, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalker
initialized with the givenview
parameter.
-
buildSynchronized
public static <T> TreeItemWalker<T> buildSynchronized(TreeTableView<T> view)
Returns a synchronized walker initialized with theTreeTableView.getRoot()
from the givenview
.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
view
- TheTreeTableView
whose root element has to be walked depth-first. Can benull
, or having anull
root element, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalker
initialized with the givenview
parameter.
-
visit
public static <T> void visit(TreeItem<T> root, Consumer<TreeItem<T>> visitor)
Walks over the given treeroot
and calls the consumer for each tree item.
-
visit
public static <T> void visit(TreeItem<T> root, BiConsumer<TreeItem<T>,Integer> visitor)
Walks over the given treeroot
and calls the consumer for each tree item.
-
visit
public static <T> void visit(TreeView<T> tree, Consumer<TreeItem<T>> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item.
-
visit
public static <T> void visit(TreeView<T> tree, BiConsumer<TreeItem<T>,Integer> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item.
-
visit
public static <T> void visit(TreeTableView<T> tree, Consumer<TreeItem<T>> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
tree
- TheTreeTableView
whose root has to be visited.visitor
- The visitor receiving the visitedTreeItem
during the tree walk.
-
visit
public static <T> void visit(TreeTableView<T> tree, BiConsumer<TreeItem<T>,Integer> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
tree
- TheTreeTableView
whose root has to be visited.visitor
- The visitor receiving the visitedTreeItem
and the current walking depth during the tree walk.
-
visitValue
public static <T> void visitValue(TreeItem<T> root, Consumer<T> visitor)
Walks over the given treeroot
and calls the consumer for each tree item's value.
-
visitValue
public static <T> void visitValue(TreeItem<T> root, BiConsumer<T,Integer> visitor)
Walks over the given treeroot
and calls the consumer for each tree item's value.
-
visitValue
public static <T> void visitValue(TreeView<T> tree, Consumer<T> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item's value.
-
visitValue
public static <T> void visitValue(TreeView<T> tree, BiConsumer<T,Integer> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item's value.
-
visitValue
public static <T> void visitValue(TreeTableView<T> tree, Consumer<T> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item's value.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
tree
- TheTreeTableView
whose root has to be visited.visitor
- The visitor receiving the visitedTreeItem
's value during the tree walk.
-
visitValue
public static <T> void visitValue(TreeTableView<T> tree, BiConsumer<T,Integer> visitor)
Walks over the given root of the giventree
and calls the consumer for each tree item's value.- Type Parameters:
T
- The type of theTreeItem
s.- Parameters:
tree
- TheTreeTableView
whose root has to be visited.visitor
- The visitor receiving the visitedTreeItem
's value and the current walking depth during the tree walk.
-
getDepth
public int getDepth()
- Returns:
- The current depth level.
0
means "root item" (the one used to build this walker).1
means "root's child" and so on.
-
hasNext
public boolean hasNext()
-
spliterator
public Spliterator<TreeItem<T>> spliterator()
- Specified by:
spliterator
in interfaceIterable<T>
-
-