Package org.parboiled.trees
Class GraphUtils
java.lang.Object
org.parboiled.trees.GraphUtils
General utility methods for operating on directed graphs (consisting of
GraphNode
s).-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends GraphNode<T>,
C extends Collection<T>>
CcollectAllNodes
(T node, C collection) Collects all nodes from the graph reachable from the given node in the given collection.static <T extends GraphNode<T>>
intcountAllDistinct
(T node) Counts all distinct nodes in the graph reachable from the given node.static <T extends GraphNode<T>>
TgetFirstChild
(T node) Returns the first child node of the given node or null if node is null or does not have any children.static <T extends GraphNode<T>>
TgetLastChild
(T node) Returns the last child node of the given node or null if node is null or does not have any children.static boolean
hasChildren
(GraphNode<?> node) Returns true if this node is not null and has at least one child node.Creates a string representation of the graph reachable from the given node using the given formatter.Creates a string representation of the graph reachable from the given node using the given formatter.
-
Method Details
-
hasChildren
Returns true if this node is not null and has at least one child node.- Parameters:
node
- a node- Returns:
- true if this node is not null and has at least one child node.
-
getFirstChild
Returns the first child node of the given node or null if node is null or does not have any children.- Parameters:
node
- a node- Returns:
- the first child node of the given node or null if node is null or does not have any children
-
getLastChild
Returns the last child node of the given node or null if node is null or does not have any children.- Parameters:
node
- a node- Returns:
- the last child node of the given node or null if node is null or does not have any children
-
countAllDistinct
Counts all distinct nodes in the graph reachable from the given node. This method can properly deal with cycles in the graph.- Parameters:
node
- the root node- Returns:
- the number of distinct nodes
-
collectAllNodes
public static <T extends GraphNode<T>,C extends Collection<T>> C collectAllNodes(T node, C collection) Collects all nodes from the graph reachable from the given node in the given collection. This method can properly deal with cycles in the graph.- Parameters:
node
- the root nodecollection
- the collection to collect into- Returns:
- the same collection passed as a parameter
-
printTree
Creates a string representation of the graph reachable from the given node using the given formatter.- Parameters:
node
- the root nodeformatter
- the node formatter- Returns:
- a new string
-
printTree
public static <T extends GraphNode<T>> String printTree(T node, Formatter<T> formatter, Predicate<T> nodeFilter, Predicate<T> subTreeFilter) Creates a string representation of the graph reachable from the given node using the given formatter. The given filter predicated determines whether a particular node (and its subtree respectively) is to be printed or not.- Parameters:
node
- the root nodeformatter
- the node formatternodeFilter
- the predicate selecting the nodes to printsubTreeFilter
- the predicate determining whether to descend into a given nodes subtree or not- Returns:
- a new string
-