
mutiny.zero.PublisherHelpers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mutiny-zero Show documentation
Show all versions of mutiny-zero Show documentation
Mutiny Zero is a minimal API for creating reactive-streams compliant publishers
The newest version!
package mutiny.zero;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.Flow.Subscriber;
public interface PublisherHelpers {
/**
* Collect all items as a list.
*
* @param publisher the publisher
* @param the emitted type
* @return the future accumulating the items into a list.
*/
static CompletionStage> collectToList(Publisher publisher) {
List list = new ArrayList<>();
CompletableFuture> future = new CompletableFuture<>();
publisher.subscribe(new Subscriber<>() {
@Override
public void onSubscribe(Flow.Subscription subscription) {
subscription.request(Long.MAX_VALUE);
}
@Override
public void onNext(T t) {
list.add(t);
}
@Override
public void onError(Throwable throwable) {
future.completeExceptionally(throwable);
}
@Override
public void onComplete() {
future.complete(list);
}
});
return future;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy