Class AbstractNetwork<N,E>

java.lang.Object
com.google.common.graph.AbstractNetwork<N,E>
Type Parameters:
N - Node parameter type
E - Edge parameter type
All Implemented Interfaces:
Network<N,E>, PredecessorsFunction<N>, SuccessorsFunction<N>
Direct Known Subclasses:
ImmutableNetwork

@Beta public abstract class AbstractNetwork<N,E> extends Object implements Network<N,E>
This class provides a skeletal implementation of Network. It is recommended to extend this class rather than implement Network directly.

The methods implemented in this class should not be overridden unless the subclass admits a more efficient implementation.

Since:
20.0
Author:
James Sexton
  • Constructor Details

    • AbstractNetwork

      public AbstractNetwork()
      Constructor for use by subclasses.
  • Method Details

    • asGraph

      public Graph<N> asGraph()
      Description copied from interface: Network
      Returns a live view of this network as a Graph. The resulting Graph will have an edge connecting node A to node B if this Network has an edge connecting A to B.

      If this network allows parallel edges, parallel edges will be treated as if collapsed into a single edge. For example, the Network.degree(Object) of a node in the Graph view may be less than the degree of the same node in this Network.

      Specified by:
      asGraph in interface Network<N,E>
    • degree

      public int degree(N node)
      Description copied from interface: Network
      Returns the count of node's incident edges, counting self-loops twice (equivalently, the number of times an edge touches node).

      For directed networks, this is equal to inDegree(node) + outDegree(node).

      For undirected networks, this is equal to incidentEdges(node).size() + (number of self-loops incident to node).

      If the count is greater than Integer.MAX_VALUE, returns Integer.MAX_VALUE.

      Specified by:
      degree in interface Network<N,E>
    • inDegree

      public int inDegree(N node)
      Description copied from interface: Network
      Returns the count of node's incoming edges in a directed network. In an undirected network, returns the Network.degree(Object).

      If the count is greater than Integer.MAX_VALUE, returns Integer.MAX_VALUE.

      Specified by:
      inDegree in interface Network<N,E>
    • outDegree

      public int outDegree(N node)
      Description copied from interface: Network
      Returns the count of node's outgoing edges in a directed network. In an undirected network, returns the Network.degree(Object).

      If the count is greater than Integer.MAX_VALUE, returns Integer.MAX_VALUE.

      Specified by:
      outDegree in interface Network<N,E>
    • adjacentEdges

      public Set<E> adjacentEdges(E edge)
      Description copied from interface: Network
      Returns a live view of the edges which have an incident node in common with edge. An edge is not considered adjacent to itself.

      If edge is removed from the network 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 edge is re-added to the network after having been removed, view's behavior is undefined
      Specified by:
      adjacentEdges in interface Network<N,E>
    • edgesConnecting

      public Set<E> edgesConnecting(N nodeU, N nodeV)
      Description copied from interface: Network
      Returns a live view of the set of edges that each directly connect nodeU to nodeV.

      In an undirected network, this is equal to edgesConnecting(nodeV, nodeU).

      The resulting set of edges will be parallel (i.e. have equal Network.incidentNodes(Object)). If this network does not allow parallel edges, the resulting set will contain at most one edge (equivalent to edgeConnecting(nodeU, nodeV).asSet()).

      If either nodeU or nodeV are removed from the network 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 nodeU or nodeV are re-added to the network after having been removed, view's behavior is undefined
      Specified by:
      edgesConnecting in interface Network<N,E>
    • edgesConnecting

      public Set<E> edgesConnecting(EndpointPair<N> endpoints)
      Description copied from interface: Network
      Returns a live view of the set of edges that each directly connect endpoints (in the order, if any, specified by endpoints).

      The resulting set of edges will be parallel (i.e. have equal Network.incidentNodes(Object)). If this network does not allow parallel edges, the resulting set will contain at most one edge (equivalent to edgeConnecting(endpoints).asSet()).

      If this network is directed, endpoints must be ordered.

      If either element of endpoints is removed from the network 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 either endpoint is re-added to the network after having been removed, view's behavior is undefined
      Specified by:
      edgesConnecting in interface Network<N,E>
    • edgeConnecting

      public Optional<E> edgeConnecting(N nodeU, N nodeV)
      Description copied from interface: Network
      Returns the single edge that directly connects nodeU to nodeV, if one is present, or Optional.empty() if no such edge exists.

      In an undirected network, this is equal to edgeConnecting(nodeV, nodeU).

      Specified by:
      edgeConnecting in interface Network<N,E>
    • edgeConnecting

      public Optional<E> edgeConnecting(EndpointPair<N> endpoints)
      Description copied from interface: Network
      Returns the single edge that directly connects endpoints (in the order, if any, specified by endpoints), if one is present, or Optional.empty() if no such edge exists.

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

      Specified by:
      edgeConnecting in interface Network<N,E>
    • edgeConnectingOrNull

      public @Nullable E edgeConnectingOrNull(N nodeU, N nodeV)
      Description copied from interface: Network
      Returns the single edge that directly connects nodeU to nodeV, if one is present, or null if no such edge exists.

      In an undirected network, this is equal to edgeConnectingOrNull(nodeV, nodeU).

      Specified by:
      edgeConnectingOrNull in interface Network<N,E>
    • edgeConnectingOrNull

      Description copied from interface: Network
      Returns the single edge that directly connects endpoints (in the order, if any, specified by endpoints), if one is present, or null if no such edge exists.

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

      Specified by:
      edgeConnectingOrNull in interface Network<N,E>
    • hasEdgeConnecting

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

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

      Specified by:
      hasEdgeConnecting in interface Network<N,E>
    • hasEdgeConnecting

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

      Unlike the other EndpointPair-accepting methods, this method does not throw if the endpoints are unordered and the network is directed; it simply returns false. This is for consistency with Graph.hasEdgeConnecting(EndpointPair) and ValueGraph.hasEdgeConnecting(EndpointPair).

      Specified by:
      hasEdgeConnecting in interface Network<N,E>
    • validateEndpoints

      protected final void validateEndpoints(EndpointPair<?> endpoints)
      Throws an IllegalArgumentException if the ordering of endpoints is not compatible with the directionality of this graph.
    • isOrderingCompatible

      protected final boolean isOrderingCompatible(EndpointPair<?> endpoints)
    • equals

      public final boolean equals(@Nullable Object obj)
      Description copied from interface: Network
      Returns true iff object is a Network that has the same elements and the same structural relationships as those in this network.

      Thus, two networks A and B are equal if all of the following are true:

      • A and B have equal directedness.
      • A and B have equal node sets.
      • A and B have equal edge sets.
      • Every edge in A and B connects the same nodes in the same direction (if any).

      Network properties besides directedness do not affect equality. For example, two networks may be considered equal even if one allows parallel edges and the other doesn't. Additionally, the order in which nodes or edges are added to the network, and the order in which they are iterated over, are irrelevant.

      A reference implementation of this is provided by equals(Object).

      Specified by:
      equals in interface Network<N,E>
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Description copied from interface: Network
      Returns the hash code for this network. The hash code of a network is defined as the hash code of a map from each of its edges to their incident nodes.

      A reference implementation of this is provided by hashCode().

      Specified by:
      hashCode in interface Network<N,E>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns a string representation of this network.
      Overrides:
      toString in class Object
    • edgeInvalidatableSet

      protected final <T> Set<T> edgeInvalidatableSet(Set<T> set, E edge)
      Returns a Set whose methods throw IllegalStateException when the given edge is not present in this network.
      Since:
      33.1.0
    • nodeInvalidatableSet

      protected final <T> Set<T> nodeInvalidatableSet(Set<T> set, N node)
      Returns a Set whose methods throw IllegalStateException when the given node is not present in this network.
      Since:
      33.1.0
    • nodePairInvalidatableSet

      protected final <T> Set<T> nodePairInvalidatableSet(Set<T> set, N nodeU, N nodeV)
      Returns a Set whose methods throw IllegalStateException when either of the given nodes is not present in this network.
      Since:
      33.1.0