Skip to content

Commit 9f3bae5

Browse files
ashish1294nick-someone
authored andcommitted
Add immediateVoidFuture() to Futures.java to create an immediately succeeding ListenableFuture<Void>.
RELNOTES=`util.concurrent`: Added `immediateVoidFuture`. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=303756624
1 parent 86e3620 commit 9f3bae5

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static com.google.common.util.concurrent.Futures.immediateCancelledFuture;
3232
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
3333
import static com.google.common.util.concurrent.Futures.immediateFuture;
34+
import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
3435
import static com.google.common.util.concurrent.Futures.inCompletionOrder;
3536
import static com.google.common.util.concurrent.Futures.lazyTransform;
3637
import static com.google.common.util.concurrent.Futures.nonCancellationPropagating;
@@ -138,6 +139,14 @@ public void testImmediateFuture() throws Exception {
138139
assertThat(future.toString()).contains("[status=SUCCESS, result=[" + DATA1 + "]]");
139140
}
140141

142+
public void testImmediateVoidFuture() throws Exception {
143+
ListenableFuture<Void> voidFuture = immediateVoidFuture();
144+
145+
assertThat(getDone(voidFuture)).isNull();
146+
assertThat(getDoneFromTimeoutOverload(voidFuture)).isNull();
147+
assertThat(voidFuture.toString()).contains("[status=SUCCESS, result=[null]]");
148+
}
149+
141150
public void testImmediateFailedFuture() throws Exception {
142151
Exception exception = new Exception();
143152
ListenableFuture<String> future = immediateFailedFuture(exception);

android/guava/src/com/google/common/util/concurrent/Futures.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ public static <V> ListenableFuture<V> immediateFuture(@NullableDecl V value) {
135135
return new ImmediateFuture<>(value);
136136
}
137137

138+
/**
139+
* Returns a successful {@code ListenableFuture<Void>}. This method is equivalent to {@code
140+
* immediateFuture(null)} except that it is restricted to produce futures of type {@code Void}.
141+
*
142+
* @since NEXT
143+
*/
144+
@SuppressWarnings("unchecked")
145+
public static ListenableFuture<Void> immediateVoidFuture() {
146+
return (ListenableFuture<Void>) ImmediateFuture.NULL;
147+
}
148+
138149
/**
139150
* Returns a {@code ListenableFuture} which has an exception set immediately upon construction.
140151
*

guava-gwt/test/com/google/common/util/concurrent/FuturesTest_gwt.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,33 @@ public void testImmediateFuture() throws Exception {
17461746
}
17471747
}
17481748

1749+
public void testImmediateVoidFuture() throws Exception {
1750+
com.google.common.util.concurrent.FuturesTest testCase = new com.google.common.util.concurrent.FuturesTest();
1751+
testCase.setUp();
1752+
Throwable failure = null;
1753+
try {
1754+
testCase.testImmediateVoidFuture();
1755+
} catch (Throwable t) {
1756+
failure = t;
1757+
}
1758+
try {
1759+
testCase.tearDown();
1760+
} catch (Throwable t) {
1761+
if (failure == null) {
1762+
failure = t;
1763+
}
1764+
}
1765+
if (failure instanceof Exception) {
1766+
throw (Exception) failure;
1767+
}
1768+
if (failure instanceof Error) {
1769+
throw (Error) failure;
1770+
}
1771+
if (failure != null) {
1772+
throw new RuntimeException(failure);
1773+
}
1774+
}
1775+
17491776
public void testNonCancellationPropagating_delegateCancelled() throws Exception {
17501777
com.google.common.util.concurrent.FuturesTest testCase = new com.google.common.util.concurrent.FuturesTest();
17511778
testCase.setUp();

guava-tests/test/com/google/common/util/concurrent/FuturesTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static com.google.common.util.concurrent.Futures.immediateCancelledFuture;
3232
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
3333
import static com.google.common.util.concurrent.Futures.immediateFuture;
34+
import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
3435
import static com.google.common.util.concurrent.Futures.inCompletionOrder;
3536
import static com.google.common.util.concurrent.Futures.lazyTransform;
3637
import static com.google.common.util.concurrent.Futures.nonCancellationPropagating;
@@ -138,6 +139,14 @@ public void testImmediateFuture() throws Exception {
138139
assertThat(future.toString()).contains("[status=SUCCESS, result=[" + DATA1 + "]]");
139140
}
140141

142+
public void testImmediateVoidFuture() throws Exception {
143+
ListenableFuture<Void> voidFuture = immediateVoidFuture();
144+
145+
assertThat(getDone(voidFuture)).isNull();
146+
assertThat(getDoneFromTimeoutOverload(voidFuture)).isNull();
147+
assertThat(voidFuture.toString()).contains("[status=SUCCESS, result=[null]]");
148+
}
149+
141150
public void testImmediateFailedFuture() throws Exception {
142151
Exception exception = new Exception();
143152
ListenableFuture<String> future = immediateFailedFuture(exception);

guava/src/com/google/common/util/concurrent/Futures.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ public static <V> ListenableFuture<V> immediateFuture(@Nullable V value) {
137137
return new ImmediateFuture<>(value);
138138
}
139139

140+
/**
141+
* Returns a successful {@code ListenableFuture<Void>}. This method is equivalent to {@code
142+
* immediateFuture(null)} except that it is restricted to produce futures of type {@code Void}.
143+
*
144+
* @since NEXT
145+
*/
146+
@SuppressWarnings("unchecked")
147+
public static ListenableFuture<Void> immediateVoidFuture() {
148+
return (ListenableFuture<Void>) ImmediateFuture.NULL;
149+
}
150+
140151
/**
141152
* Returns a {@code ListenableFuture} which has an exception set immediately upon construction.
142153
*

0 commit comments

Comments
 (0)