package io.vertx.core.impl.future;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.NoStackTraceThrowable;
import java.util.function.Function;
public final class FailedFuture<T> extends FutureBase<T> {
private final Throwable cause;
public FailedFuture(Throwable t) {
this(null, t);
}
public FailedFuture(ContextInternal context, Throwable t) {
super(context);
this.cause = t != null ? t : new NoStackTraceThrowable(null);
}
public FailedFuture(String failureMessage) {
this(null, failureMessage);
}
public FailedFuture(ContextInternal context, String failureMessage) {
this(context, new NoStackTraceThrowable(failureMessage));
}
@Override
public boolean isComplete() {
return true;
}
@Override
public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
if (handler instanceof Listener) {
emitFailure(cause, (Listener<T>) handler);
} else {
emit(this, handler);
}
return this;
}
@Override
public Future<T> onSuccess(Handler<T> handler) {
return this;
}
@Override
public Future<T> onFailure(Handler<Throwable> handler) {
emit(cause, handler);
return this;
}
@Override
public void addListener(Listener<T> listener) {
emitFailure(cause, listener);
}
@Override
public T result() {
return null;
}
@Override
public Throwable cause() {
return cause;
}
@Override
public boolean succeeded() {
return false;
}
@Override
public boolean failed() {
return true;
}
@Override
public <U> Future<U> map(Function<T, U> mapper) {
return (Future<U>) this;
}
@Override
public <V> Future<V> map(V value) {
return (Future<V>) this;
}
@Override
public Future<T> otherwise(T value) {
return new SucceededFuture<>(context, value);
}
@Override
public String toString() {
return "Future{cause=" + cause.getMessage() + "}";
}
}