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

io.proximax.async.AsyncCallbacks Maven / Gradle / Ivy

The newest version!
package io.proximax.async;

import java.util.function.Consumer;

/**
 * This model class wraps the callbacks for asynchronous invocation
 * @param  the result type that this class will process
 */
public class AsyncCallbacks {

    private final Consumer successCallback;
    private final Consumer failureCallback;

    public AsyncCallbacks(Consumer successCallback, Consumer failureCallback) {
        this.successCallback = successCallback;
        this.failureCallback = failureCallback;
    }

    /**
     * Create instance of this callback
     * @param successCallback the success callback
     * @param failureCallback the failure callback
     * @param  the result type
     * @return the async callback instance
     */
    public static  AsyncCallbacks create(Consumer successCallback, Consumer failureCallback) {
        return new AsyncCallbacks(successCallback, failureCallback);
    }

    /**
     * Invoke success callback with result
     * @param result the result
     */
    public void onSuccess(T result) {
        if (successCallback != null) {
            successCallback.accept(result);
        }
    }

    /**
     * Invoke failure callback with exception
     * @param ex the exception
     */
    public void onFailure(Throwable ex) {
        if (failureCallback != null) {
            failureCallback.accept(ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy