Module xaos.ui

Class TreeItemWalker<T>

  • Type Parameters:
    T - The type of the TreeItems.
    All Implemented Interfaces:
    Iterable<TreeItem<T>>, Iterator<TreeItem<T>>

    public class TreeItemWalker<T>
    extends Object
    implements Iterator<TreeItem<T>>, Iterable<TreeItem<T>>
    Walks a TreeItems 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: 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/or stream()'s methods from different threads concurrently can produce odd results. Call buildSynchronized to get a synchronized version of the walker.

    Author:
    claudio.rosati@esss.se
    See Also:
    Iterate TreeView nodes
    • Constructor Detail

      • TreeItemWalker

        protected TreeItemWalker​(TreeItem<T> root)
        Initialize the walker with the given root item.
        Parameters:
        root - The TreeItem being the root to be walked depth-first. Can be null, in which case an empty walker will be created.
      • TreeItemWalker

        protected TreeItemWalker​(TreeView<T> view)
        Initialize the walker with the TreeView.getRoot() from the given view.
        Parameters:
        view - The TreeView whose root element has to be walked depth-first. Can be null, or having a null root element, in which case an empty walker will be created.
      • TreeItemWalker

        protected TreeItemWalker​(TreeTableView<T> view)
        Initialize the walker with the TreeTableView.getRoot() from the given view.
        Parameters:
        view - The TreeTableView whose root element has to be walked depth-first. Can be null, or having a null 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 given root item.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        root - The TreeItem being the root to be walked depth-first. Can be null, in which case an empty walker will be created.
        Returns:
        A new instance of TreeItemWalker initialized with the given root parameter.
      • build

        public static <T> TreeItemWalker<T> build​(TreeView<T> view)
        Returns a walker initialized with the TreeView.getRoot() from the given view.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        view - The TreeView whose root element has to be walked depth-first. Can be null, or having a null root element, in which case an empty walker will be created.
        Returns:
        A new instance of TreeItemWalker initialized with the given view parameter.
      • build

        public static <T> TreeItemWalker<T> build​(TreeTableView<T> view)
        Returns a walker initialized with the TreeTableView.getRoot() from the given view.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        view - The TreeTableView whose root element has to be walked depth-first. Can be null, or having a null root element, in which case an empty walker will be created.
        Returns:
        A new instance of TreeItemWalker initialized with the given view parameter.
      • buildSynchronized

        public static <T> TreeItemWalker<T> buildSynchronized​(TreeItem<T> root)
        Returns a synchronized walker initialized with the given root item.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        root - The TreeItem being the root to be walked depth-first. Can be null, in which case an empty walker will be created.
        Returns:
        A new instance of TreeItemWalker initialized with the given root parameter.
      • buildSynchronized

        public static <T> TreeItemWalker<T> buildSynchronized​(TreeView<T> view)
        Returns a synchronized walker initialized with the TreeView.getRoot() from the given view.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        view - The TreeView whose root element has to be walked depth-first. Can be null, or having a null root element, in which case an empty walker will be created.
        Returns:
        A new instance of TreeItemWalker initialized with the given view parameter.
      • buildSynchronized

        public static <T> TreeItemWalker<T> buildSynchronized​(TreeTableView<T> view)
        Returns a synchronized walker initialized with the TreeTableView.getRoot() from the given view.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        view - The TreeTableView whose root element has to be walked depth-first. Can be null, or having a null root element, in which case an empty walker will be created.
        Returns:
        A new instance of TreeItemWalker initialized with the given view parameter.
      • visit

        public static <T> void visit​(TreeItem<T> root,
                                     Consumer<TreeItem<T>> visitor)
        Walks over the given tree root and calls the consumer for each tree item.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        root - The root TreeItem to visit.
        visitor - The visitor receiving the visited TreeItem during the tree walk.
      • visit

        public static <T> void visit​(TreeItem<T> root,
                                     BiConsumer<TreeItem<T>,​Integer> visitor)
        Walks over the given tree root and calls the consumer for each tree item.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        root - The root TreeItem to visit.
        visitor - The visitor receiving the visited TreeItem and the current walking depth during the tree walk.
      • visit

        public static <T> void visit​(TreeView<T> tree,
                                     Consumer<TreeItem<T>> visitor)
        Walks over the given root of the given tree and calls the consumer for each tree item.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        tree - The TreeView whose root has to be visited.
        visitor - The visitor receiving the visited TreeItem during the tree walk.
      • visit

        public static <T> void visit​(TreeView<T> tree,
                                     BiConsumer<TreeItem<T>,​Integer> visitor)
        Walks over the given root of the given tree and calls the consumer for each tree item.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        tree - The TreeView whose root has to be visited.
        visitor - The visitor receiving the visited TreeItem and the current walking depth during the tree walk.
      • visit

        public static <T> void visit​(TreeTableView<T> tree,
                                     Consumer<TreeItem<T>> visitor)
        Walks over the given root of the given tree and calls the consumer for each tree item.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        tree - The TreeTableView whose root has to be visited.
        visitor - The visitor receiving the visited TreeItem 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 given tree and calls the consumer for each tree item.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        tree - The TreeTableView whose root has to be visited.
        visitor - The visitor receiving the visited TreeItem 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 tree root and calls the consumer for each tree item's value.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        root - The root TreeItem to visit.
        visitor - The visitor receiving the visited TreeItem's value during the tree walk.
      • visitValue

        public static <T> void visitValue​(TreeItem<T> root,
                                          BiConsumer<T,​Integer> visitor)
        Walks over the given tree root and calls the consumer for each tree item's value.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        root - The root TreeItem to visit.
        visitor - The visitor receiving the visited TreeItem's value and the current walking depth during the tree walk.
      • visitValue

        public static <T> void visitValue​(TreeView<T> tree,
                                          Consumer<T> visitor)
        Walks over the given root of the given tree and calls the consumer for each tree item's value.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        tree - The TreeView whose root has to be visited.
        visitor - The visitor receiving the visited TreeItem's value during the tree walk.
      • visitValue

        public static <T> void visitValue​(TreeView<T> tree,
                                          BiConsumer<T,​Integer> visitor)
        Walks over the given root of the given tree and calls the consumer for each tree item's value.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        tree - The TreeView whose root has to be visited.
        visitor - The visitor receiving the visited TreeItem's value and the current walking depth during the tree walk.
      • visitValue

        public static <T> void visitValue​(TreeTableView<T> tree,
                                          Consumer<T> visitor)
        Walks over the given root of the given tree and calls the consumer for each tree item's value.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        tree - The TreeTableView whose root has to be visited.
        visitor - The visitor receiving the visited TreeItem'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 given tree and calls the consumer for each tree item's value.
        Type Parameters:
        T - The type of the TreeItems.
        Parameters:
        tree - The TreeTableView whose root has to be visited.
        visitor - The visitor receiving the visited TreeItem'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()
        Specified by:
        hasNext in interface Iterator<T>
        Returns:
        true if the walker still has unserved items.
      • next

        public TreeItem<T> next()
        Specified by:
        next in interface Iterator<T>
        Returns:
        The next tree item in depth-first walk order: the parent is returned before any of its children.
      • stream

        public Stream<TreeItem<T>> stream()
        Returns:
        A Stream of all (remaining) items. Note that the walker can traverse only once over items. So if hasNext()/next() where used to partially walk the tree, a following call to stream() will return the remaining items.