mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-21 13:58:07 +01:00
Use a common utility for turning Google API futures into CompletableFutures
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package org.whispersystems.textsecuregcm.util;
|
||||
|
||||
import com.google.api.core.ApiFuture;
|
||||
import com.google.api.core.ApiFutureCallback;
|
||||
import com.google.api.core.ApiFutures;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
public class GoogleApiUtil {
|
||||
|
||||
public static <T> CompletableFuture<T> toCompletableFuture(final ApiFuture<T> apiFuture, final Executor executor) {
|
||||
final CompletableFuture<T> completableFuture = new CompletableFuture<>();
|
||||
|
||||
ApiFutures.addCallback(apiFuture, new ApiFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(final T value) {
|
||||
completableFuture.complete(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(final Throwable throwable) {
|
||||
completableFuture.completeExceptionally(throwable);
|
||||
}
|
||||
}, executor);
|
||||
|
||||
return completableFuture;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user