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

mutiny.zero.internal.StreamPublisher Maven / Gradle / Ivy

Go to download

Mutiny Zero is a minimal API for creating reactive-streams compliant publishers

There is a newer version: 1.1.1
Show newest version
package mutiny.zero.internal;

import static java.util.Objects.requireNonNull;

import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Publisher;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class StreamPublisher implements Publisher {

    private final Supplier> supplier;

    public StreamPublisher(Supplier> supplier) {
        this.supplier = supplier;
    }

    @Override
    public void subscribe(Flow.Subscriber subscriber) {
        requireNonNull(subscriber, "The subscriber cannot be null");
        Stream stream = supplier.get();
        if (stream == null) {
            subscriber.onSubscribe(new AlreadyCompletedSubscription());
            subscriber.onError(new NullPointerException("The supplied stream cannot be null"));
        } else {
            subscriber.onSubscribe(new IteratorSubscription<>(stream.iterator(), subscriber));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy