Package org.parboiled.trees
Class GraphUtils
- java.lang.Object
-
- org.parboiled.trees.GraphUtils
-
public final class GraphUtils extends java.lang.Object
General utility methods for operating on directed graphs (consisting ofGraphNode
s).
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends GraphNode<T>,C extends java.util.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.static <T extends GraphNode<T>>
java.lang.StringprintTree(T node, Formatter<T> formatter)
Creates a string representation of the graph reachable from the given node using the given formatter.static <T extends GraphNode<T>>
java.lang.StringprintTree(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.
-
-
-
Method Detail
-
hasChildren
public static boolean hasChildren(GraphNode<?> node)
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
public static <T extends GraphNode<T>> T getFirstChild(T node)
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
public static <T extends GraphNode<T>> T getLastChild(T node)
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
public static <T extends GraphNode<T>> int countAllDistinct(T node)
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 java.util.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
public static <T extends GraphNode<T>> java.lang.String printTree(T node, Formatter<T> formatter)
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>> java.lang.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
-
-