package it.cavallium.dbengine.client; import java.util.Collection; import java.util.List; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class CountedStream { private final Flux stream; private final long count; public CountedStream(Flux stream, long count) { this.stream = stream; this.count = count; } public Flux getStream() { return stream; } public long getCount() { return count; } @SafeVarargs public static CountedStream merge(CountedStream... stream) { return merge(List.of(stream)); } public static CountedStream merge(Collection> stream) { return stream .stream() .reduce((a, b) -> new CountedStream<>(Flux.merge(a.getStream(), b.getStream()), a.getCount() + b.getCount())) .orElseGet(() -> new CountedStream<>(Flux.empty(), 0)); } public static Mono> merge(Flux> stream) { return stream .reduce((a, b) -> new CountedStream<>(Flux.merge(a.getStream(), b.getStream()), a.getCount() + b.getCount())) .switchIfEmpty(Mono.fromSupplier(() -> new CountedStream<>(Flux.empty(), 0))); } public Mono> collectList() { return stream.collectList(); } }