From ac122499bd07ed588e1e37b1040f05bfa8a9c862 Mon Sep 17 00:00:00 2001 From: jnyman Date: Thu, 5 Dec 2019 20:22:25 -0800 Subject: [PATCH] Support stable incident edge order for directed Immutable[Value]Graphs. RELNOTES=n/a ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=284109953 --- .../common/graph/AbstractGraphTest.java | 16 ++--- .../StandardImmutableDirectedGraphTest.java | 34 +++++------ .../StandardMutableDirectedGraphTest.java | 11 ++-- .../StandardMutableUndirectedGraphTest.java | 11 ++-- .../graph/DirectedGraphConnections.java | 59 +++++++++++++++---- .../google/common/graph/ImmutableGraph.java | 7 ++- .../common/graph/ImmutableValueGraph.java | 2 +- .../common/graph/AbstractGraphTest.java | 16 ++--- .../StandardImmutableDirectedGraphTest.java | 34 +++++------ .../StandardMutableDirectedGraphTest.java | 11 ++-- .../StandardMutableUndirectedGraphTest.java | 11 ++-- .../graph/DirectedGraphConnections.java | 59 +++++++++++++++---- .../google/common/graph/ImmutableGraph.java | 7 ++- .../common/graph/ImmutableValueGraph.java | 2 +- 14 files changed, 158 insertions(+), 122 deletions(-) diff --git a/android/guava-tests/test/com/google/common/graph/AbstractGraphTest.java b/android/guava-tests/test/com/google/common/graph/AbstractGraphTest.java index 83a3e54c9fda..dfbf78e87363 100644 --- a/android/guava-tests/test/com/google/common/graph/AbstractGraphTest.java +++ b/android/guava-tests/test/com/google/common/graph/AbstractGraphTest.java @@ -25,7 +25,6 @@ import static org.junit.Assert.fail; import com.google.common.collect.ImmutableSet; -import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.HashSet; import java.util.Set; import org.junit.After; @@ -81,25 +80,18 @@ public abstract class AbstractGraphTest { * A proxy method that adds the node {@code n} to the graph being tested. In case of Immutable * graph implementations, this method should replace {@link #graph} with a new graph that includes * this node. - * - * @return {@code true} iff the graph was modified as a result of this call */ - @CanIgnoreReturnValue - abstract boolean addNode(Integer n); + abstract void addNode(Integer n); /** * A proxy method that adds the edge {@code e} to the graph being tested. In case of Immutable * graph implementations, this method should replace {@link #graph} with a new graph that includes * this edge. - * - * @return {@code true} iff the graph was modified as a result of this call */ - @CanIgnoreReturnValue - abstract boolean putEdge(Integer n1, Integer n2); + abstract void putEdge(Integer n1, Integer n2); - @CanIgnoreReturnValue - final boolean putEdge(EndpointPair endpoints) { - return putEdge(endpoints.nodeU(), endpoints.nodeV()); + final void putEdge(EndpointPair endpoints) { + putEdge(endpoints.nodeU(), endpoints.nodeV()); } final boolean graphIsMutable() { diff --git a/android/guava-tests/test/com/google/common/graph/StandardImmutableDirectedGraphTest.java b/android/guava-tests/test/com/google/common/graph/StandardImmutableDirectedGraphTest.java index c03bde45caa1..eb8220816dd1 100644 --- a/android/guava-tests/test/com/google/common/graph/StandardImmutableDirectedGraphTest.java +++ b/android/guava-tests/test/com/google/common/graph/StandardImmutableDirectedGraphTest.java @@ -16,7 +16,6 @@ package com.google.common.graph; -import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.Arrays; import java.util.Collection; import org.junit.runner.RunWith; @@ -34,12 +33,14 @@ public static Collection parameters() { new Object[][] { {false, ElementOrder.unordered()}, {true, ElementOrder.unordered()}, - // TODO(b/142723300): Add ElementOrder.stable() once it is supported + {false, ElementOrder.stable()}, + {true, ElementOrder.stable()} }); } private final boolean allowsSelfLoops; private final ElementOrder incidentEdgeOrder; + private ImmutableGraph.Builder graphBuilder; public StandardImmutableDirectedGraphTest( boolean allowsSelfLoops, ElementOrder incidentEdgeOrder) { @@ -59,28 +60,23 @@ ElementOrder incidentEdgeOrder() { @Override public Graph createGraph() { - return GraphBuilder.directed() - .allowsSelfLoops(allowsSelfLoops()) - .incidentEdgeOrder(incidentEdgeOrder) - .immutable() - .build(); + graphBuilder = + GraphBuilder.directed() + .allowsSelfLoops(allowsSelfLoops()) + .incidentEdgeOrder(incidentEdgeOrder) + .immutable(); + return graphBuilder.build(); } - @CanIgnoreReturnValue @Override - final boolean addNode(Integer n) { - MutableGraph mutableGraph = Graphs.copyOf(graph); - boolean somethingChanged = mutableGraph.addNode(n); - graph = ImmutableGraph.copyOf(mutableGraph); - return somethingChanged; + final void addNode(Integer n) { + graphBuilder.addNode(n); + graph = graphBuilder.build(); } - @CanIgnoreReturnValue @Override - final boolean putEdge(Integer n1, Integer n2) { - MutableGraph mutableGraph = Graphs.copyOf(graph); - boolean somethingChanged = mutableGraph.putEdge(n1, n2); - graph = ImmutableGraph.copyOf(mutableGraph); - return somethingChanged; + final void putEdge(Integer n1, Integer n2) { + graphBuilder.putEdge(n1, n2); + graph = graphBuilder.build(); } } diff --git a/android/guava-tests/test/com/google/common/graph/StandardMutableDirectedGraphTest.java b/android/guava-tests/test/com/google/common/graph/StandardMutableDirectedGraphTest.java index 8f1e18096224..974ee1d31565 100644 --- a/android/guava-tests/test/com/google/common/graph/StandardMutableDirectedGraphTest.java +++ b/android/guava-tests/test/com/google/common/graph/StandardMutableDirectedGraphTest.java @@ -16,7 +16,6 @@ package com.google.common.graph; -import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.Arrays; import java.util.Collection; import org.junit.runner.RunWith; @@ -66,15 +65,13 @@ public MutableGraph createGraph() { .build(); } - @CanIgnoreReturnValue @Override - final boolean addNode(Integer n) { - return graphAsMutableGraph.addNode(n); + final void addNode(Integer n) { + graphAsMutableGraph.addNode(n); } - @CanIgnoreReturnValue @Override - final boolean putEdge(Integer n1, Integer n2) { - return graphAsMutableGraph.putEdge(n1, n2); + final void putEdge(Integer n1, Integer n2) { + graphAsMutableGraph.putEdge(n1, n2); } } diff --git a/android/guava-tests/test/com/google/common/graph/StandardMutableUndirectedGraphTest.java b/android/guava-tests/test/com/google/common/graph/StandardMutableUndirectedGraphTest.java index 3c9f2f924836..b77f50e9f0a4 100644 --- a/android/guava-tests/test/com/google/common/graph/StandardMutableUndirectedGraphTest.java +++ b/android/guava-tests/test/com/google/common/graph/StandardMutableUndirectedGraphTest.java @@ -16,7 +16,6 @@ package com.google.common.graph; -import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.Arrays; import java.util.Collection; import org.junit.runner.RunWith; @@ -52,15 +51,13 @@ public MutableGraph createGraph() { return GraphBuilder.undirected().allowsSelfLoops(allowsSelfLoops()).build(); } - @CanIgnoreReturnValue @Override - final boolean addNode(Integer n) { - return graphAsMutableGraph.addNode(n); + final void addNode(Integer n) { + graphAsMutableGraph.addNode(n); } - @CanIgnoreReturnValue @Override - final boolean putEdge(Integer n1, Integer n2) { - return graphAsMutableGraph.putEdge(n1, n2); + final void putEdge(Integer n1, Integer n2) { + graphAsMutableGraph.putEdge(n1, n2); } } diff --git a/android/guava/src/com/google/common/graph/DirectedGraphConnections.java b/android/guava/src/com/google/common/graph/DirectedGraphConnections.java index c523a96f4d18..77fd83db0ad9 100644 --- a/android/guava/src/com/google/common/graph/DirectedGraphConnections.java +++ b/android/guava/src/com/google/common/graph/DirectedGraphConnections.java @@ -16,6 +16,7 @@ package com.google.common.graph; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static com.google.common.graph.GraphConstants.INNER_CAPACITY; @@ -25,7 +26,7 @@ import com.google.common.base.Function; import com.google.common.collect.AbstractIterator; -import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; import com.google.common.collect.UnmodifiableIterator; import java.util.AbstractSet; @@ -175,21 +176,55 @@ static DirectedGraphConnections of(ElementOrder incidentEdgeOrde } static DirectedGraphConnections ofImmutable( - Set predecessors, Map successorValues) { + N thisNode, Iterable> incidentEdges, Function successorNodeToValueFn) { Map adjacentNodeValues = new HashMap<>(); - adjacentNodeValues.putAll(successorValues); - for (N predecessor : predecessors) { - Object value = adjacentNodeValues.put(predecessor, PRED); - if (value != null) { - adjacentNodeValues.put(predecessor, new PredAndSucc(value)); + ImmutableList.Builder> orderedNodeConnectionsBuilder = + ImmutableList.builder(); + int predecessorCount = 0; + int successorCount = 0; + + for (EndpointPair incidentEdge : incidentEdges) { + if (incidentEdge.nodeU().equals(thisNode) && incidentEdge.nodeV().equals(thisNode)) { + // incidentEdge is a self-loop + + adjacentNodeValues.put(thisNode, new PredAndSucc(successorNodeToValueFn.apply(thisNode))); + + orderedNodeConnectionsBuilder.add(new NodeConnection.Pred<>(thisNode)); + orderedNodeConnectionsBuilder.add(new NodeConnection.Succ<>(thisNode)); + predecessorCount++; + successorCount++; + } else if (incidentEdge.nodeV().equals(thisNode)) { // incidentEdge is an inEdge + N predecessor = incidentEdge.nodeU(); + + Object existingValue = adjacentNodeValues.put(predecessor, PRED); + if (existingValue != null) { + adjacentNodeValues.put(predecessor, new PredAndSucc(existingValue)); + } + + orderedNodeConnectionsBuilder.add(new NodeConnection.Pred<>(predecessor)); + predecessorCount++; + } else { // incidentEdge is an outEdge + checkArgument(incidentEdge.nodeU().equals(thisNode)); + + N successor = incidentEdge.nodeV(); + V value = successorNodeToValueFn.apply(successor); + + Object existingValue = adjacentNodeValues.put(successor, value); + if (existingValue != null) { + checkArgument(existingValue == PRED); + adjacentNodeValues.put(successor, new PredAndSucc(value)); + } + + orderedNodeConnectionsBuilder.add(new NodeConnection.Succ<>(successor)); + successorCount++; } } + return new DirectedGraphConnections<>( - /* adjacentNodeValues = */ ImmutableMap.copyOf(adjacentNodeValues), - // TODO(b/142723300): Pass in an ImmutableList here with the ordered node connections - /* orderedNodeConnections = */ null, - /* predecessorCount = */ predecessors.size(), - /* successorCount = */ successorValues.size()); + adjacentNodeValues, + orderedNodeConnectionsBuilder.build(), + predecessorCount, + successorCount); } @Override diff --git a/android/guava/src/com/google/common/graph/ImmutableGraph.java b/android/guava/src/com/google/common/graph/ImmutableGraph.java index ead5217ea379..1bd6d920a76f 100644 --- a/android/guava/src/com/google/common/graph/ImmutableGraph.java +++ b/android/guava/src/com/google/common/graph/ImmutableGraph.java @@ -84,11 +84,12 @@ private static ImmutableMap> getNodeConnect return nodeConnections.build(); } + @SuppressWarnings("unchecked") private static GraphConnections connectionsOf(Graph graph, N node) { - Function edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); + Function edgeValueFn = + (Function) Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() - ? DirectedGraphConnections.ofImmutable( - graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) + ? DirectedGraphConnections.ofImmutable(node, graph.incidentEdges(node), edgeValueFn) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); } diff --git a/android/guava/src/com/google/common/graph/ImmutableValueGraph.java b/android/guava/src/com/google/common/graph/ImmutableValueGraph.java index 3fe163118a3d..42f175c9ee25 100644 --- a/android/guava/src/com/google/common/graph/ImmutableValueGraph.java +++ b/android/guava/src/com/google/common/graph/ImmutableValueGraph.java @@ -94,7 +94,7 @@ public V apply(N successorNode) { }; return graph.isDirected() ? DirectedGraphConnections.ofImmutable( - graph.predecessors(node), Maps.asMap(graph.successors(node), successorNodeToValueFn)) + node, graph.incidentEdges(node), successorNodeToValueFn) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn)); } diff --git a/guava-tests/test/com/google/common/graph/AbstractGraphTest.java b/guava-tests/test/com/google/common/graph/AbstractGraphTest.java index 83a3e54c9fda..dfbf78e87363 100644 --- a/guava-tests/test/com/google/common/graph/AbstractGraphTest.java +++ b/guava-tests/test/com/google/common/graph/AbstractGraphTest.java @@ -25,7 +25,6 @@ import static org.junit.Assert.fail; import com.google.common.collect.ImmutableSet; -import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.HashSet; import java.util.Set; import org.junit.After; @@ -81,25 +80,18 @@ public abstract class AbstractGraphTest { * A proxy method that adds the node {@code n} to the graph being tested. In case of Immutable * graph implementations, this method should replace {@link #graph} with a new graph that includes * this node. - * - * @return {@code true} iff the graph was modified as a result of this call */ - @CanIgnoreReturnValue - abstract boolean addNode(Integer n); + abstract void addNode(Integer n); /** * A proxy method that adds the edge {@code e} to the graph being tested. In case of Immutable * graph implementations, this method should replace {@link #graph} with a new graph that includes * this edge. - * - * @return {@code true} iff the graph was modified as a result of this call */ - @CanIgnoreReturnValue - abstract boolean putEdge(Integer n1, Integer n2); + abstract void putEdge(Integer n1, Integer n2); - @CanIgnoreReturnValue - final boolean putEdge(EndpointPair endpoints) { - return putEdge(endpoints.nodeU(), endpoints.nodeV()); + final void putEdge(EndpointPair endpoints) { + putEdge(endpoints.nodeU(), endpoints.nodeV()); } final boolean graphIsMutable() { diff --git a/guava-tests/test/com/google/common/graph/StandardImmutableDirectedGraphTest.java b/guava-tests/test/com/google/common/graph/StandardImmutableDirectedGraphTest.java index c03bde45caa1..eb8220816dd1 100644 --- a/guava-tests/test/com/google/common/graph/StandardImmutableDirectedGraphTest.java +++ b/guava-tests/test/com/google/common/graph/StandardImmutableDirectedGraphTest.java @@ -16,7 +16,6 @@ package com.google.common.graph; -import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.Arrays; import java.util.Collection; import org.junit.runner.RunWith; @@ -34,12 +33,14 @@ public static Collection parameters() { new Object[][] { {false, ElementOrder.unordered()}, {true, ElementOrder.unordered()}, - // TODO(b/142723300): Add ElementOrder.stable() once it is supported + {false, ElementOrder.stable()}, + {true, ElementOrder.stable()} }); } private final boolean allowsSelfLoops; private final ElementOrder incidentEdgeOrder; + private ImmutableGraph.Builder graphBuilder; public StandardImmutableDirectedGraphTest( boolean allowsSelfLoops, ElementOrder incidentEdgeOrder) { @@ -59,28 +60,23 @@ ElementOrder incidentEdgeOrder() { @Override public Graph createGraph() { - return GraphBuilder.directed() - .allowsSelfLoops(allowsSelfLoops()) - .incidentEdgeOrder(incidentEdgeOrder) - .immutable() - .build(); + graphBuilder = + GraphBuilder.directed() + .allowsSelfLoops(allowsSelfLoops()) + .incidentEdgeOrder(incidentEdgeOrder) + .immutable(); + return graphBuilder.build(); } - @CanIgnoreReturnValue @Override - final boolean addNode(Integer n) { - MutableGraph mutableGraph = Graphs.copyOf(graph); - boolean somethingChanged = mutableGraph.addNode(n); - graph = ImmutableGraph.copyOf(mutableGraph); - return somethingChanged; + final void addNode(Integer n) { + graphBuilder.addNode(n); + graph = graphBuilder.build(); } - @CanIgnoreReturnValue @Override - final boolean putEdge(Integer n1, Integer n2) { - MutableGraph mutableGraph = Graphs.copyOf(graph); - boolean somethingChanged = mutableGraph.putEdge(n1, n2); - graph = ImmutableGraph.copyOf(mutableGraph); - return somethingChanged; + final void putEdge(Integer n1, Integer n2) { + graphBuilder.putEdge(n1, n2); + graph = graphBuilder.build(); } } diff --git a/guava-tests/test/com/google/common/graph/StandardMutableDirectedGraphTest.java b/guava-tests/test/com/google/common/graph/StandardMutableDirectedGraphTest.java index 8f1e18096224..974ee1d31565 100644 --- a/guava-tests/test/com/google/common/graph/StandardMutableDirectedGraphTest.java +++ b/guava-tests/test/com/google/common/graph/StandardMutableDirectedGraphTest.java @@ -16,7 +16,6 @@ package com.google.common.graph; -import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.Arrays; import java.util.Collection; import org.junit.runner.RunWith; @@ -66,15 +65,13 @@ public MutableGraph createGraph() { .build(); } - @CanIgnoreReturnValue @Override - final boolean addNode(Integer n) { - return graphAsMutableGraph.addNode(n); + final void addNode(Integer n) { + graphAsMutableGraph.addNode(n); } - @CanIgnoreReturnValue @Override - final boolean putEdge(Integer n1, Integer n2) { - return graphAsMutableGraph.putEdge(n1, n2); + final void putEdge(Integer n1, Integer n2) { + graphAsMutableGraph.putEdge(n1, n2); } } diff --git a/guava-tests/test/com/google/common/graph/StandardMutableUndirectedGraphTest.java b/guava-tests/test/com/google/common/graph/StandardMutableUndirectedGraphTest.java index 3c9f2f924836..b77f50e9f0a4 100644 --- a/guava-tests/test/com/google/common/graph/StandardMutableUndirectedGraphTest.java +++ b/guava-tests/test/com/google/common/graph/StandardMutableUndirectedGraphTest.java @@ -16,7 +16,6 @@ package com.google.common.graph; -import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.util.Arrays; import java.util.Collection; import org.junit.runner.RunWith; @@ -52,15 +51,13 @@ public MutableGraph createGraph() { return GraphBuilder.undirected().allowsSelfLoops(allowsSelfLoops()).build(); } - @CanIgnoreReturnValue @Override - final boolean addNode(Integer n) { - return graphAsMutableGraph.addNode(n); + final void addNode(Integer n) { + graphAsMutableGraph.addNode(n); } - @CanIgnoreReturnValue @Override - final boolean putEdge(Integer n1, Integer n2) { - return graphAsMutableGraph.putEdge(n1, n2); + final void putEdge(Integer n1, Integer n2) { + graphAsMutableGraph.putEdge(n1, n2); } } diff --git a/guava/src/com/google/common/graph/DirectedGraphConnections.java b/guava/src/com/google/common/graph/DirectedGraphConnections.java index 030f87b1a03f..bf8ead496329 100644 --- a/guava/src/com/google/common/graph/DirectedGraphConnections.java +++ b/guava/src/com/google/common/graph/DirectedGraphConnections.java @@ -16,6 +16,7 @@ package com.google.common.graph; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static com.google.common.graph.GraphConstants.INNER_CAPACITY; @@ -25,7 +26,7 @@ import com.google.common.base.Function; import com.google.common.collect.AbstractIterator; -import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; import com.google.common.collect.UnmodifiableIterator; import java.util.AbstractSet; @@ -175,21 +176,55 @@ static DirectedGraphConnections of(ElementOrder incidentEdgeOrde } static DirectedGraphConnections ofImmutable( - Set predecessors, Map successorValues) { + N thisNode, Iterable> incidentEdges, Function successorNodeToValueFn) { Map adjacentNodeValues = new HashMap<>(); - adjacentNodeValues.putAll(successorValues); - for (N predecessor : predecessors) { - Object value = adjacentNodeValues.put(predecessor, PRED); - if (value != null) { - adjacentNodeValues.put(predecessor, new PredAndSucc(value)); + ImmutableList.Builder> orderedNodeConnectionsBuilder = + ImmutableList.builder(); + int predecessorCount = 0; + int successorCount = 0; + + for (EndpointPair incidentEdge : incidentEdges) { + if (incidentEdge.nodeU().equals(thisNode) && incidentEdge.nodeV().equals(thisNode)) { + // incidentEdge is a self-loop + + adjacentNodeValues.put(thisNode, new PredAndSucc(successorNodeToValueFn.apply(thisNode))); + + orderedNodeConnectionsBuilder.add(new NodeConnection.Pred<>(thisNode)); + orderedNodeConnectionsBuilder.add(new NodeConnection.Succ<>(thisNode)); + predecessorCount++; + successorCount++; + } else if (incidentEdge.nodeV().equals(thisNode)) { // incidentEdge is an inEdge + N predecessor = incidentEdge.nodeU(); + + Object existingValue = adjacentNodeValues.put(predecessor, PRED); + if (existingValue != null) { + adjacentNodeValues.put(predecessor, new PredAndSucc(existingValue)); + } + + orderedNodeConnectionsBuilder.add(new NodeConnection.Pred<>(predecessor)); + predecessorCount++; + } else { // incidentEdge is an outEdge + checkArgument(incidentEdge.nodeU().equals(thisNode)); + + N successor = incidentEdge.nodeV(); + V value = successorNodeToValueFn.apply(successor); + + Object existingValue = adjacentNodeValues.put(successor, value); + if (existingValue != null) { + checkArgument(existingValue == PRED); + adjacentNodeValues.put(successor, new PredAndSucc(value)); + } + + orderedNodeConnectionsBuilder.add(new NodeConnection.Succ<>(successor)); + successorCount++; } } + return new DirectedGraphConnections<>( - /* adjacentNodeValues = */ ImmutableMap.copyOf(adjacentNodeValues), - // TODO(b/142723300): Pass in an ImmutableList here with the ordered node connections - /* orderedNodeConnections = */ null, - /* predecessorCount = */ predecessors.size(), - /* successorCount = */ successorValues.size()); + adjacentNodeValues, + orderedNodeConnectionsBuilder.build(), + predecessorCount, + successorCount); } @Override diff --git a/guava/src/com/google/common/graph/ImmutableGraph.java b/guava/src/com/google/common/graph/ImmutableGraph.java index ead5217ea379..1bd6d920a76f 100644 --- a/guava/src/com/google/common/graph/ImmutableGraph.java +++ b/guava/src/com/google/common/graph/ImmutableGraph.java @@ -84,11 +84,12 @@ private static ImmutableMap> getNodeConnect return nodeConnections.build(); } + @SuppressWarnings("unchecked") private static GraphConnections connectionsOf(Graph graph, N node) { - Function edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); + Function edgeValueFn = + (Function) Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() - ? DirectedGraphConnections.ofImmutable( - graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) + ? DirectedGraphConnections.ofImmutable(node, graph.incidentEdges(node), edgeValueFn) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); } diff --git a/guava/src/com/google/common/graph/ImmutableValueGraph.java b/guava/src/com/google/common/graph/ImmutableValueGraph.java index 3fe163118a3d..42f175c9ee25 100644 --- a/guava/src/com/google/common/graph/ImmutableValueGraph.java +++ b/guava/src/com/google/common/graph/ImmutableValueGraph.java @@ -94,7 +94,7 @@ public V apply(N successorNode) { }; return graph.isDirected() ? DirectedGraphConnections.ofImmutable( - graph.predecessors(node), Maps.asMap(graph.successors(node), successorNodeToValueFn)) + node, graph.incidentEdges(node), successorNodeToValueFn) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn)); }