Skip to content

Commit 09e655f

Browse files
cushonGoogle Java Core Libraries
authored andcommitted
Change the return types of transitiveClosure() and reachableNodes() to Immutable* types.
One of those changes is only a change in the declared return type; the other is a behavior change: - `reachableNodes()` already returned an immutable object, even though that was not reflected in the declared return type. - `transitiveClosure()` used to return a mutable object. RELNOTES=`graph`: Changed the return types of `transitiveClosure()` and `reachableNodes()` to `Immutable*` types. `reachableNodes()` already returned an immutable object (even though that was not reflected in the declared return type); `transitiveClosure()` used to return a mutable object. PiperOrigin-RevId: 607051970
1 parent 17c0328 commit 09e655f

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

android/guava/src/com/google/common/graph/Graphs.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*/
4444
@Beta
4545
@ElementTypesAreNonnullByDefault
46-
public final class Graphs {
46+
public final class Graphs extends GraphsBridgeMethods {
4747

4848
private Graphs() {}
4949

@@ -146,10 +146,13 @@ private static boolean canTraverseWithoutReusingEdge(
146146
* <p>This is a "snapshot" based on the current topology of {@code graph}, rather than a live view
147147
* of the transitive closure of {@code graph}. In other words, the returned {@link Graph} will not
148148
* be updated after modifications to {@code graph}.
149+
*
150+
* @since NEXT (present with return type {@code Graph} since 20.0)
149151
*/
150152
// TODO(b/31438252): Consider potential optimizations for this algorithm.
151-
public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
152-
MutableGraph<N> transitiveClosure = GraphBuilder.from(graph).allowsSelfLoops(true).build();
153+
public static <N> ImmutableGraph<N> transitiveClosure(Graph<N> graph) {
154+
ImmutableGraph.Builder<N> transitiveClosure =
155+
GraphBuilder.from(graph).allowsSelfLoops(true).<N>immutable();
153156
// Every node is, at a minimum, reachable from itself. Since the resulting transitive closure
154157
// will have no isolated nodes, we can skip adding nodes explicitly and let putEdge() do it.
155158

@@ -178,7 +181,7 @@ public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
178181
}
179182
}
180183

181-
return transitiveClosure;
184+
return transitiveClosure.build();
182185
}
183186

184187
/**
@@ -191,8 +194,9 @@ public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
191194
* not be updated after modifications to {@code graph}.
192195
*
193196
* @throws IllegalArgumentException if {@code node} is not present in {@code graph}
197+
* @since NEXT (present with return type {@code Set} since 20.0)
194198
*/
195-
public static <N> Set<N> reachableNodes(Graph<N> graph, N node) {
199+
public static <N> ImmutableSet<N> reachableNodes(Graph<N> graph, N node) {
196200
checkArgument(graph.nodes().contains(node), NODE_NOT_IN_GRAPH, node);
197201
return ImmutableSet.copyOf(Traverser.forGraph(graph).breadthFirst(node));
198202
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.google.common.graph;
2+
3+
import com.google.common.annotations.Beta;
4+
import java.util.Set;
5+
6+
/**
7+
* Supertype for {@link Graphs}, containing the old signatures of methods whose signatures we've
8+
* changed. This provides binary compatibility for users who compiled against the old signatures.
9+
*/
10+
@Beta
11+
@ElementTypesAreNonnullByDefault
12+
abstract class GraphsBridgeMethods {
13+
14+
@SuppressWarnings("PreferredInterfaceType")
15+
public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
16+
return Graphs.transitiveClosure(graph);
17+
}
18+
19+
@SuppressWarnings("PreferredInterfaceType")
20+
public static <N> Set<N> reachableNodes(Graph<N> graph, N node) {
21+
return Graphs.reachableNodes(graph, node);
22+
}
23+
}

guava/src/com/google/common/graph/Graphs.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*/
4545
@Beta
4646
@ElementTypesAreNonnullByDefault
47-
public final class Graphs {
47+
public final class Graphs extends GraphsBridgeMethods {
4848

4949
private Graphs() {}
5050

@@ -147,10 +147,13 @@ private static boolean canTraverseWithoutReusingEdge(
147147
* <p>This is a "snapshot" based on the current topology of {@code graph}, rather than a live view
148148
* of the transitive closure of {@code graph}. In other words, the returned {@link Graph} will not
149149
* be updated after modifications to {@code graph}.
150+
*
151+
* @since NEXT (present with return type {@code Graph} since 20.0)
150152
*/
151153
// TODO(b/31438252): Consider potential optimizations for this algorithm.
152-
public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
153-
MutableGraph<N> transitiveClosure = GraphBuilder.from(graph).allowsSelfLoops(true).build();
154+
public static <N> ImmutableGraph<N> transitiveClosure(Graph<N> graph) {
155+
ImmutableGraph.Builder<N> transitiveClosure =
156+
GraphBuilder.from(graph).allowsSelfLoops(true).<N>immutable();
154157
// Every node is, at a minimum, reachable from itself. Since the resulting transitive closure
155158
// will have no isolated nodes, we can skip adding nodes explicitly and let putEdge() do it.
156159

@@ -179,7 +182,7 @@ public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
179182
}
180183
}
181184

182-
return transitiveClosure;
185+
return transitiveClosure.build();
183186
}
184187

185188
/**
@@ -192,8 +195,9 @@ public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
192195
* not be updated after modifications to {@code graph}.
193196
*
194197
* @throws IllegalArgumentException if {@code node} is not present in {@code graph}
198+
* @since NEXT (present with return type {@code Set} since 20.0)
195199
*/
196-
public static <N> Set<N> reachableNodes(Graph<N> graph, N node) {
200+
public static <N> ImmutableSet<N> reachableNodes(Graph<N> graph, N node) {
197201
checkArgument(graph.nodes().contains(node), NODE_NOT_IN_GRAPH, node);
198202
return ImmutableSet.copyOf(Traverser.forGraph(graph).breadthFirst(node));
199203
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.google.common.graph;
2+
3+
import com.google.common.annotations.Beta;
4+
import java.util.Set;
5+
6+
/**
7+
* Supertype for {@link Graphs}, containing the old signatures of methods whose signatures we've
8+
* changed. This provides binary compatibility for users who compiled against the old signatures.
9+
*/
10+
@Beta
11+
@ElementTypesAreNonnullByDefault
12+
abstract class GraphsBridgeMethods {
13+
14+
@SuppressWarnings("PreferredInterfaceType")
15+
public static <N> Graph<N> transitiveClosure(Graph<N> graph) {
16+
return Graphs.transitiveClosure(graph);
17+
}
18+
19+
@SuppressWarnings("PreferredInterfaceType")
20+
public static <N> Set<N> reachableNodes(Graph<N> graph, N node) {
21+
return Graphs.reachableNodes(graph, node);
22+
}
23+
}

0 commit comments

Comments
 (0)