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

cc.youchain.protocol.core.RemoteCall Maven / Gradle / Ivy

There is a newer version: 1.1.5
Show newest version
package cc.youchain.protocol.core;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;

import io.reactivex.Flowable;

import cc.youchain.utils.Async;

/**
 * A common type for wrapping remote requests.
 *
 * @param  Our return type.
 */
public class RemoteCall {

    private Callable callable;

    public RemoteCall(Callable callable) {
        this.callable = callable;
    }

    /**
     * Perform request synchronously.
     *
     * @return result of enclosed function
     * @throws Exception if the function throws an exception
     */
    public T send() throws Exception {
        return callable.call();
    }

    /**
     * Perform request asynchronously with a future.
     *
     * @return a future containing our function
     */
    public CompletableFuture sendAsync() {
        return Async.run(this::send);
    }

    /**
     * Provide an flowable to emit result from our function.
     *
     * @return an flowable
     */
    public Flowable flowable() {
        return Flowable.fromCallable(this::send);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy