Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Future without parameter results in the following warning:

Future is a raw type. References to generic type Future should be parameterized

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.

Is there any way to make parameterized list of futures work with CompositeFuture or do we just have to ignore that warning? It doesn't break anything but would still be nice to find a solution to get rid of that warning.

like image 918
Rauno Avatar asked Jan 04 '17 15:01

Rauno


1 Answers

On one hand, you can't use CompositeFuture.all() with list of parametrized futures. This is a design decision that the developers took, due to type erasure.
But actually, CompositeFuture.all() doesn't do anything special. 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);
like image 143
Alexey Soshin Avatar answered Sep 29 '22 19:09

Alexey Soshin