package io.vertx.cassandra.impl;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import java.util.Objects;
import java.util.function.Function;
class Util {
static <T> void handleOnContext(ListenableFuture<T> listenableFuture, Context context, Handler<AsyncResult<T>> handler) {
handleOnContext(listenableFuture, context, Function.identity(), handler);
}
static <I, O> void handleOnContext(ListenableFuture<I> listenableFuture, Context context, Function<I, O> converter, Handler<AsyncResult<O>> handler) {
Objects.requireNonNull(listenableFuture, "listenableFuture must not be null");
Objects.requireNonNull(context, "context must not be null");
Objects.requireNonNull(converter, "converter must not be null");
if (handler != null) {
Futures.addCallback(listenableFuture, new FutureCallback<I>() {
@Override
public void onSuccess(I result) {
context.runOnContext(v -> handler.handle(Future.succeededFuture(converter.apply(result))));
}
@Override
public void onFailure(Throwable t) {
context.runOnContext(v -> handler.handle(Future.failedFuture(t)));
}
});
}
}
}