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

com.firefly.reactive.adapter.common.ReactiveUtils Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.reactive.adapter.common;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.concurrent.CompletableFuture;

/**
 * @author Pengtao Qiu
 */
abstract public class ReactiveUtils {
    public static  CompletableFuture toFuture(Publisher publisher) {
        CompletableFuture future = new CompletableFuture<>();
        publisher.subscribe(new Subscriber() {
            @Override
            public void onSubscribe(Subscription s) {
                s.request(1);
            }

            @Override
            public void onNext(T t) {
                future.complete(t);
            }

            @Override
            public void onError(Throwable t) {
                future.completeExceptionally(t);
            }

            @Override
            public void onComplete() {
                if (!future.isDone()) {
                    future.complete(null);
                }
            }
        });
        return future;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy