Skip to content

Commit c51785c

Browse files
Future combiners should also accept varargs (#5688)
Motivation: For the Future class, the following functions have been defined repeatedly with multiple definitions accepting multiple number of arguments. Future.all() Future.any() Future.join() For all the 3 methods, there are multiple definitions with multiple number of arguments.
1 parent 933083e commit c51785c

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

vertx-core/src/main/java/io/vertx/core/Future.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ static CompositeFuture all(Future<?> f1, Future<?> f2, Future<?> f3, Future<?> f
7575
return CompositeFutureImpl.all(f1, f2, f3, f4, f5, f6);
7676
}
7777

78+
/**
79+
* Like {@link #all(Future, Future)} but with a variable number of futures.
80+
*/
81+
static CompositeFuture all(Future<?> ... futures) {
82+
return CompositeFutureImpl.all(futures);
83+
}
84+
7885
/**
7986
* Like {@link #all(Future, Future)} but with a list of futures.<p>
8087
*
@@ -125,6 +132,15 @@ static CompositeFuture any(Future<?> f1, Future<?> f2, Future<?> f3, Future<?> f
125132
return CompositeFutureImpl.any(f1, f2, f3, f4, f5, f6);
126133
}
127134

135+
/**
136+
* Like {@link #any(Future, Future)} but with a variable number of futures.
137+
*
138+
* When the list is empty, the returned future will be already completed.
139+
*/
140+
static <T> CompositeFuture any(Future<?> ... futures) {
141+
return CompositeFutureImpl.any(futures);
142+
}
143+
128144
/**
129145
* Like {@link #any(Future, Future)} but with a list of futures.<p>
130146
*
@@ -175,6 +191,15 @@ static CompositeFuture join(Future<?> f1, Future<?> f2, Future<?> f3, Future<?>
175191
return CompositeFutureImpl.join(f1, f2, f3, f4, f5, f6);
176192
}
177193

194+
/**
195+
* Like {@link #join(Future, Future)} but with a variable number of futures.
196+
*
197+
* When the list is empty, the returned future will be already completed.
198+
*/
199+
static <T> CompositeFuture join(Future<?> ... futures) {
200+
return CompositeFutureImpl.join(futures);
201+
}
202+
178203
/**
179204
* Like {@link #join(Future, Future)} but with a list of futures.<p>
180205
*

vertx-core/src/test/java/io/vertx/tests/future/FutureTest.java

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Arrays;
2626
import java.util.Collections;
2727
import java.util.List;
28+
import java.util.Objects;
2829
import java.util.concurrent.*;
2930
import java.util.concurrent.atomic.AtomicBoolean;
3031
import java.util.concurrent.atomic.AtomicInteger;
@@ -36,6 +37,135 @@
3637
*/
3738
public class FutureTest extends FutureTestBase {
3839

40+
@Test
41+
public void testAllSucceededWithEightFutures() {
42+
testAllSucceeded(Future::all);
43+
}
44+
45+
private void testAllSucceeded(EightFunction<Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, CompositeFuture> all) {
46+
Promise<String> p1 = Promise.promise();
47+
Future<String> f1 = p1.future();
48+
Promise<String> p2 = Promise.promise();
49+
Future<String> f2 = p2.future();
50+
Promise<String> p3 = Promise.promise();
51+
Future<String> f3 = p3.future();
52+
Promise<String> p4 = Promise.promise();
53+
Future<String> f4 = p4.future();
54+
Promise<String> p5 = Promise.promise();
55+
Future<String> f5 = p5.future();
56+
Promise<String> p6 = Promise.promise();
57+
Future<String> f6 = p6.future();
58+
Promise<String> p7 = Promise.promise();
59+
Future<String> f7 = p7.future();
60+
Promise<String> p8 = Promise.promise();
61+
Future<String> f8 = p8.future();
62+
CompositeFuture composite = all.apply(f1, f2, f3, f4, f5, f6, f7, f8);
63+
Checker<CompositeFuture> checker = new Checker<>(composite);
64+
p1.complete("f1");
65+
p2.complete("f2");
66+
p3.complete("f3");
67+
p4.complete("f4");
68+
p5.complete("f5");
69+
p6.complete("f6");
70+
p7.complete("f7");
71+
p8.complete("f8");
72+
checker.assertSucceeded(composite);
73+
assertEquals("f1", composite.<String>resultAt(0));
74+
assertEquals("f2", composite.<String>resultAt(1));
75+
assertEquals("f3", composite.<String>resultAt(2));
76+
assertEquals("f4", composite.<String>resultAt(3));
77+
assertEquals("f5", composite.<String>resultAt(4));
78+
assertEquals("f6", composite.<String>resultAt(5));
79+
assertEquals("f7", composite.<String>resultAt(6));
80+
assertEquals("f8", composite.<String>resultAt(7));
81+
}
82+
83+
@Test
84+
public void testAnySucceededWithEightFutures() {
85+
testAnySucceeded(Future::any);
86+
}
87+
88+
private void testAnySucceeded(EightFunction<Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, CompositeFuture> any) {
89+
Promise<String> p1 = Promise.promise();
90+
Future<String> f1 = p1.future();
91+
Promise<String> p2 = Promise.promise();
92+
Future<String> f2 = p2.future();
93+
Promise<String> p3 = Promise.promise();
94+
Future<String> f3 = p3.future();
95+
Promise<String> p4 = Promise.promise();
96+
Future<String> f4 = p4.future();
97+
Promise<String> p5 = Promise.promise();
98+
Future<String> f5 = p5.future();
99+
Promise<String> p6 = Promise.promise();
100+
Future<String> f6 = p6.future();
101+
Promise<String> p7 = Promise.promise();
102+
Future<String> f7 = p7.future();
103+
Promise<String> p8 = Promise.promise();
104+
Future<String> f8 = p8.future();
105+
CompositeFuture composite = any.apply(f1, f2, f3, f4, f5, f6, f7, f8);
106+
Checker<CompositeFuture> checker = new Checker<>(composite);
107+
p1.complete("f1");
108+
p2.complete("f2");
109+
p3.complete("f3");
110+
p4.complete("f4");
111+
p5.complete("f5");
112+
p6.complete("f6");
113+
p7.complete("f7");
114+
p8.complete("f8");
115+
checker.assertSucceeded(composite);
116+
assertEquals("f1", composite.<String>resultAt(0));
117+
assertEquals("f2", composite.<String>resultAt(1));
118+
assertEquals("f3", composite.<String>resultAt(2));
119+
assertEquals("f4", composite.<String>resultAt(3));
120+
assertEquals("f5", composite.<String>resultAt(4));
121+
assertEquals("f6", composite.<String>resultAt(5));
122+
assertEquals("f7", composite.<String>resultAt(6));
123+
assertEquals("f8", composite.<String>resultAt(7));
124+
}
125+
126+
@Test
127+
public void testJoinSucceededWithEightFutures() {
128+
testJoinSucceeded(Future::join);
129+
}
130+
131+
private void testJoinSucceeded(EightFunction<Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, CompositeFuture> join) {
132+
Promise<String> p1 = Promise.promise();
133+
Future<String> f1 = p1.future();
134+
Promise<String> p2 = Promise.promise();
135+
Future<String> f2 = p2.future();
136+
Promise<String> p3 = Promise.promise();
137+
Future<String> f3 = p3.future();
138+
Promise<String> p4 = Promise.promise();
139+
Future<String> f4 = p4.future();
140+
Promise<String> p5 = Promise.promise();
141+
Future<String> f5 = p5.future();
142+
Promise<String> p6 = Promise.promise();
143+
Future<String> f6 = p6.future();
144+
Promise<String> p7 = Promise.promise();
145+
Future<String> f7 = p7.future();
146+
Promise<String> p8 = Promise.promise();
147+
Future<String> f8 = p8.future();
148+
CompositeFuture composite = join.apply(f1, f2, f3, f4, f5, f6, f7, f8);
149+
Checker<CompositeFuture> checker = new Checker<>(composite);
150+
p1.complete("f1");
151+
p2.complete("f2");
152+
p3.complete("f3");
153+
p4.complete("f4");
154+
p5.complete("f5");
155+
p6.complete("f6");
156+
p7.complete("f7");
157+
p8.complete("f8");
158+
checker.assertSucceeded(composite);
159+
assertEquals("f1", composite.<String>resultAt(0));
160+
assertEquals("f2", composite.<String>resultAt(1));
161+
assertEquals("f3", composite.<String>resultAt(2));
162+
assertEquals("f4", composite.<String>resultAt(3));
163+
assertEquals("f5", composite.<String>resultAt(4));
164+
assertEquals("f6", composite.<String>resultAt(5));
165+
assertEquals("f7", composite.<String>resultAt(6));
166+
assertEquals("f8", composite.<String>resultAt(7));
167+
}
168+
39169
@Test
40170
public void testCreateWithHandler() {
41171
AtomicInteger count = new AtomicInteger();
@@ -1927,4 +2057,16 @@ public void testOnCompleteParameterTypeIsContravariant(Future<String> fut, Compl
19272057
String cq = res;
19282058
});
19292059
}
2060+
2061+
@FunctionalInterface
2062+
private interface EightFunction<A, B, C, D, E, F, G, H, R> {
2063+
2064+
R apply(A a, B b, C c, D d, E e, F f, G g, H h);
2065+
2066+
default <V> EightFunction<A, B, C, D, E, F, G, H, V> andThen(
2067+
Function<? super R, ? extends V> after) {
2068+
Objects.requireNonNull(after);
2069+
return (a, b, c, d, e, f, g, h) -> after.apply(apply(a, b, c, d, e, f, g, h));
2070+
}
2071+
}
19302072
}

0 commit comments

Comments
 (0)