Vert.x java <Futures> Parameter List

I ran into a strange Vert.x futures issue the other day that doesn't break code but bother me.

A future without a parameter results in the following warning:

The future is a raw type. References to a Generic Type The Future Must Be Parameterized

Add parameter, problem solved:

 Future<YourClassName> future = ... 

When you work with a list of futures, you can also easily parameterize it:

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

But CompositeFuture.all() does not seem to be relevant to the parameterized list and forces you to remove the parameter.

Is there a way to make a parameterized list of futures work with CompositeFuture or do we just need to ignore this warning? This will not break anything, but it would still be nice to find a solution to get rid of this warning.

+5
source share
2 answers

On the one hand, you cannot use CompositeFuture.all() with a list of parameterized futures. This is a design decision made by developers due to type erasure.
But actually CompositeFuture.all() does nothing special. So you can have your own interface with a 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 the reason for initial design decision MyCompositeFuture.all(listFutures); 
+2
source

CompositeFuture.all() returns a CompositeFuture , which itself is a Future<CompositeFuture> . Thus, you can assign the result of Future<CompositeFuture> instead of raw Future :

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

Source: https://habr.com/ru/post/1262299/


All Articles