Class GraphUtils


  • public final class GraphUtils
    extends java.lang.Object
    General utility methods for operating on directed graphs (consisting of GraphNodes).
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      static <T extends GraphNode<T>>
      int
      countAllDistinct​(T node)
      Counts all distinct nodes in the graph reachable from the given node.
      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.
      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.
      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.String
      printTree​(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.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 node
        collection - 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 node
        formatter - 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 node
        formatter - the node formatter
        nodeFilter - the predicate selecting the nodes to print
        subTreeFilter - the predicate determining whether to descend into a given nodes subtree or not
        Returns:
        a new string