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

com.yelstream.topp.furnace.reactive.integration.IterablePublisher Maven / Gradle / Ivy

Go to download

Topp Furnace Reactive Integration provides interactions between selected reactive frameworks.

The newest version!
package com.yelstream.topp.furnace.reactive.integration;

import java.util.Iterator;
import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;

public class IterablePublisher implements Publisher {

    private final Iterable iterable;

    public IterablePublisher(Iterable iterable) {
        this.iterable = iterable;
    }

    @Override
    public void subscribe(Subscriber subscriber) {
        Iterator iterator = iterable.iterator();
        subscriber.onSubscribe(new Subscription() {
            private boolean completed = false;

            @Override
            public void request(long n) {
                if (n <= 0) {
                    subscriber.onError(new IllegalArgumentException("Demand must be positive"));
                    return;
                }
                for (long i = 0; i < n && iterator.hasNext(); i++) {
                    subscriber.onNext(iterator.next());
                }
                if (!iterator.hasNext() && !completed) {
                    completed = true;
                    subscriber.onComplete();
                }
            }

            @Override
            public void cancel() {
                completed = true;
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy