From 9f3bae585349808c5a6fe6c88db0f573036d553f Mon Sep 17 00:00:00 2001 From: ashishkedia Date: Mon, 30 Mar 2020 09:26:40 -0700 Subject: [PATCH] Add immediateVoidFuture() to Futures.java to create an immediately succeeding ListenableFuture. RELNOTES=`util.concurrent`: Added `immediateVoidFuture`. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=303756624 --- .../common/util/concurrent/FuturesTest.java | 9 +++++++ .../common/util/concurrent/Futures.java | 11 ++++++++ .../util/concurrent/FuturesTest_gwt.java | 27 +++++++++++++++++++ .../common/util/concurrent/FuturesTest.java | 9 +++++++ .../common/util/concurrent/Futures.java | 11 ++++++++ 5 files changed, 67 insertions(+) diff --git a/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java b/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java index b96ac8515383..38c6a41907a8 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java @@ -31,6 +31,7 @@ import static com.google.common.util.concurrent.Futures.immediateCancelledFuture; import static com.google.common.util.concurrent.Futures.immediateFailedFuture; import static com.google.common.util.concurrent.Futures.immediateFuture; +import static com.google.common.util.concurrent.Futures.immediateVoidFuture; import static com.google.common.util.concurrent.Futures.inCompletionOrder; import static com.google.common.util.concurrent.Futures.lazyTransform; import static com.google.common.util.concurrent.Futures.nonCancellationPropagating; @@ -138,6 +139,14 @@ public void testImmediateFuture() throws Exception { assertThat(future.toString()).contains("[status=SUCCESS, result=[" + DATA1 + "]]"); } + public void testImmediateVoidFuture() throws Exception { + ListenableFuture voidFuture = immediateVoidFuture(); + + assertThat(getDone(voidFuture)).isNull(); + assertThat(getDoneFromTimeoutOverload(voidFuture)).isNull(); + assertThat(voidFuture.toString()).contains("[status=SUCCESS, result=[null]]"); + } + public void testImmediateFailedFuture() throws Exception { Exception exception = new Exception(); ListenableFuture future = immediateFailedFuture(exception); diff --git a/android/guava/src/com/google/common/util/concurrent/Futures.java b/android/guava/src/com/google/common/util/concurrent/Futures.java index 93b336833814..f04fd8d4912a 100644 --- a/android/guava/src/com/google/common/util/concurrent/Futures.java +++ b/android/guava/src/com/google/common/util/concurrent/Futures.java @@ -135,6 +135,17 @@ public static ListenableFuture immediateFuture(@NullableDecl V value) { return new ImmediateFuture<>(value); } + /** + * Returns a successful {@code ListenableFuture}. This method is equivalent to {@code + * immediateFuture(null)} except that it is restricted to produce futures of type {@code Void}. + * + * @since NEXT + */ + @SuppressWarnings("unchecked") + public static ListenableFuture immediateVoidFuture() { + return (ListenableFuture) ImmediateFuture.NULL; + } + /** * Returns a {@code ListenableFuture} which has an exception set immediately upon construction. * diff --git a/guava-gwt/test/com/google/common/util/concurrent/FuturesTest_gwt.java b/guava-gwt/test/com/google/common/util/concurrent/FuturesTest_gwt.java index b87108eebccb..60e160919c88 100644 --- a/guava-gwt/test/com/google/common/util/concurrent/FuturesTest_gwt.java +++ b/guava-gwt/test/com/google/common/util/concurrent/FuturesTest_gwt.java @@ -1746,6 +1746,33 @@ public void testImmediateFuture() throws Exception { } } +public void testImmediateVoidFuture() throws Exception { + com.google.common.util.concurrent.FuturesTest testCase = new com.google.common.util.concurrent.FuturesTest(); + testCase.setUp(); + Throwable failure = null; + try { + testCase.testImmediateVoidFuture(); + } catch (Throwable t) { + failure = t; + } + try { + testCase.tearDown(); + } catch (Throwable t) { + if (failure == null) { + failure = t; + } + } + if (failure instanceof Exception) { + throw (Exception) failure; + } + if (failure instanceof Error) { + throw (Error) failure; + } + if (failure != null) { + throw new RuntimeException(failure); + } +} + public void testNonCancellationPropagating_delegateCancelled() throws Exception { com.google.common.util.concurrent.FuturesTest testCase = new com.google.common.util.concurrent.FuturesTest(); testCase.setUp(); diff --git a/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java b/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java index 8ba8a8e4fe49..d82496faabe4 100644 --- a/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java @@ -31,6 +31,7 @@ import static com.google.common.util.concurrent.Futures.immediateCancelledFuture; import static com.google.common.util.concurrent.Futures.immediateFailedFuture; import static com.google.common.util.concurrent.Futures.immediateFuture; +import static com.google.common.util.concurrent.Futures.immediateVoidFuture; import static com.google.common.util.concurrent.Futures.inCompletionOrder; import static com.google.common.util.concurrent.Futures.lazyTransform; import static com.google.common.util.concurrent.Futures.nonCancellationPropagating; @@ -138,6 +139,14 @@ public void testImmediateFuture() throws Exception { assertThat(future.toString()).contains("[status=SUCCESS, result=[" + DATA1 + "]]"); } + public void testImmediateVoidFuture() throws Exception { + ListenableFuture voidFuture = immediateVoidFuture(); + + assertThat(getDone(voidFuture)).isNull(); + assertThat(getDoneFromTimeoutOverload(voidFuture)).isNull(); + assertThat(voidFuture.toString()).contains("[status=SUCCESS, result=[null]]"); + } + public void testImmediateFailedFuture() throws Exception { Exception exception = new Exception(); ListenableFuture future = immediateFailedFuture(exception); diff --git a/guava/src/com/google/common/util/concurrent/Futures.java b/guava/src/com/google/common/util/concurrent/Futures.java index 8b05b395cc9f..2a6bbf89a52f 100644 --- a/guava/src/com/google/common/util/concurrent/Futures.java +++ b/guava/src/com/google/common/util/concurrent/Futures.java @@ -137,6 +137,17 @@ public static ListenableFuture immediateFuture(@Nullable V value) { return new ImmediateFuture<>(value); } + /** + * Returns a successful {@code ListenableFuture}. This method is equivalent to {@code + * immediateFuture(null)} except that it is restricted to produce futures of type {@code Void}. + * + * @since NEXT + */ + @SuppressWarnings("unchecked") + public static ListenableFuture immediateVoidFuture() { + return (ListenableFuture) ImmediateFuture.NULL; + } + /** * Returns a {@code ListenableFuture} which has an exception set immediately upon construction. *