All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spotify.confidence.GrpcUtil Maven / Gradle / Ivy

The newest version!
package com.spotify.confidence;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.concurrent.CompletableFuture;

final class GrpcUtil {

  private GrpcUtil() {}

  static  CompletableFuture toCompletableFuture(final ListenableFuture listenableFuture) {
    final CompletableFuture completableFuture =
        new CompletableFuture<>() {
          @Override
          public boolean cancel(boolean mayInterruptIfRunning) {
            listenableFuture.cancel(mayInterruptIfRunning);
            return super.cancel(mayInterruptIfRunning);
          }
        };
    Futures.addCallback(
        listenableFuture,
        new FutureCallback() {
          @Override
          public void onSuccess(T result) {
            completableFuture.complete(result);
          }

          @Override
          public void onFailure(Throwable t) {
            completableFuture.completeExceptionally(t);
          }
        },
        MoreExecutors.directExecutor());
    return completableFuture;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy