如何使用CompletionStage的集合很好地完成allOf / AnyOf [英] How to nicely do allOf/AnyOf with Collections of CompletionStage

查看:132
本文介绍了如何使用CompletionStage的集合很好地完成allOf / AnyOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前使用Colletion of CompletionStage做一些简单的事情需要跳过几个丑陋的箍:

Currently to do something simple with Collections of CompletionStage requires jumping through several ugly hoops:

public static CompletionStage<String> translate(String foo) {
    // just example code to reproduce
    return CompletableFuture.completedFuture("translated " + foo);
}

public static CompletionStage<List<String>> translateAllAsync(List<String> input) {
    List<CompletableFuture<String>> tFutures = input.stream()
        .map(s -> translate(s)
            .toCompletableFuture())
        .collect(Collectors.toList()); // cannot use toArray because of generics Arrays creation :-(
    return CompletableFuture.allOf(tFutures.toArray(new CompletableFuture<?>[0])) // not using size() on purpose, see comments
        .thenApply(nil -> tFutures.stream()
            .map(f -> f.join())
            .map(s -> s.toUpperCase())
            .collect(Collectors.toList()));
}

我是什么想写的是:

public CompletionStage<List<String>> translateAllAsync(List<String> input) {
    // allOf takes a collection< futures<X>>, 
    // and returns a future<collection<x>> for thenApply()
    return XXXUtil.allOf(input.stream() 
            .map(s -> translate(s))
            .collect(Collectors.toList()))
        .thenApply(translations -> translations.stream()
            .map(s -> s.toUpperCase())
            .collect(Collectors.toList()));
}

关于toCompletableFuture并转换为数组和加入的整个仪式是模板分散注意力实际的代码语义。

The whole ceremony about toCompletableFuture and converting to an Array and join is boilerplate distracting from the actual code semantics.

可能有一个版本的allOf()返回 Future< Collection< Future< X>>> 而不是未来< Collection< X>> 在某些情况下也可能有用。

Possibly having a version of allOf() returning a Future<Collection<Future<X>>> instead of Future<Collection<X>> may also be useful in some cases.

我可以尝试自己实现XXXUtil,但我想知道是否已经有一个成熟的第三方库来解决这个问题和类似问题(例如Spotify的CompletableFutures)。如果是这样,我希望看到这样一个库的等效代码作为答案。

I could try implementing XXXUtil myself, but I wonder if there already is a mature 3rdparty library for this and similar issues (Such as Spotify's CompletableFutures). If so, I'd like to see the equivalent code for such a library as an answer.

或者上面发布的原始代码可能会以某种方式更紧凑地写在不同的方式?

Or maybe the original code posted above can somehow be written more compactly in a different way?

JUnit测试代码:

JUnit test code:

@Test
public void testTranslate() throws Exception {
    List<String> list = translateAllAsync(Arrays.asList("foo", "bar")).toCompletableFuture().get();
    Collections.sort(list);
    assertEquals(list,
        Arrays.asList("TRANSLATED BAR", "TRANSLATED FOO"));
}


推荐答案

我只是查看了源代码代码 CompletableFuture.allOf ,发现它基本上创建了一次处理两个阶段的节点的二叉树。我们可以轻松实现类似的逻辑,而无需明确使用 toCompletableFuture()并一次性处理结果列表生成:

I just looked into the source code of CompletableFuture.allOf, to find that it basically creates a binary tree of nodes handling two stages at a time. We can easily implement a similar logic without using toCompletableFuture() explicitly and handling the result list generation in one go:

public static <T> CompletionStage<List<T>> allOf(
                  Stream<? extends CompletionStage<? extends T>> source) {
    return allOf(source.collect(Collectors.toList()));
}
public static <T> CompletionStage<List<T>> allOf(
                  List<? extends CompletionStage<? extends T>> source) {
    int size = source.size();
    if(size == 0) return CompletableFuture.completedFuture(Collections.emptyList());
    List<T> result = new ArrayList<>(Collections.nCopies(size, null));
    return allOf(source, result, 0, size-1).thenApply(x -> result);
}
private static <T> CompletionStage<Void> allOf(
                   List<? extends CompletionStage<? extends T>> source,
                   List<T> result, int from, int to) {
    if(from < to) {
        int mid = (from+to)>>>1;
        return allOf(source, result, from, mid)
            .thenCombine(allOf(source, result, mid+1, to), (x,y)->x);
    }
    return source.get(from).thenAccept(t -> result.set(from, t));
}

就是这样。

您可以使用此解决方案将问题代码的逻辑实现为

You can use this solution to implement the logic of your question’s code as

public static CompletionStage<List<String>> translateAllAsync(List<String> input) {
    return allOf(input.stream().map(s -> translate(s)))
        .thenApply(list -> list.stream()
            .map(s -> s.toUpperCase())
            .collect(Collectors.toList()));
}

虽然使用

public static CompletionStage<List<String>> translateAllAsync(List<String> input) {
    return allOf(input.stream().map(s -> translate(s).thenApply(String::toUpperCase)));
}

请注意,此解决方案维护订单,因此无需对结果出现在测试用例中:

Note that this solution maintains the order, so there is no need for sorting the result in the test case:

@Test
public void testTranslate() throws Exception {
    List<String> list = translateAllAsync(Arrays.asList("foo", "bar")).toCompletableFuture().get();
    assertEquals(list, Arrays.asList("TRANSLATED FOO", "TRANSLATED BAR"));
}

这篇关于如何使用CompletionStage的集合很好地完成allOf / AnyOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆