Skip to content

Commit

Permalink
Update Network.asGraph()'s edge value methods to be consistent with h…
Browse files Browse the repository at this point in the history
…ow they're defined in the interfaces. Update tests to fail with the old implementation.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=130683572
  • Loading branch information
Bezier89 authored and cpovirk committed Aug 22, 2016
1 parent ed63eb9 commit d0b8e4a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
Expand Up @@ -165,6 +165,14 @@ static <N> void validateGraph(Graph<N, ?> graph) {
assertThat(graph.outDegree(node)).isEqualTo(graph.degree(node));
}

for (N otherNode : graph.nodes()) {
if (graph.successors(node).contains(otherNode)) {
assertThat(graph.edgeValue(node, otherNode)).isNotNull();
} else {
assertThat(graph.edgeValueOrDefault(node, otherNode, null)).isNull();
}
}

for (N adjacentNode : graph.adjacentNodes(node)) {
if (!graph.allowsSelfLoops()) {
assertThat(node).isNotEqualTo(adjacentNode);
Expand Down
Expand Up @@ -231,7 +231,6 @@ static <N, E> void validateNetwork(Network<N, E> network) {
Set<E> edgesConnecting = network.edgesConnecting(node, otherNode);
boolean isSelfLoop = node.equals(otherNode);
if (network.isDirected() || !isSelfLoop) {
assertThat(edgesConnecting).isEqualTo(asGraph.edgeValue(node, otherNode));
assertThat(edgesConnecting).isEqualTo(
Sets.intersection(network.outEdges(node), network.inEdges(otherNode)));
}
Expand Down Expand Up @@ -279,8 +278,10 @@ static <N, E> void validateNetwork(Network<N, E> network) {
}

for (N successor : network.successors(node)) {
Set<E> edgesConnecting = network.edgesConnecting(node, successor);
assertThat(network.predecessors(successor)).contains(node);
assertThat(network.edgesConnecting(node, successor)).isNotEmpty();
assertThat(edgesConnecting).isNotEmpty();
assertThat(edgesConnecting).isEqualTo(asGraph.edgeValue(node, successor));
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions guava/src/com/google/common/graph/AbstractNetwork.java
Expand Up @@ -16,7 +16,8 @@

package com.google.common.graph;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.graph.GraphConstants.EDGE_CONNECTING_NOT_IN_GRAPH;
import static com.google.common.graph.GraphConstants.GRAPH_STRING_FORMAT;

import com.google.common.annotations.Beta;
Expand Down Expand Up @@ -120,12 +121,15 @@ public Set<N> successors(Object node) {

@Override
public Set<E> edgeValue(Object nodeA, Object nodeB) {
return checkNotNull(edgesConnecting(nodeA, nodeB));
Set<E> edges = edgesConnecting(nodeA, nodeB);
checkArgument(!edges.isEmpty(), EDGE_CONNECTING_NOT_IN_GRAPH, nodeA, nodeB);
return edges;
}

@Override
public Set<E> edgeValueOrDefault(Object nodeA, Object nodeB, Set<E> defaultValue) {
return checkNotNull(edgesConnecting(nodeA, nodeB));
Set<E> edges = edgesConnecting(nodeA, nodeB);
return edges.isEmpty() ? defaultValue : edges;
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion guava/src/com/google/common/graph/Network.java
Expand Up @@ -111,7 +111,7 @@ public interface Network<N, E> {
* an edge connecting node A to node B iff this {@link Network} has an edge connecting A to B.
*
* <p>{@link Graph#edgeValue(Object, Object)} will return the set of edges connecting node A to
* node B. It will return the empty set if there are no edges connecting A to B.
* node B if the set is non-empty, otherwise, it will throw {@link IllegalArgumentException}.
*
* <p>If this network {@link #allowsParallelEdges()}, parallel edges will treated as if collapsed
* into a single edge. For example, the {@link #degree(Object)} of a node in the {@link Graph}
Expand Down

0 comments on commit d0b8e4a

Please sign in to comment.