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

com.jetdrone.vertx.yoke.util.Promise Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy