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

io.smallrye.reactive.converters.microprofile.PublisherBuilderConverter Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
package io.smallrye.reactive.converters.microprofile;

import java.util.Optional;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;

import org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder;
import org.eclipse.microprofile.reactive.streams.operators.ReactiveStreams;
import org.reactivestreams.Publisher;

import io.reactivex.processors.AsyncProcessor;
import io.smallrye.reactive.converters.ReactiveTypeConverter;

public class PublisherBuilderConverter implements ReactiveTypeConverter {
    @SuppressWarnings("unchecked")
    @Override
    public  CompletionStage toCompletionStage(PublisherBuilder instance) {
        return instance.findFirst().run().thenApplyAsync(x -> ((Optional) x).orElse(null));
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Publisher toRSPublisher(PublisherBuilder instance) {
        return instance.buildRs();
    }

    @Override
    public  PublisherBuilder fromCompletionStage(CompletionStage cs) {
        AsyncProcessor processor = AsyncProcessor.create();
        cs.whenComplete((X v, Throwable e) -> {
            if (e != null) {
                processor.onError(e instanceof CompletionException ? e.getCause() : e);
            } else if (v == null) {
                processor.onError(new NullPointerException());
            } else {
                processor.onNext(v);
                processor.onComplete();
            }
        });
        return ReactiveStreams.fromPublisher(processor);
    }

    @Override
    public  PublisherBuilder fromPublisher(Publisher publisher) {
        return ReactiveStreams.fromPublisher(publisher);
    }

    @Override
    public Class type() {
        return PublisherBuilder.class;
    }

    @Override
    public boolean emitItems() {
        return true;
    }

    @Override
    public boolean emitAtMostOneItem() {
        return false;
    }

    @Override
    public boolean supportNullValue() {
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy