Skip to content

Better generics #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/org/dataloader/impl/PromisedValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface PromisedValues<T> {
*
* @return a new PromisedValues
*/
static <T> PromisedValues<T> allOf(List<CompletionStage<T>> cfs) {
static <T> PromisedValues<T> allOf(List<? extends CompletionStage<T>> cfs) {
return PromisedValuesImpl.combineAllOf(cfs);
}

Expand Down Expand Up @@ -68,7 +68,7 @@ static <T> PromisedValues<T> allOf(CompletionStage<T> f1, CompletionStage<T> f2)
*
* @return a new PromisedValues
*/
static <T> PromisedValues allOf(CompletionStage<T> f1, CompletionStage<T> f2, CompletionStage<T> f3) {
static <T> PromisedValues<T> allOf(CompletionStage<T> f1, CompletionStage<T> f2, CompletionStage<T> f3) {
return PromisedValuesImpl.combineAllOf(asList(f1, f2, f3));
}

Expand All @@ -87,7 +87,7 @@ static <T> PromisedValues allOf(CompletionStage<T> f1, CompletionStage<T> f2, Co
*
* @return a new PromisedValues
*/
static <T> PromisedValues allOf(CompletionStage<T> f1, CompletionStage<T> f2, CompletionStage<T> f3, CompletionStage<T> f4) {
static <T> PromisedValues<T> allOf(CompletionStage<T> f1, CompletionStage<T> f2, CompletionStage<T> f3, CompletionStage<T> f4) {
return PromisedValuesImpl.combineAllOf(asList(f1, f2, f3, f4));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/dataloader/impl/PromisedValuesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

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

private final List<CompletionStage<T>> futures;
private final List<? extends CompletionStage<T>> futures;
private final CompletionStage<Void> controller;
private AtomicReference<Throwable> cause;

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

public static <T> PromisedValues<T> combineAllOf(List<CompletionStage<T>> cfs) {
public static <T> PromisedValues<T> combineAllOf(List<? extends CompletionStage<T>> cfs) {
return new PromisedValuesImpl<>(nonNull(cfs));
}

Expand Down
35 changes: 34 additions & 1 deletion src/test/java/org/dataloader/impl/PromisedValuesImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Arrays.asList;
Expand All @@ -13,6 +15,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;

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

assertThat(promisedValues.toList(), equalTo(asList(1, null)));
assertThat(result, equalTo(asList(1, null)));
}
}

@Test
public void type_generics_compile_as_expected() throws Exception {

PromisedValues<String> pvList = PromisedValues.allOf(Collections.singletonList(new CompletableFuture<String>()));
PromisedValues<String> pvList2 = PromisedValues.allOf(Collections.<CompletionStage<String>>singletonList(new CompletableFuture<>()));

assertThat(pvList, notNullValue());
assertThat(pvList2, notNullValue());

PromisedValues<String> pv2args = PromisedValues.allOf(new CompletableFuture<>(), new CompletableFuture<>());
PromisedValues<String> pv3args = PromisedValues.allOf(new CompletableFuture<>(), new CompletableFuture<>(), new CompletableFuture<>());
PromisedValues<String> pv4args = PromisedValues.allOf(new CompletableFuture<>(), new CompletableFuture<>(), new CompletableFuture<>(), new CompletableFuture<>());

assertThat(pv2args, notNullValue());
assertThat(pv3args, notNullValue());
assertThat(pv4args, notNullValue());

PromisedValues<String> pvListOfPVs = PromisedValues.allPromisedValues(Arrays.asList(pv2args, pv3args));

assertThat(pvListOfPVs, notNullValue());

PromisedValues<String> pv2ArgsOfPVs = PromisedValues.allPromisedValues(pv2args, pv3args);
PromisedValues<String> pv3ArgsOfPVs = PromisedValues.allPromisedValues(pv2args, pv3args, pv4args);
PromisedValues<String> pv4ArgsOfPVs = PromisedValues.allPromisedValues(pv2args, pv3args, pv4args, pv2args);

assertThat(pv2ArgsOfPVs, notNullValue());
assertThat(pv3ArgsOfPVs, notNullValue());
assertThat(pv4ArgsOfPVs, notNullValue());
}
}