Class StandardValueGraph<N,V>

Type Parameters:
N - Node parameter type
V - Value parameter type
All Implemented Interfaces:
BaseGraph<N>, PredecessorsFunction<N>, SuccessorsFunction<N>, ValueGraph<N,V>
Direct Known Subclasses:
ImmutableValueGraph, StandardMutableValueGraph

class StandardValueGraph<N,V> extends AbstractValueGraph<N,V>
Standard implementation of ValueGraph that supports the options supplied by AbstractGraphBuilder.

This class maintains a map of nodes to GraphConnections.

Collection-returning accessors return unmodifiable views: the view returned will reflect changes to the graph (if the graph is mutable) but may not be modified by the user.

The time complexity of all collection-returning accessors is O(1), since views are returned.

  • Field Details

    • isDirected

      private final boolean isDirected
    • allowsSelfLoops

      private final boolean allowsSelfLoops
    • nodeOrder

      private final ElementOrder<N> nodeOrder
    • nodeConnections

      final MapIteratorCache<N,GraphConnections<N,V>> nodeConnections
    • edgeCount

      long edgeCount
  • Constructor Details

    • StandardValueGraph

      StandardValueGraph(AbstractGraphBuilder<? super N> builder)
      Constructs a graph with the properties specified in builder.
    • StandardValueGraph

      StandardValueGraph(AbstractGraphBuilder<? super N> builder, Map<N,GraphConnections<N,V>> nodeConnections, long edgeCount)
      Constructs a graph with the properties specified in builder, initialized with the given node map.
  • Method Details

    • nodes

      public Set<N> nodes()
      Description copied from interface: ValueGraph
      Returns all nodes in this graph, in the order specified by ValueGraph.nodeOrder().
    • isDirected

      public boolean isDirected()
      Description copied from interface: ValueGraph
      Returns true if the edges in this graph are directed. Directed edges connect a source node to a target node, while undirected edges connect a pair of nodes to each other.
    • allowsSelfLoops

      public boolean allowsSelfLoops()
      Description copied from interface: ValueGraph
      Returns true if this graph allows self-loops (edges that connect a node to itself). Attempting to add a self-loop to a graph that does not allow them will throw an IllegalArgumentException.
    • nodeOrder

      public ElementOrder<N> nodeOrder()
      Description copied from interface: ValueGraph
      Returns the order of iteration for the elements of ValueGraph.nodes().
    • adjacentNodes

      public Set<N> adjacentNodes(N node)
      Description copied from interface: ValueGraph
      Returns a live view of the nodes which have an incident edge in common with node in this graph.

      This is equal to the union of ValueGraph.predecessors(Object) and ValueGraph.successors(Object).

      If node is removed from the graph after this method is called, the Set view returned by this method will be invalidated, and will throw IllegalStateException if it is accessed in any way, with the following exceptions:

      • view.equals(view) evaluates to true (but any other `equals()` expression involving view will throw)
      • hashCode() does not throw
      • if node is re-added to the graph after having been removed, view's behavior is undefined
    • predecessors

      public Set<N> predecessors(N node)
      Description copied from interface: ValueGraph
      Returns a live view of all nodes in this graph adjacent to node which can be reached by traversing node's incoming edges against the direction (if any) of the edge.

      In an undirected graph, this is equivalent to ValueGraph.adjacentNodes(Object).

      If node is removed from the graph after this method is called, the `Set` returned by this method will be invalidated, and will throw `IllegalStateException` if it is accessed in any way.

    • successors

      public Set<N> successors(N node)
      Description copied from interface: ValueGraph
      Returns a live view of all nodes in this graph adjacent to node which can be reached by traversing node's outgoing edges in the direction (if any) of the edge.

      In an undirected graph, this is equivalent to ValueGraph.adjacentNodes(Object).

      This is not the same as "all nodes reachable from node by following outgoing edges". For that functionality, see Graphs.reachableNodes(Graph, Object).

      If node is removed from the graph after this method is called, the Set view returned by this method will be invalidated, and will throw IllegalStateException if it is accessed in any way, with the following exceptions:

      • view.equals(view) evaluates to true (but any other `equals()` expression involving view will throw)
      • hashCode() does not throw
      • if node is re-added to the graph after having been removed, view's behavior is undefined
    • incidentEdges

      public Set<EndpointPair<N>> incidentEdges(N node)
      Description copied from interface: BaseGraph
      Returns a live view of the edges in this graph whose endpoints include node.

      This is equal to the union of incoming and outgoing edges.

      If node is removed from the graph after this method is called, the Set view returned by this method will be invalidated, and will throw IllegalStateException if it is accessed in any way, with the following exceptions:

      • view.equals(view) evaluates to true (but any other `equals()` expression involving view will throw)
      • hashCode() does not throw
      • if node is re-added to the graph after having been removed, view's behavior is undefined
      Specified by:
      incidentEdges in interface BaseGraph<N>
      Specified by:
      incidentEdges in interface ValueGraph<N,V>
      Overrides:
      incidentEdges in class AbstractBaseGraph<N>
    • hasEdgeConnecting

      public boolean hasEdgeConnecting(N nodeU, N nodeV)
      Description copied from interface: BaseGraph
      Returns true if there is an edge that directly connects nodeU to nodeV. This is equivalent to nodes().contains(nodeU) && successors(nodeU).contains(nodeV).

      In an undirected graph, this is equal to hasEdgeConnecting(nodeV, nodeU).

      Specified by:
      hasEdgeConnecting in interface BaseGraph<N>
      Specified by:
      hasEdgeConnecting in interface ValueGraph<N,V>
      Overrides:
      hasEdgeConnecting in class AbstractBaseGraph<N>
    • hasEdgeConnecting

      public boolean hasEdgeConnecting(EndpointPair<N> endpoints)
      Description copied from interface: BaseGraph
      Returns true if there is an edge that directly connects endpoints (in the order, if any, specified by endpoints). This is equivalent to edges().contains(endpoints).

      Unlike the other EndpointPair-accepting methods, this method does not throw if the endpoints are unordered; it simply returns false. This is for consistency with the behavior of

      invalid reference
      Collection#contains(Object)
      (which does not generally throw if the object cannot be present in the collection), and the desire to have this method's behavior be compatible with edges().contains(endpoints).
      Specified by:
      hasEdgeConnecting in interface BaseGraph<N>
      Specified by:
      hasEdgeConnecting in interface ValueGraph<N,V>
      Overrides:
      hasEdgeConnecting in class AbstractBaseGraph<N>
    • edgeValueOrDefault

      @CheckForNull public V edgeValueOrDefault(N nodeU, N nodeV, @CheckForNull V defaultValue)
      Description copied from interface: ValueGraph
      Returns the value of the edge that connects nodeU to nodeV, if one is present; otherwise, returns defaultValue.

      In an undirected graph, this is equal to edgeValueOrDefault(nodeV, nodeU, defaultValue).

    • edgeValueOrDefault

      @CheckForNull public V edgeValueOrDefault(EndpointPair<N> endpoints, @CheckForNull V defaultValue)
      Description copied from interface: ValueGraph
      Returns the value of the edge that connects endpoints (in the order, if any, specified by endpoints), if one is present; otherwise, returns defaultValue.

      If this graph is directed, the endpoints must be ordered.

    • edgeCount

      protected long edgeCount()
      Description copied from class: AbstractBaseGraph
      Returns the number of edges in this graph; used to calculate the size of Graph.edges(). This implementation requires O(|N|) time. Classes extending this one may manually keep track of the number of edges as the graph is updated, and override this method for better performance.
      Overrides:
      edgeCount in class AbstractBaseGraph<N>
    • checkedConnections

      private final GraphConnections<N,V> checkedConnections(N node)
    • containsNode

      final boolean containsNode(@CheckForNull N node)
    • hasEdgeConnectingInternal

      private final boolean hasEdgeConnectingInternal(N nodeU, N nodeV)
    • edgeValueOrDefaultInternal

      @CheckForNull private final V edgeValueOrDefaultInternal(N nodeU, N nodeV, @CheckForNull V defaultValue)