org.infinispan.commons.reactive.RxJavaInterop Maven / Gradle / Ivy
package org.infinispan.commons.reactive;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import org.infinispan.commons.util.Util;
import org.infinispan.commons.util.concurrent.CompletionStages;
import org.reactivestreams.Publisher;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.processors.AsyncProcessor;
import io.reactivex.rxjava3.processors.FlowableProcessor;
/**
* Static factory class that provides methods to obtain commonly used instances for interoperation between RxJava
* and standard JRE.
* @author wburns
* @since 10.0
*/
public class RxJavaInterop {
protected RxJavaInterop() { }
/**
* Provides a {@link Function} that can be used to convert from an instance of {@link java.util.Map.Entry} to
* the key of the entry. This is useful for the instance passed to a method like {@link Flowable#map(Function)}.
*
* @param key type
* @param value type
* @return rxjava function to convert from a Map.Entry to its key.
*/
public static Function, K> entryToKeyFunction() {
return (Function) entryToKeyFunction;
}
/**
* Provides a {@link Function} that can be used to convert from an instance of {@link java.util.Map.Entry} to
* the value of the entry. This is useful for the instance passed to a method like {@link Flowable#map(Function)}.
*
* @param key type
* @param value type
* @return rxjava function to convert from a Map.Entry to its value.
*/
public static Function, V> entryToValueFunction() {
return (Function) entryToValueFunction;
}
public static Function super Throwable, Publisher> cacheExceptionWrapper() {
return (Function) wrapThrowable;
}
public static Function identityFunction() {
return (Function) identityFunction;
}
public static Consumer emptyConsumer() {
return (Consumer) emptyConsumer;
}
/**
* Returns a {@link FlowableProcessor} that is already complete and will ignore any value submitted to it and will
* immediately cancel any subscriptions it receives.
* @return processor that is completed
* @param user value type
*/
public static FlowableProcessor completedFlowableProcessor() {
return (FlowableProcessor) completeFlowableProcessor;
}
/**
* Transforms a void completable future into a flowable.
*
* The flowable completes without emitting any value.
*
*
* @param stage: Completable future to transform.
* @return A Flowable that completes once the completable future completes.
* @param : A generic type for the flowable.
*/
public static Flowable voidCompletionStageToFlowable(CompletionStage> stage) {
if (CompletionStages.isCompletedSuccessfully(stage)) {
return Flowable.empty();
}
AsyncProcessor ap = AsyncProcessor.create();
stage.whenComplete((value, t) -> {
if (t != null) {
ap.onError(t);
} else {
ap.onComplete();
}
});
return ap;
}
private static final Function