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