Package org.tensorics.core.tree.walking
Class Trees
- java.lang.Object
-
- org.tensorics.core.tree.walking.Trees
-
public final class Trees extends java.lang.Object
Provides utility methods to walk through a tree ofNode
s.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Collection<Node>
findBottomNodes(Node startingNode)
Searches for all bottom nodes of the tree.static <T extends Node>
java.util.Set<T>findClosestAncestorNodeFromNodesToRootOfType(Node childNode, Node rootNode, com.google.common.reflect.TypeToken<T> nodeToken)
Searches ancestor nodes which match the givenTypeToken
(taking into account all generic parameters!), starting from the given child not while using the tree as defined by the given rootNode.static <T extends Node>
java.util.Set<? extends T>findHighestNodeOfClass(Node rootNode, java.lang.Class<T> nodeClassToFind)
walks through the tree, starting from the given rootNode and collects the highest node which implement the given class.static <T> java.util.Set<T>
findNodesOfClass(Node rootNode, java.lang.Class<T> nodeClassToFind)
walks through the tree, starting from the given rootNode and collects all the nodes which implement the given class.static java.util.List<Path>
getPathsFromChildToAncestor(Node childNode, Node ancestorNode)
Searches in the tree for paths between the two given nodes.static <T extends Node>
TrebuildTree(T rootNode, RebuildingContext rebuildingContext)
Rebuilds the tree by taking elements from given context.static java.util.List<Node>
subTreeContent(Node rootNode)
Retrieves the content of the given subtree.static void
walkParentAfterChildren(Node startingNode, NodeCallback callback)
Traverses the tree, starting from the given node in a way, such that all children are visited before the parent node is visited.
-
-
-
Method Detail
-
walkParentAfterChildren
public static void walkParentAfterChildren(Node startingNode, NodeCallback callback)
Traverses the tree, starting from the given node in a way, such that all children are visited before the parent node is visited. It allows to provide a callback which will be called at different points of the traversal, depending on the implemented interfaces of the callback.- Parameters:
startingNode
- the node which should be considered as the root node for the walk-throughcallback
- the callback which will be called at certain points of the traversal, depending on the interfaces which are implemented by the callback.- See Also:
TreeWalker
,NodeCallback
-
subTreeContent
public static java.util.List<Node> subTreeContent(Node rootNode)
Retrieves the content of the given subtree. All children will be returned in the list, before the parent is returned.- Parameters:
rootNode
- The node which shall be taken as root of the tree.- Returns:
- a list containing all nodes of the tree, where all the children are put before their common parent node.
-
getPathsFromChildToAncestor
public static java.util.List<Path> getPathsFromChildToAncestor(Node childNode, Node ancestorNode)
Searches in the tree for paths between the two given nodes. The direction of the path will be from the childNode to the ancestor. The childNode will be the first element of each path and the given ancestor node the last element.- Parameters:
childNode
- the node from which the paths to be found shall startancestorNode
- the ancestor node, which will be the end node of each path- Returns:
- all the paths from childNode to ancestorNode
-
findBottomNodes
public static java.util.Collection<Node> findBottomNodes(Node startingNode)
Searches for all bottom nodes of the tree. Bottom nodes are those, which are furthest away from the startingNode on a given branch. (In other words, those with no more children)- Parameters:
startingNode
- the node which shall be used as root of the tree.- Returns:
- a list of all the nodes which are at the bottom of the tree (i.e. which have no children).
-
rebuildTree
public static <T extends Node> T rebuildTree(T rootNode, RebuildingContext rebuildingContext)
Rebuilds the tree by taking elements from given context.- Parameters:
rootNode
- the root node of the treerebuildingContext
- the context containing the elements needed for rebuilding of the tree- Returns:
- a new root node of rebuilt tree
-
findNodesOfClass
public static <T> java.util.Set<T> findNodesOfClass(Node rootNode, java.lang.Class<T> nodeClassToFind)
walks through the tree, starting from the given rootNode and collects all the nodes which implement the given class. The given class is not restricted to s subclass of a node, because it could potentially be a marker interface. However, the returned set will implement both, Node and the queried type.- Parameters:
rootNode
- the node from which to start the searchnodeClassToFind
- the class of the nodes to find- Returns:
- a set of all found nodes, which implement the given class
-
findHighestNodeOfClass
public static <T extends Node> java.util.Set<? extends T> findHighestNodeOfClass(Node rootNode, java.lang.Class<T> nodeClassToFind)
walks through the tree, starting from the given rootNode and collects the highest node which implement the given class.- Parameters:
rootNode
- the node from which to start the searchnodeClassToFind
- the class of the nodes to find- Returns:
- a set of one found Node, which implement the given class
-
findClosestAncestorNodeFromNodesToRootOfType
public static <T extends Node> java.util.Set<T> findClosestAncestorNodeFromNodesToRootOfType(Node childNode, Node rootNode, com.google.common.reflect.TypeToken<T> nodeToken) throws NoMatchingNodeFoundException
Searches ancestor nodes which match the givenTypeToken
(taking into account all generic parameters!), starting from the given child not while using the tree as defined by the given rootNode. This is eg. used for finding nodes, that can handle Exceptions. If the child node id not part of the tree then an empty set is returned. If there exist paths between the two nodes, then it is enforced, that in this paths a matching node is found. If this is not the case, an exception will be thrown. If the same node is found in different paths, then the corresponding node is only contained once in the set.- Parameters:
childNode
- the node for which the closest ancestors shall be found.rootNode
- the node which defines the root of the tree.nodeToken
- the token, against which the nodes are matched.- Returns:
- a set of
Node
containing for each path the closestNode
that match the givenTypeToken
- Throws:
NoMatchingNodeFoundException
- if there exists a path, that does not contain a matching node.
-
-