com.jetdrone.vertx.yoke.util.Promise Maven / Gradle / Ivy
package com.jetdrone.vertx.yoke.util;
import com.jetdrone.vertx.yoke.core.YokeAsyncResult;
import org.vertx.java.core.AsyncResultHandler;
import org.vertx.java.core.Handler;
final class Promise {
@SuppressWarnings("unchecked")
private Handler handler;
private boolean canceled;
private boolean resolved;
public void then(Handler handler) {
this.handler = handler;
}
public void then(AsyncResultHandler handler) {
this.handler = handler;
}
@SuppressWarnings("unchecked")
public void cancel(Object error) {
if (!canceled) {
canceled = true;
if (handler != null) {
if (handler instanceof AsyncResultHandler) {
handler.handle(new YokeAsyncResult(error, null));
handler = null;
return;
}
handler.handle(error);
handler = null;
}
}
}
@SuppressWarnings("unchecked")
public void resolve(T value) {
if (!canceled && !resolved) {
resolved = true;
if (handler != null) {
if (handler instanceof AsyncResultHandler) {
handler.handle(new YokeAsyncResult<>(value));
handler = null;
return;
}
handler.handle(value);
handler = null;
}
}
}
public boolean isCanceled() {
return canceled;
}
public boolean isResolved() {
return resolved;
}
}