package io.vertx.core.impl.future;
import io.vertx.core.Future;
import io.vertx.core.impl.ContextInternal;
import java.util.function.Function;
class EventuallyTransformation<T, U> extends Transformation<U> implements Listener<T> {
private final Function<Void, Future<U>> mapper;
EventuallyTransformation(ContextInternal context, Function<Void, Future<U>> mapper) {
super(context);
this.mapper = mapper;
}
@Override
public void onSuccess(T value) {
FutureInternal<U> future;
try {
future = (FutureInternal<U>) mapper.apply(null);
} catch (Throwable e) {
tryFail(e);
return;
}
future.addListener(newListener());
}
@Override
public void onFailure(Throwable failure) {
FutureInternal<U> future;
try {
future = (FutureInternal<U>) mapper.apply(null);
} catch (Throwable e) {
tryFail(e);
return;
}
future.addListener(newListener());
}
private Listener<U> newListener() {
return new Listener<U>() {
@Override
public void onSuccess(U value) {
tryComplete(value);
}
@Override
public void onFailure(Throwable failure) {
tryFail(failure);
}
};
}
}