Class TreeParentIterable<Node,​Id>

  • Type Parameters:
    Node - Type of the items in the tree.
    Id - Type of the identifier that identifiers the items in the tree.
    All Implemented Interfaces:
    Iterable<Node>

    public final class TreeParentIterable<Node,​Id>
    extends Object
    implements Iterable<Node>
    An iterable that iterates over a tree-like graph, starting at a given node, and accessing each parent. Throws an exception in case the graph is not a tree, thus also preventing endless cycles when the graph is cyclic.
    Since:
    8.2.0
    Author:
    XIMA MEDIA GmbH
    • Constructor Detail

      • TreeParentIterable

        public TreeParentIterable​(Node startItem,
                                  boolean includeStartItem,
                                  ITreeParentAccessor<Node,​Id> treeAccessor)
        Returns a new iterable over the parents in the tree, starting at the given item.
        Parameters:
        startItem - Child item where to start the traversal.
        includeStartItem - Whether to include the start item in the iteration.
        treeAccessor - Handler for accessing the parent of a node and its ID.
      • TreeParentIterable

        public TreeParentIterable​(Node startItem,
                                  ITreeParentAccessor<Node,​Id> treeAccessor)
        Returns a new iterable over the parents in the tree, starting at the given item.
        Parameters:
        startItem - Child item where to start the traversal. This item is inluded in the traversal.
        treeAccessor - Handler for accessing the parent of a node and its ID.
    • Method Detail

      • closestParentExcludingStart

        public static <Node> Node closestParentExcludingStart​(Node startItem,
                                                              ITreeParentAccessor<Node,​?> treeAccessor,
                                                              Predicate<? super Node> filter)
        Finds the closest parent of the given item that matches the given filter. Does not include the start node in the list of candidates. A null filter is treated as no filter, i.e. so this method returns the first parent.
        Type Parameters:
        Node - Type of the items in the tree.
        Parameters:
        startItem - Node where to start the traversal.
        treeAccessor - Handler for accessing the parent of a node, and its identifier.
        filter - Predicate to filter the nodes.
        Returns:
        The closest parent of the given item that matches the given filter. null if no such parent exists.
      • closestParentIncludingStart

        public static <Node> Node closestParentIncludingStart​(Node startItem,
                                                              ITreeParentAccessor<Node,​?> treeAccessor,
                                                              Predicate<? super Node> filter)
        Finds the closest parent of the given item that matches the given filter. Include the start node in the list of candidates. A null filter is treated as no filter, i.e. this method returns the start node itself.
        Type Parameters:
        Node - Type of the items in the tree.
        Parameters:
        startItem - Node where to start the traversal.
        treeAccessor - Handler for accessing the parent of a node, and its identifier.
        filter - Predicate to filter the nodes.
        Returns:
        The closest parent of the given item that matches the given filter. null if no such parent exists.
      • toList

        public static <Node> List<Node> toList​(Node startItem,
                                               boolean includeStartItem,
                                               ITreeParentAccessor<Node,​?> treeAccessor)
        Iterates over the node and its parents, and returns a list of all iterated nodes.
        Type Parameters:
        Node - Type of the items in the tree.
        Parameters:
        startItem - Node where to start the traversal. This item is included in the returned list.
        includeStartItem - Whether to include the start item in the iteration.
        treeAccessor - Handler for accessing the parent of a node, and its identifier.
        Returns:
        A list with the given start item and all its parents.
      • toSet

        public static <Node> Set<Node> toSet​(Node startItem,
                                             boolean includeStartItem,
                                             ITreeParentAccessor<Node,​?> treeAccessor)
        Iterates over the node and its parents, and returns a set of all iterated nodes.
        Type Parameters:
        Node - Type of the items in the tree.
        Parameters:
        startItem - Node where to start the traversal. This item is included in the returned list.
        includeStartItem - Whether to include the start item in the iteration.
        treeAccessor - Handler for accessing the parent of a node, and its identifier.
        Returns:
        A set with the given start item and all its parents.