Abstract idea
I want to get the first value coming out of a set of Futures, that satisfies a given predicate.
If a satisfying value is found, all other Futures should be cancelled. If no value is found after all Futures have returned the execution should be terminated (by returning a default value or throwing an exception).
Concrete example
public boolean isThereVacantHotelRooms(Set<URL> hotelApiUrls) {
// returns true if any provided server responds with a number larger than 0
}
I'm looking for a pretty way of implementing the above in Java 8 (external libs are fine). I have tried implementing it with CompletableFuture as well as RxJava, but I both feel very non-idiomatic for this problem and I end up with lots of ugly code.
I think, your case can be accomplished with a combination of merge, filter and take: