- java.lang.Object
-
- eu.ess.xaos.ui.control.tree.TreeItemWalker<T>
-
- Type Parameters:
T- The type of theTreeItems.
public class TreeItemWalker<T> extends Object implements Iterator<TreeItem<T>>, Iterable<TreeItem<T>>
Walks aTreeItems tree depth-first. It supports streaming, visitor pattern and iterator pattern.Important Note #1: Once used, a
TreeItemWalkercannot be reused. A new instance has to be created instead.Important Note #2:
TreeItems 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. CallbuildSynchronizedto get a synchronized version of the walker.- Author:
- claudio.rosati@esss.se
- See Also:
- Iterate TreeView nodes
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedTreeItemWalker(TreeItem<T> root)Initialize the walker with the givenrootitem.protectedTreeItemWalker(TreeTableView<T> view)Initialize the walker with theTreeTableView.getRoot()from the givenview.protectedTreeItemWalker(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 givenrootitem.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 givenrootitem.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()intgetDepth()booleanhasNext()Iterator<TreeItem<T>>iterator()TreeItem<T>next()Spliterator<TreeItem<T>>spliterator()Stream<TreeItem<T>>stream()static <T> voidvisit(TreeItem<T> root, BiConsumer<TreeItem<T>,Integer> visitor)Walks over the given treerootand calls the consumer for each tree item.static <T> voidvisit(TreeItem<T> root, Consumer<TreeItem<T>> visitor)Walks over the given treerootand calls the consumer for each tree item.static <T> voidvisit(TreeTableView<T> tree, BiConsumer<TreeItem<T>,Integer> visitor)Walks over the given root of the giventreeand calls the consumer for each tree item.static <T> voidvisit(TreeTableView<T> tree, Consumer<TreeItem<T>> visitor)Walks over the given root of the giventreeand calls the consumer for each tree item.static <T> voidvisit(TreeView<T> tree, BiConsumer<TreeItem<T>,Integer> visitor)Walks over the given root of the giventreeand calls the consumer for each tree item.static <T> voidvisit(TreeView<T> tree, Consumer<TreeItem<T>> visitor)Walks over the given root of the giventreeand calls the consumer for each tree item.static <T> voidvisitValue(TreeItem<T> root, BiConsumer<T,Integer> visitor)Walks over the given treerootand calls the consumer for each tree item's value.static <T> voidvisitValue(TreeItem<T> root, Consumer<T> visitor)Walks over the given treerootand calls the consumer for each tree item's value.static <T> voidvisitValue(TreeTableView<T> tree, BiConsumer<T,Integer> visitor)Walks over the given root of the giventreeand calls the consumer for each tree item's value.static <T> voidvisitValue(TreeTableView<T> tree, Consumer<T> visitor)Walks over the given root of the giventreeand calls the consumer for each tree item's value.static <T> voidvisitValue(TreeView<T> tree, BiConsumer<T,Integer> visitor)Walks over the given root of the giventreeand calls the consumer for each tree item's value.static <T> voidvisitValue(TreeView<T> tree, Consumer<T> visitor)Walks over the given root of the giventreeand 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 givenrootitem.- Parameters:
root- TheTreeItembeing 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- TheTreeViewwhose root element has to be walked depth-first. Can benull, or having anullroot 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- TheTreeTableViewwhose root element has to be walked depth-first. Can benull, or having anullroot 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 givenrootitem.- Type Parameters:
T- The type of theTreeItems.- Parameters:
root- TheTreeItembeing the root to be walked depth-first. Can benull, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalkerinitialized with the givenrootparameter.
-
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 theTreeItems.- Parameters:
view- TheTreeViewwhose root element has to be walked depth-first. Can benull, or having anullroot element, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalkerinitialized with the givenviewparameter.
-
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 theTreeItems.- Parameters:
view- TheTreeTableViewwhose root element has to be walked depth-first. Can benull, or having anullroot element, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalkerinitialized with the givenviewparameter.
-
buildSynchronized
public static <T> TreeItemWalker<T> buildSynchronized(TreeItem<T> root)
Returns a synchronized walker initialized with the givenrootitem.- Type Parameters:
T- The type of theTreeItems.- Parameters:
root- TheTreeItembeing the root to be walked depth-first. Can benull, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalkerinitialized with the givenrootparameter.
-
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 theTreeItems.- Parameters:
view- TheTreeViewwhose root element has to be walked depth-first. Can benull, or having anullroot element, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalkerinitialized with the givenviewparameter.
-
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 theTreeItems.- Parameters:
view- TheTreeTableViewwhose root element has to be walked depth-first. Can benull, or having anullroot element, in which case an empty walker will be created.- Returns:
- A new instance of
TreeItemWalkerinitialized with the givenviewparameter.
-
visit
public static <T> void visit(TreeItem<T> root, Consumer<TreeItem<T>> visitor)
Walks over the given treerootand 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 treerootand 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 giventreeand 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 giventreeand 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 giventreeand calls the consumer for each tree item.- Type Parameters:
T- The type of theTreeItems.- Parameters:
tree- TheTreeTableViewwhose root has to be visited.visitor- The visitor receiving the visitedTreeItemduring the tree walk.
-
visit
public static <T> void visit(TreeTableView<T> tree, BiConsumer<TreeItem<T>,Integer> visitor)
Walks over the given root of the giventreeand calls the consumer for each tree item.- Type Parameters:
T- The type of theTreeItems.- Parameters:
tree- TheTreeTableViewwhose root has to be visited.visitor- The visitor receiving the visitedTreeItemand the current walking depth during the tree walk.
-
visitValue
public static <T> void visitValue(TreeItem<T> root, Consumer<T> visitor)
Walks over the given treerootand 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 treerootand 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 giventreeand 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 giventreeand 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 giventreeand calls the consumer for each tree item's value.- Type Parameters:
T- The type of theTreeItems.- Parameters:
tree- TheTreeTableViewwhose 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 giventreeand calls the consumer for each tree item's value.- Type Parameters:
T- The type of theTreeItems.- Parameters:
tree- TheTreeTableViewwhose 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.
0means "root item" (the one used to build this walker).1means "root's child" and so on.
-
hasNext
public boolean hasNext()
-
spliterator
public Spliterator<TreeItem<T>> spliterator()
- Specified by:
spliteratorin interfaceIterable<T>
-
-