io.proximax.utils.AsyncUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-chain-xipfs-sdk Show documentation
Show all versions of java-chain-xipfs-sdk Show documentation
Official ProximaX P2P Storage SDK Library in Java.
The newest version!
package io.proximax.utils;
import io.proximax.async.AsyncCallbacks;
import io.proximax.async.AsyncTask;
import io.reactivex.Observable;
import static io.proximax.utils.ParameterValidationUtils.checkParameter;
/**
* The utility class for handling async calls
*/
public class AsyncUtils {
private AsyncUtils() {
}
/**
* Observe for the first item on the Observable then invoke callbacks from AsyncCallbacks using result.
* Finally, update AsyncStatus to done.
* @param observable the observable
* @param asyncCallbacks the async callbacks
* @param asyncTask the async task that contains the state
* @param the result type
*/
public static void processFirstItem(Observable observable, AsyncCallbacks asyncCallbacks, AsyncTask asyncTask) {
checkParameter(observable != null, "observable is required");
checkParameter(asyncTask != null, "asyncTask is required");
observable.firstOrError()
.subscribe(
result -> {
if (asyncCallbacks != null) {
asyncCallbacks.onSuccess(result);
}
asyncTask.done();
}, throwable -> {
if (asyncCallbacks != null) {
asyncCallbacks.onFailure(throwable);
}
asyncTask.done();
});
}
}