com.yelstream.topp.furnace.reactive.integration.IterablePublisher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of topp-furnace-reactive-integration Show documentation
Show all versions of topp-furnace-reactive-integration Show documentation
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 super T> 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;
}
});
}
}