
io.vertx.rxjava3.FlowableHelper Maven / Gradle / Ivy
package io.vertx.rxjava3;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.type.TypeReference;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableTransformer;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.streams.ReadStream;
import io.vertx.rxjava3.impl.FlowableReadStream;
import io.vertx.rxjava3.impl.ReadStreamSubscriber;
import io.vertx.rxjava3.impl.FlowableUnmarshaller;
import java.util.function.Function;
/**
* @author Julien Viet
*/
public class FlowableHelper {
/**
* Adapts an RxJava {@link Flowable} to a Vert.x {@link io.vertx.core.streams.ReadStream}. The returned
* readstream will be subscribed to the {@link Flowable}.
*
* @param observable the observable to adapt
* @return the adapted stream
*/
public static ReadStream toReadStream(Flowable observable) {
return ReadStreamSubscriber.asReadStream(observable, Function.identity());
}
/**
* Like {@link #toFlowable(ReadStream)} but with a {@code mapping} function
*/
public static Flowable toFlowable(ReadStream stream, Function mapping) {
return RxJavaPlugins.onAssembly(new FlowableReadStream<>(stream, FlowableReadStream.DEFAULT_MAX_BUFFER_SIZE, mapping));
}
/**
* Adapts a Vert.x {@link ReadStream} to an RxJava {@link Flowable}. After
* the stream is adapted to a flowable, the original stream handlers should not be used anymore
* as they will be used by the flowable adapter.
*
* @param stream the stream to adapt
* @return the adapted observable
*/
public static Flowable toFlowable(ReadStream stream) {
return RxJavaPlugins.onAssembly(new FlowableReadStream<>(stream, FlowableReadStream.DEFAULT_MAX_BUFFER_SIZE, Function.identity()));
}
/**
* Adapts a Vert.x {@link ReadStream} to an RxJava {@link Flowable}. After
* the stream is adapted to a flowable, the original stream handlers should not be used anymore
* as they will be used by the flowable adapter.
*
* @param stream the stream to adapt
* @return the adapted observable
*/
public static Flowable toFlowable(ReadStream stream, long maxBufferSize) {
return RxJavaPlugins.onAssembly(new FlowableReadStream<>(stream, maxBufferSize, Function.identity()));
}
public static FlowableTransformer unmarshaller(Class mappedType) {
return new FlowableUnmarshaller<>(java.util.function.Function.identity(), mappedType);
}
public static FlowableTransformer unmarshaller(TypeReference mappedTypeRef) {
return new FlowableUnmarshaller<>(java.util.function.Function.identity(), mappedTypeRef);
}
public static FlowableTransformer unmarshaller(Class mappedType, ObjectCodec mapper) {
return new FlowableUnmarshaller<>(java.util.function.Function.identity(), mappedType, mapper);
}
public static FlowableTransformerunmarshaller(TypeReference mappedTypeRef, ObjectCodec mapper) {
return new FlowableUnmarshaller<>(java.util.function.Function.identity(), mappedTypeRef, mapper);
}
}