Set<Future>中满足<Object>谓词的第一个对象



>抽象概念

我想从一组满足给定谓词的期货中获取第一个值。

如果找到令人满意的价值,所有其他期货应被取消。如果在所有期货返回后未找到任何值,则应终止执行(通过返回默认值或抛出异常)。

具体示例

public boolean isThereVacantHotelRooms(Set<URL> hotelApiUrls) {
    // returns true if any provided server responds with a number larger than 0
}

我正在寻找一种在Java 8中实现上述内容的漂亮方法(外部库很好)。我尝试过用CompletableFuture和RxJava实现它,但我都觉得这个问题非常不习惯,我最终得到了很多丑陋的代码。

我认为

,您的情况可以通过合并,过滤和获取的组合来完成:

List<Observable<HotelInfo>> hotels = new ArrayList<>();
for (URL u : urls) {
    Observable<HotelInfo> hotelInfo = networkAPI.askHotel(u);
    hotels.add(hotelInfo);
}
Observable.merge(hotels)
.filter(h -> h.vacancy > 0)
.take(1)
.subscribe(h -> System.out.println("Winner: " + h), Throwable::printStackTrace);

由于您已经尝试了其他解决方案,因此这里是河口解决方案进行比较

// throw exception for failure
public void checkVacantHotelRooms(Set<URL> hotelApiUrls) throws Exception
{
    checkVacantHotelRoomsAsync(hotelApiUrls)  // Async<Void>
        .timeout(Duration.ofSeconds(10))      // cancel on 10s
        .sync();    //  async -> sync
}
public Async<Void> checkVacantHotelRoomsAsync(Set<URL> hotelApiUrls)
{
    Stream<Async<Void>> resultStream = hotelApiUrls.stream()    // Stream<URL>
        .map(this::getResponseBodyAsync)                        // Stream<Async<String>>
        .map(asyncBody->asyncBody.map(this::checkResponse));    // Stream<Async<Void>
    return AsyncBundle.anyOf(resultStream); 
    // succeeds if one result succeeds; others will be cancelled
}
Void checkResponse(String responseBody) throws Exception
{
    if(responseBody.contains("something"))
        return (Void)null;
    throw new Exception("none in this hotel");
}
-----
HttpClient httpClient = new HttpClient();
int maxBodyLength = 1000;
Async<String> getResponseBodyAsync(URL url)
{
    Async<HttpResponse> asyncResponse = httpClient.doGet(url.toExternalForm());
    return asyncResponse.then(resp->resp.bodyString(maxBodyLength));
}

最新更新