In my quarkus service, i am building a custom accumulator to consume a Multi
. The stream can potentially be infinite, and I wonder how to early terminate and collect the results when sufficient data has been accumulated?
Below are my prototype:
Multi<Data> sortedStream = getStream();
return this.sortedStream.collectItems().in(
LinkedList::new,
new BiConsumer<LinkedList<Coverage>, Data>() {
@Override
public void accept(LinkedList<Coverage> coverages, Data incoming) {
if (coverages.isEmpty()) {
coverages.add(new Coverage(incoming));
return;
}
if (enough(coverages)) {
// Question: How to early terminate and collect coverage downstream?
}
Coverage last = coverages.getLast();
if (worthAdd(last, incoming)) {
coverages.add(new Coverage(incoming));
} else {
return;
}
}
}
)