Skip to content

Commit

Permalink
Add immediateVoidFuture() to Futures.java to create an immediately su…
Browse files Browse the repository at this point in the history
…cceeding ListenableFuture<Void>.

RELNOTES=`util.concurrent`: Added `immediateVoidFuture`.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=303756624
  • Loading branch information
ashish1294 authored and nick-someone committed Mar 31, 2020
1 parent 86e3620 commit 9f3bae5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -138,6 +139,14 @@ public void testImmediateFuture() throws Exception {
assertThat(future.toString()).contains("[status=SUCCESS, result=[" + DATA1 + "]]");
}

public void testImmediateVoidFuture() throws Exception {
ListenableFuture<Void> 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<String> future = immediateFailedFuture(exception);
Expand Down
11 changes: 11 additions & 0 deletions android/guava/src/com/google/common/util/concurrent/Futures.java
Expand Up @@ -135,6 +135,17 @@ public static <V> ListenableFuture<V> immediateFuture(@NullableDecl V value) {
return new ImmediateFuture<>(value);
}

/**
* Returns a successful {@code ListenableFuture<Void>}. 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<Void> immediateVoidFuture() {
return (ListenableFuture<Void>) ImmediateFuture.NULL;
}

/**
* Returns a {@code ListenableFuture} which has an exception set immediately upon construction.
*
Expand Down
Expand Up @@ -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();
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -138,6 +139,14 @@ public void testImmediateFuture() throws Exception {
assertThat(future.toString()).contains("[status=SUCCESS, result=[" + DATA1 + "]]");
}

public void testImmediateVoidFuture() throws Exception {
ListenableFuture<Void> 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<String> future = immediateFailedFuture(exception);
Expand Down
11 changes: 11 additions & 0 deletions guava/src/com/google/common/util/concurrent/Futures.java
Expand Up @@ -137,6 +137,17 @@ public static <V> ListenableFuture<V> immediateFuture(@Nullable V value) {
return new ImmediateFuture<>(value);
}

/**
* Returns a successful {@code ListenableFuture<Void>}. 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<Void> immediateVoidFuture() {
return (ListenableFuture<Void>) ImmediateFuture.NULL;
}

/**
* Returns a {@code ListenableFuture} which has an exception set immediately upon construction.
*
Expand Down

0 comments on commit 9f3bae5

Please sign in to comment.