Skip to content

Commit

Permalink
Fix some generics usages
Browse files Browse the repository at this point in the history
  • Loading branch information
spkrka committed Dec 10, 2015
1 parent d95b3f5 commit 3589783
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 64 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/spotify/futures/FuturesExtra.java
Expand Up @@ -130,7 +130,7 @@ public ListenableFuture<A> apply(B value) throws Exception {
* @return a new future with the result of the first completing future.
* @throws NullPointerException if the {@param futures} is null
*/
public static <T> ListenableFuture<T> select(final List<ListenableFuture<T>> futures) {
public static <T> ListenableFuture<T> select(final List<? extends ListenableFuture<T>> futures) {
Preconditions.checkNotNull(futures);
if (futures.isEmpty()) {
return Futures.immediateFailedFuture(new NoSuchElementException("List is empty"));
Expand Down Expand Up @@ -590,12 +590,12 @@ public interface AsyncFunction6<Z, A, B, C, D, E, F> {
ListenableFuture<Z> apply(A a, B b, C c, D d, E e, F f) throws Exception;
}

private static <Z> ListenableFuture<Z> transform(final List<ListenableFuture<?>> inputs,
private static <Z> ListenableFuture<Z> transform(final List<? extends ListenableFuture<?>> inputs,
final Function<List<Object>, Z> function) {
return Futures.transform(Futures.allAsList(inputs), function);
}

private static <Z> ListenableFuture<Z> transform(final List<ListenableFuture<?>> inputs,
private static <Z> ListenableFuture<Z> transform(final List<? extends ListenableFuture<?>> inputs,
final AsyncFunction<List<Object>, Z> function) {
return Futures.transform(Futures.allAsList(inputs), function);
}
Expand All @@ -606,7 +606,7 @@ private static <Z> ListenableFuture<Z> transform(final List<ListenableFuture<?>>
* the join is accessed.</p>
* @see #join(ListenableFuture...)
*/
public static <T> ListenableFuture<JoinedResults> join(List<ListenableFuture<T>> inputs) {
public static ListenableFuture<JoinedResults> join(List<? extends ListenableFuture<?>> inputs) {
return Futures.transform(Futures.allAsList(inputs), new JoinedResults.Transform(inputs));
}

Expand All @@ -626,7 +626,7 @@ public static <T> ListenableFuture<JoinedResults> join(List<ListenableFuture<T>>
* </pre>
*/
public static ListenableFuture<JoinedResults> join(ListenableFuture<?>... inputs) {
List<ListenableFuture<?>> list = Arrays.asList(inputs);
List<? extends ListenableFuture<?>> list = Arrays.asList(inputs);
return Futures.transform(Futures.allAsList(list), new JoinedResults.Transform(list));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/spotify/futures/JoinedResults.java
Expand Up @@ -53,9 +53,9 @@ public <T> T get(ListenableFuture<T> future) {
return t;
}

static class Transform<T> implements Function<List<Object>, JoinedResults> {
private final List<ListenableFuture<T>> futures;
public Transform(List<ListenableFuture<T>> list) {
static class Transform implements Function<List<Object>, JoinedResults> {
private final List<? extends ListenableFuture<?>> futures;
public Transform(List<? extends ListenableFuture<?>> list) {
futures = ImmutableList.copyOf(list);
}

Expand Down
Expand Up @@ -39,7 +39,8 @@ public class CompletableFuturesExtraTest {
@Rule
public ExpectedException exception = ExpectedException.none();

@Mock FutureCallback<String> callback;
@Mock
FutureCallback<String> callback;

private final SettableFuture<String> settable = SettableFuture.create();
private final ListenableFuture<String> listenable = settable;
Expand Down Expand Up @@ -98,7 +99,6 @@ public void testToListenableFutureUnwrap() {
}



@Test
public void testToListenableFutureSuccess() throws ExecutionException, InterruptedException {
final CompletableFuture<String> completable = new CompletableFuture<String>();
Expand Down Expand Up @@ -183,12 +183,14 @@ public void testDereferenceSuccess() throws Exception {
public void testExceptionallyCompose() throws Exception {
final CompletionStage<String> future = CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalArgumentException());

final CompletionStage<String> composed = CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
return CompletableFuture.completedFuture("hello");
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
return CompletableFuture.completedFuture("hello");
}
});

assertEquals("hello", CompletableFuturesExtra.getCompleted(composed));

Expand All @@ -198,12 +200,14 @@ public CompletionStage<String> apply(Throwable throwable) {
public void testExceptionallyComposeFailure() throws Exception {
final CompletionStage<String> future = CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalArgumentException());

final CompletionStage<String> composed = CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
return CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalStateException());
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
return CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalStateException());
}
});
CompletableFuturesExtra.getCompleted(composed);
fail();
}
Expand All @@ -212,25 +216,29 @@ public CompletionStage<String> apply(Throwable throwable) {
public void testExceptionallyComposeUnused() throws Exception {
final CompletionStage<String> future = CompletableFuture.completedFuture("hello");

final CompletionStage<String> composed = CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
return CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalStateException());
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
return CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalStateException());
}
});
assertEquals("hello", CompletableFuturesExtra.getCompleted(composed));
}

@Test(expected = IllegalStateException.class)
public void testExceptionallyComposeThrows() throws Exception {
final CompletionStage<String> future = CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalArgumentException());

final CompletionStage<String> composed = CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
throw new IllegalStateException();
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
throw new IllegalStateException();
}
});
CompletableFuturesExtra.getCompleted(composed);
fail();
}
Expand All @@ -239,12 +247,14 @@ public CompletionStage<String> apply(Throwable throwable) {
public void testExceptionallyComposeReturnsNull() throws Exception {
final CompletionStage<String> future = CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalArgumentException());

final CompletionStage<String> composed = CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
return null;
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.exceptionallyCompose(future, new Function<Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(Throwable throwable) {
return null;
}
});
CompletableFuturesExtra.getCompleted(composed);
fail();
}
Expand All @@ -253,12 +263,14 @@ public CompletionStage<String> apply(Throwable throwable) {
public void testHandleCompose() throws Exception {
final CompletionStage<String> future = CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalArgumentException());

final CompletionStage<String> composed = CompletableFuturesExtra.handleCompose(future, new BiFunction<String, Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(String s, Throwable throwable) {
return CompletableFuture.completedFuture("hello");
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.handleCompose(future, new BiFunction<String, Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(String s, Throwable throwable) {
return CompletableFuture.completedFuture("hello");
}
});

assertEquals("hello", CompletableFuturesExtra.getCompleted(composed));

Expand All @@ -268,12 +280,14 @@ public CompletionStage<String> apply(String s, Throwable throwable) {
public void testHandleComposeFailure() throws Exception {
final CompletionStage<String> future = CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalArgumentException());

final CompletionStage<String> composed = CompletableFuturesExtra.handleCompose(future, new BiFunction<String, Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(String s, Throwable throwable) {
return CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalStateException());
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.handleCompose(future, new BiFunction<String, Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(String s, Throwable throwable) {
return CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalStateException());
}
});
CompletableFuturesExtra.getCompleted(composed);
fail();
}
Expand All @@ -282,12 +296,14 @@ public CompletionStage<String> apply(String s, Throwable throwable) {
public void testHandleComposeThrows() throws Exception {
final CompletionStage<String> future = CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalArgumentException());

final CompletionStage<String> composed = CompletableFuturesExtra.handleCompose(future, new BiFunction<String, Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(String s, Throwable throwable) {
throw new IllegalStateException();
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.handleCompose(future, new BiFunction<String, Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(String s, Throwable throwable) {
throw new IllegalStateException();
}
});
CompletableFuturesExtra.getCompleted(composed);
fail();
}
Expand All @@ -296,12 +312,14 @@ public CompletionStage<String> apply(String s, Throwable throwable) {
public void testHandleComposeReturnsNull() throws Exception {
final CompletionStage<String> future = CompletableFuturesExtra.exceptionallyCompletedFuture(new IllegalArgumentException());

final CompletionStage<String> composed = CompletableFuturesExtra.handleCompose(future, new BiFunction<String, Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(String s, Throwable throwable) {
return null;
}
});
final CompletionStage<String>
composed =
CompletableFuturesExtra.handleCompose(future, new BiFunction<String, Throwable, CompletionStage<String>>() {
@Override
public CompletionStage<String> apply(String s, Throwable throwable) {
return null;
}
});
CompletableFuturesExtra.getCompleted(composed);
fail();
}
Expand Down

0 comments on commit 3589783

Please sign in to comment.