Class CheckerGraph<N,E extends Number>

java.lang.Object
util.AbstractChecker<Graph<N,E>,CheckerGraph<N,E>>
specialized_checkers.collection.CheckerGraph<N,E>
Type Parameters:
N - the type of nodes in the graph
E - the type of edge weights (must extend Number)
All Implemented Interfaces:
InterfaceChecker<AbstractChecker<Graph<N,E>,CheckerGraph<N,E>>,Graph<N,E>>

public class CheckerGraph<N,E extends Number> extends AbstractChecker<Graph<N,E>,CheckerGraph<N,E>>
A specialized checker for validating properties and constraints on graph data structures.

The CheckerGraph class provides a fluent API for asserting various conditions on graphs, such as connectivity, acyclicity, node and edge existence, and more. It supports both directed and undirected graphs, as well as weighted edges.

Example usage:


 Graph<String, Integer> graph = ...;
 CheckerGraph.check(graph)
     .isConnected()
     .minNodes(3)
     .hasCycle();
 
See Also:
  • Constructor Details

    • CheckerGraph

      protected CheckerGraph(Graph<N,E> graph, String name)
      Constructs a new CheckerGraph with the specified underlying graph and name.
      Parameters:
      graph - the underlying graph structure to be used by this CheckerGraph
      name - the name assigned to this CheckerGraph
  • Method Details

    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Graph<N,E> graph, String name)
      Creates a CheckerGraph for the given graph and assigns a custom name.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      graph - the graph to check
      name - the name to assign to this checker
      Returns:
      a CheckerGraph instance for the given graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Collection<Graph.Edge<N,E>> edges, boolean directed, String name)
      Creates a CheckerGraph from a collection of edges, specifying if the graph is directed, and assigns a name.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      edges - the collection of edges to build the graph
      directed - true if the graph is directed, false otherwise
      name - the name to assign to this checker
      Returns:
      a CheckerGraph instance for the constructed graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Collection<Graph.Edge<N,E>> edges, String name)
      Creates a CheckerGraph from a collection of edges and assigns a name. The graph will be undirected by default.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      edges - the collection of edges to build the graph
      name - the name to assign to this checker
      Returns:
      a CheckerGraph instance for the constructed graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Collection<N> nodes, Collection<Graph.Edge<N,E>> edges, String name)
      Creates a CheckerGraph from a collection of nodes and edges, and assigns a name.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      nodes - the collection of nodes in the graph
      edges - the collection of edges in the graph
      name - the name to assign to this checker
      Returns:
      a CheckerGraph instance for the constructed graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Collection<N> nodes, Collection<Graph.Edge<N,E>> edges, boolean directed, String name)
      Creates a CheckerGraph from a collection of nodes and edges, specifying if the graph is directed, and assigns a name.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      nodes - the collection of nodes in the graph
      edges - the collection of edges in the graph
      directed - true if the graph is directed, false otherwise
      name - the name to assign to this checker
      Returns:
      a CheckerGraph instance for the constructed graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Graph<N,E> graph)
      Creates a CheckerGraph for the given graph with a default name.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      graph - the graph to check
      Returns:
      a CheckerGraph instance for the given graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Collection<Graph.Edge<N,E>> edges, boolean directed)
      Creates a CheckerGraph from a collection of edges, specifying if the graph is directed, with a default name.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      edges - the collection of edges to build the graph
      directed - true if the graph is directed, false otherwise
      Returns:
      a CheckerGraph instance for the constructed graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Collection<Graph.Edge<N,E>> edges)
      Creates a CheckerGraph from a collection of edges with a default name. The graph will be undirected by default.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      edges - the collection of edges to build the graph
      Returns:
      a CheckerGraph instance for the constructed graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Collection<N> nodes, Collection<Graph.Edge<N,E>> edges)
      Creates a CheckerGraph from a collection of nodes and edges with a default name.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      nodes - the collection of nodes in the graph
      edges - the collection of edges in the graph
      Returns:
      a CheckerGraph instance for the constructed graph
    • check

      public static <N, E extends Number> CheckerGraph<N,E> check(Collection<N> nodes, Collection<Graph.Edge<N,E>> edges, boolean directed)
      Creates a CheckerGraph from a collection of nodes and edges, specifying if the graph is directed, with a default name.
      Type Parameters:
      N - the type of nodes in the graph
      E - the type of edge weights (must extend Number)
      Parameters:
      nodes - the collection of nodes in the graph
      edges - the collection of edges in the graph
      directed - true if the graph is directed, false otherwise
      Returns:
      a CheckerGraph instance for the constructed graph
    • self

      protected CheckerGraph<N,E> self()
      Returns this instance (for fluent API).
      Specified by:
      self in class AbstractChecker<Graph<N,E extends Number>,CheckerGraph<N,E extends Number>>
      Returns:
      this CheckerGraph instance
    • isEmpty

      public CheckerGraph<N,E> isEmpty()
      Checks if the graph is empty (contains no nodes).
      Returns:
      this CheckerGraph instance
    • isTree

      public CheckerGraph<N,E> isTree()
      Checks if the graph is a tree (connected and acyclic).
      Returns:
      this CheckerGraph instance
    • isBinaryTree

      public CheckerGraph<N,E> isBinaryTree()
      Checks if the graph is a binary tree (each node has at most two children).
      Returns:
      this CheckerGraph instance
    • isDirected

      public CheckerGraph<N,E> isDirected()
      Checks if the graph is directed.
      Returns:
      this CheckerGraph instance
    • isConnected

      public CheckerGraph<N,E> isConnected()
      Checks if the graph is connected (there is a path between every pair of nodes).
      Returns:
      this CheckerGraph instance
    • hasCycle

      public CheckerGraph<N,E> hasCycle()
      Checks if the graph contains at least one cycle.
      Returns:
      this CheckerGraph instance
    • containsNode

      public CheckerGraph<N,E> containsNode(N node)
      Checks if the graph contains the specified node.
      Parameters:
      node - the node to check for
      Returns:
      this CheckerGraph instance
    • containsEdge

      public CheckerGraph<N,E> containsEdge(N from, N to)
      Checks if the graph contains an edge from one node to another.
      Parameters:
      from - the source node
      to - the destination node
      Returns:
      this CheckerGraph instance
    • containsEdge

      public CheckerGraph<N,E> containsEdge(Graph.Edge<N,E> edge)
      Checks if the graph contains the specified edge.
      Parameters:
      edge - the edge to check for
      Returns:
      this CheckerGraph instance
    • anyNodesMatch

      public CheckerGraph<N,E> anyNodesMatch(Predicate<N> condition)
      Checks if any node in the graph matches the given condition.
      Parameters:
      condition - the predicate to test nodes
      Returns:
      this CheckerGraph instance
    • allNodesMatch

      public CheckerGraph<N,E> allNodesMatch(Predicate<N> condition)
      Checks if all nodes in the graph match the given condition.
      Parameters:
      condition - the predicate to test nodes
      Returns:
      this CheckerGraph instance
    • anyEdgesMatch

      public CheckerGraph<N,E> anyEdgesMatch(Predicate<Graph.Edge<N,E>> condition)
      Checks if any edge in the graph matches the given condition.
      Parameters:
      condition - the predicate to test edges
      Returns:
      this CheckerGraph instance
    • allEdgesMatch

      public CheckerGraph<N,E> allEdgesMatch(Predicate<Graph.Edge<N,E>> condition)
      Checks if all edges in the graph match the given condition.
      Parameters:
      condition - the predicate to test edges
      Returns:
      this CheckerGraph instance
    • hasPath

      public CheckerGraph<N,E> hasPath(N start, N end)
      Checks if there is a path between two nodes in the graph.
      Parameters:
      start - the starting node
      end - the ending node
      Returns:
      this CheckerGraph instance
    • minNodes

      public CheckerGraph<N,E> minNodes(int min)
      Checks if the graph has at least the specified minimum number of nodes.
      Parameters:
      min - the minimum number of nodes
      Returns:
      this CheckerGraph instance
    • maxNodes

      public CheckerGraph<N,E> maxNodes(int max)
      Checks if the graph has at most the specified maximum number of nodes.
      Parameters:
      max - the maximum number of nodes
      Returns:
      this CheckerGraph instance
    • inRangeNodes

      public CheckerGraph<N,E> inRangeNodes(int min, int max)
      Checks if the number of nodes in the graph is within the specified range (inclusive).
      Parameters:
      min - the minimum number of nodes
      max - the maximum number of nodes
      Returns:
      this CheckerGraph instance
    • minEdges

      public CheckerGraph<N,E> minEdges(int min)
      Checks if the graph has at least the specified minimum number of edges.
      Parameters:
      min - the minimum number of edges
      Returns:
      this CheckerGraph instance
    • maxEdges

      public CheckerGraph<N,E> maxEdges(int max)
      Checks if the graph has at most the specified maximum number of edges.
      Parameters:
      max - the maximum number of edges
      Returns:
      this CheckerGraph instance
    • inRangeEdges

      public CheckerGraph<N,E> inRangeEdges(int min, int max)
      Checks if the number of edges in the graph is within the specified range (inclusive).
      Parameters:
      min - the minimum number of edges
      max - the maximum number of edges
      Returns:
      this CheckerGraph instance
    • minWeight

      public CheckerGraph<N,E> minWeight(double min)
      Checks if all edge weights in the graph are at least the specified minimum value.
      Parameters:
      min - the minimum edge weight
      Returns:
      this CheckerGraph instance
    • maxWeight

      public CheckerGraph<N,E> maxWeight(double max)
      Checks if all edge weights in the graph are at most the specified maximum value.
      Parameters:
      max - the maximum edge weight
      Returns:
      this CheckerGraph instance
    • inRangeWeight

      public CheckerGraph<N,E> inRangeWeight(double min, double max)
      Checks if all edge weights in the graph are within the specified range (inclusive).
      Parameters:
      min - the minimum edge weight
      max - the maximum edge weight
      Returns:
      this CheckerGraph instance