简体   繁体   English

Vert.x Java 列表<Futures>参数化

[英]Vert.x java List<Futures> parameterization

I ran in to a strange issue with Vert.x futures the other day that doesn't break the code but bothers me still.前几天我遇到了一个关于 Vert.x 期货的奇怪问题,它没有破坏代码,但仍然困扰着我。

Future without parameter results in the following warning:没有参数的 Future 会导致以下警告:

Future is a raw type. Future 是一个原始类型。 References to generic type Future should be parameterized对泛型类型 Future 的引用应该被参数化

Add the parameter, problem solved:添加参数,问题解决:

Future<YourClassName> future = ...

When dealing with a list of futures, you can also parameterize it just fine:在处理期货列表时,您也可以将其参数化就好了:

List<Future<YourClassName>> future = ...

But CompositeFuture.all() can't seem to deal with a parameterized list and forces you to remove the parameter.但是CompositeFuture.all()似乎无法处理参数化列表并强制您删除参数。

Is there any way to make parameterized list of futures work with CompositeFuture or do we just have to ignore that warning?有什么方法可以使参数化的期货列表与CompositeFuture还是我们只需忽略该警告? It doesn't break anything but would still be nice to find a solution to get rid of that warning.它不会破坏任何东西,但仍然很高兴找到摆脱该警告的解决方案。

On one hand, you can't use CompositeFuture.all() with list of parametrized futures.一方面,您不能将CompositeFuture.all()与参数化期货列表一起使用。 This is a design decision that the developers took, due to type erasure.由于类型擦除,这是开发人员做出的设计决定。
But actually, CompositeFuture.all() doesn't do anything special.但实际上, CompositeFuture.all()并没有做任何特别的事情。 So you may have your own interface with static method, that will do the same:所以你可能有自己的静态方法接口,它会做同样的事情:

interface MyCompositeFuture extends CompositeFuture {

    // This is what the regular does, just for example
    /*
    static CompositeFuture all(List<Future> futures) {
        return CompositeFutureImpl.all(futures.toArray(new Future[futures.size()]));
    }
    */

    static <T> CompositeFuture all(List<Future<T>> futures) {
        return CompositeFutureImpl.all(futures.toArray(new Future[futures.size()]));
    }
}

And now:现在:

    List<Future<String>> listFuturesT = new ArrayList<>();
    // This works
    MyCompositeFuture.all(listFuturesT);

    List<Future> listFutures = new ArrayList<>();
    // This doesnt, and that's the reason for initial design decision
    MyCompositeFuture.all(listFutures);

CompositeFuture.all() returns a CompositeFuture , which is itself a Future<CompositeFuture> . CompositeFuture.all()返回一个CompositeFuture ,它本身就是一个Future<CompositeFuture> So you can assign the result to a Future<CompositeFuture> instead of a raw Future :因此,您可以将结果分配给Future<CompositeFuture>而不是原始Future

Future<String> fut1 = asyncOp1();
Future<Integer> fut2 = asyncOp2();
Future<CompositeFuture> all = CompositeFuture.all(fut1, fut2);

[promoting a comment] [促进评论]

Suggestion from @ oh-god-spiders in the comments did help in my case. @ oh-god-spiders在评论中的建议对我的情况有所帮助。

val futures: util.List[Future[List[CustomClass]]] = asyncCode()
// Doesn’t work
val compositeFuture = CompositeFuture.all(futures)

// Works
val compositeFuture = CompositeFuture.all(new util.ArrayList[Future[_]](futures))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM