Skip to content

Commit 0331945

Browse files
authored
Merge pull request #2 from graphql-java/1-better-generics
Better generics
2 parents ae5d3dc + 667ecec commit 0331945

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/main/java/org/dataloader/impl/PromisedValues.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface PromisedValues<T> {
3535
*
3636
* @return a new PromisedValues
3737
*/
38-
static <T> PromisedValues<T> allOf(List<CompletionStage<T>> cfs) {
38+
static <T> PromisedValues<T> allOf(List<? extends CompletionStage<T>> cfs) {
3939
return PromisedValuesImpl.combineAllOf(cfs);
4040
}
4141

@@ -68,7 +68,7 @@ static <T> PromisedValues<T> allOf(CompletionStage<T> f1, CompletionStage<T> f2)
6868
*
6969
* @return a new PromisedValues
7070
*/
71-
static <T> PromisedValues allOf(CompletionStage<T> f1, CompletionStage<T> f2, CompletionStage<T> f3) {
71+
static <T> PromisedValues<T> allOf(CompletionStage<T> f1, CompletionStage<T> f2, CompletionStage<T> f3) {
7272
return PromisedValuesImpl.combineAllOf(asList(f1, f2, f3));
7373
}
7474

@@ -87,7 +87,7 @@ static <T> PromisedValues allOf(CompletionStage<T> f1, CompletionStage<T> f2, Co
8787
*
8888
* @return a new PromisedValues
8989
*/
90-
static <T> PromisedValues allOf(CompletionStage<T> f1, CompletionStage<T> f2, CompletionStage<T> f3, CompletionStage<T> f4) {
90+
static <T> PromisedValues<T> allOf(CompletionStage<T> f1, CompletionStage<T> f2, CompletionStage<T> f3, CompletionStage<T> f4) {
9191
return PromisedValuesImpl.combineAllOf(asList(f1, f2, f3, f4));
9292
}
9393

src/main/java/org/dataloader/impl/PromisedValuesImpl.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
public class PromisedValuesImpl<T> implements PromisedValues<T> {
1717

18-
private final List<CompletionStage<T>> futures;
18+
private final List<? extends CompletionStage<T>> futures;
1919
private final CompletionStage<Void> controller;
2020
private AtomicReference<Throwable> cause;
2121

22-
private PromisedValuesImpl(List<CompletionStage<T>> cs) {
22+
private PromisedValuesImpl(List<? extends CompletionStage<T>> cs) {
2323
this.futures = nonNull(cs);
2424
this.cause = new AtomicReference<>();
2525
List<CompletableFuture> cfs = cs.stream().map(CompletionStage::toCompletableFuture).collect(Collectors.toList());
@@ -36,7 +36,7 @@ private PromisedValuesImpl(PromisedValuesImpl<T> other, CompletionStage<Void> co
3636
this.controller = controller;
3737
}
3838

39-
public static <T> PromisedValues<T> combineAllOf(List<CompletionStage<T>> cfs) {
39+
public static <T> PromisedValues<T> combineAllOf(List<? extends CompletionStage<T>> cfs) {
4040
return new PromisedValuesImpl<>(nonNull(cfs));
4141
}
4242

src/test/java/org/dataloader/impl/PromisedValuesImplTest.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import org.junit.Test;
44

5+
import java.util.Arrays;
56
import java.util.Collections;
67
import java.util.List;
78
import java.util.concurrent.CompletableFuture;
9+
import java.util.concurrent.CompletionStage;
810
import java.util.concurrent.atomic.AtomicBoolean;
911

1012
import static java.util.Arrays.asList;
@@ -13,6 +15,7 @@
1315
import static org.hamcrest.Matchers.equalTo;
1416
import static org.hamcrest.Matchers.instanceOf;
1517
import static org.hamcrest.Matchers.is;
18+
import static org.hamcrest.Matchers.notNullValue;
1619
import static org.hamcrest.Matchers.nullValue;
1720
import static org.junit.Assert.assertThat;
1821

@@ -178,5 +181,35 @@ public void exceptions_are_captured_and_reported() throws Exception {
178181

179182
assertThat(promisedValues.toList(), equalTo(asList(1, null)));
180183
assertThat(result, equalTo(asList(1, null)));
181-
}
184+
}
185+
186+
@Test
187+
public void type_generics_compile_as_expected() throws Exception {
188+
189+
PromisedValues<String> pvList = PromisedValues.allOf(Collections.singletonList(new CompletableFuture<String>()));
190+
PromisedValues<String> pvList2 = PromisedValues.allOf(Collections.<CompletionStage<String>>singletonList(new CompletableFuture<>()));
191+
192+
assertThat(pvList, notNullValue());
193+
assertThat(pvList2, notNullValue());
194+
195+
PromisedValues<String> pv2args = PromisedValues.allOf(new CompletableFuture<>(), new CompletableFuture<>());
196+
PromisedValues<String> pv3args = PromisedValues.allOf(new CompletableFuture<>(), new CompletableFuture<>(), new CompletableFuture<>());
197+
PromisedValues<String> pv4args = PromisedValues.allOf(new CompletableFuture<>(), new CompletableFuture<>(), new CompletableFuture<>(), new CompletableFuture<>());
198+
199+
assertThat(pv2args, notNullValue());
200+
assertThat(pv3args, notNullValue());
201+
assertThat(pv4args, notNullValue());
202+
203+
PromisedValues<String> pvListOfPVs = PromisedValues.allPromisedValues(Arrays.asList(pv2args, pv3args));
204+
205+
assertThat(pvListOfPVs, notNullValue());
206+
207+
PromisedValues<String> pv2ArgsOfPVs = PromisedValues.allPromisedValues(pv2args, pv3args);
208+
PromisedValues<String> pv3ArgsOfPVs = PromisedValues.allPromisedValues(pv2args, pv3args, pv4args);
209+
PromisedValues<String> pv4ArgsOfPVs = PromisedValues.allPromisedValues(pv2args, pv3args, pv4args, pv2args);
210+
211+
assertThat(pv2ArgsOfPVs, notNullValue());
212+
assertThat(pv3ArgsOfPVs, notNullValue());
213+
assertThat(pv4ArgsOfPVs, notNullValue());
214+
}
182215
}

0 commit comments

Comments
 (0)