Skip to content

Commit

Permalink
Merge 87cbca3 into 54a064d
Browse files Browse the repository at this point in the history
  • Loading branch information
rydenius committed Mar 27, 2017
2 parents 54a064d + 87cbca3 commit f35b640
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
40 changes: 1 addition & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Guava's ListenableFuture class

### Runtime dependencies
* Java 6 or higher
* Guava 18.0 or higher
* Guava 20.0 or higher

### Usage

Expand All @@ -33,44 +33,6 @@ To import it with maven, use this:

### Examples

#### Cleaner transforms for Java 8.
Java 8 introduced lambdas which can greatly reduce verbosity in code, which is
great when using futures and transforms. One drawback with lambdas though is
that when a lambda is supplied as an argument to a method with overloaded
parameters, the compiler may fail to figure out which variant of a method call
that is intended to be used.

Ideally, applying java 8 lambdas to Guava's Futures.transform() would look
something like this:
```java
public static <A, B> ListenableFuture<B> example(ListenableFuture<A> future) {
return Futures.transform(future, a -> toB(a));
}
```

Unfortunately this doesn't actually work because Futures.transform has
two variants: one that takes a Function as its second parameter and one that
takes an AsyncFunction. The compiler can't determine which variant to use
without additional type information.

You could work around that by casting it like this:
```java
public static <A, B> ListenableFuture<B> example(ListenableFuture<A> future) {
return Futures.transform(future, (Function<A, B>) a -> toB(a));
}
```

With futures-extra you can do this instead:
```java
public static <A, B> ListenableFuture<B> example(ListenableFuture<A> future) {
return FuturesExtra.syncTransform(future, a -> toB(a));
}
```

This is just a simple delegating method that explicitly calls
Futures.transform(future, Function). There is also a corresponding
FuturesExtra.asyncTransform that calls Futures.transform(future, AsyncFunction).

#### Joining multiple futures

A common use case is waiting for two or more futures and then transforming the
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spotify</groupId>
<version>2.6.3-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<artifactId>futures-extra</artifactId>
<packaging>jar</packaging>

Expand Down Expand Up @@ -90,7 +90,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<version>20.0</version>
</dependency>

<!-- test dependencies -->
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/com/spotify/futures/FuturesExtra.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static <A, B> ListenableFuture<A> fastFail(
final ListenableFuture<B> conditionValue,
final ListenableFuture<A> future,
final Validator<B> validator) {
return Futures.transform(conditionValue, new AsyncFunction<B, A>() {
return Futures.transformAsync(conditionValue, new AsyncFunction<B, A>() {
@Override
public ListenableFuture<A> apply(B value) throws Exception {
try {
Expand Down Expand Up @@ -269,7 +269,7 @@ public interface Function2<Z, A, B> {
/**
* Transform the input futures into a single future, using the provided
* transform function. The transformation follows the same semantics as as
* {@link Futures#transform(ListenableFuture, AsyncFunction)} and the input
* {@link Futures#transformAsync(ListenableFuture, AsyncFunction)} and the input
* futures are combined using {@link Futures#allAsList}.
*
* @param a a ListenableFuture to combine
Expand Down Expand Up @@ -341,7 +341,7 @@ public interface Function3<Z, A, B, C> {
/**
* Transform the input futures into a single future, using the provided
* transform function. The transformation follows the same semantics as as
* {@link Futures#transform(ListenableFuture, AsyncFunction)} and the input
* {@link Futures#transformAsync(ListenableFuture, AsyncFunction)} and the input
* futures are combined using {@link Futures#allAsList}.
*
* @param a a ListenableFuture to combine
Expand Down Expand Up @@ -406,7 +406,7 @@ public interface Function4<Z, A, B, C, D> {
/**
* Transform the input futures into a single future, using the provided
* transform function. The transformation follows the same semantics as as
* {@link Futures#transform(ListenableFuture, AsyncFunction)} and the input
* {@link Futures#transformAsync(ListenableFuture, AsyncFunction)} and the input
* futures are combined using {@link Futures#allAsList}.
*
* @param a a ListenableFuture to combine
Expand Down Expand Up @@ -477,7 +477,7 @@ public interface Function5<Z, A, B, C, D, E> {
/**
* Transform the input futures into a single future, using the provided
* transform function. The transformation follows the same semantics as as
* {@link Futures#transform(ListenableFuture, AsyncFunction)} and the input
* {@link Futures#transformAsync(ListenableFuture, AsyncFunction)} and the input
* futures are combined using {@link Futures#allAsList}.
*
* @param a a ListenableFuture to combine
Expand Down Expand Up @@ -555,7 +555,7 @@ public interface Function6<Z, A, B, C, D, E, F> {
/**
* Transform the input futures into a single future, using the provided
* transform function. The transformation follows the same semantics as as
* {@link Futures#transform(ListenableFuture, AsyncFunction)} and the input
* {@link Futures#transformAsync(ListenableFuture, AsyncFunction)} and the input
* futures are combined using {@link Futures#allAsList}.
*
* @param a a ListenableFuture to combine
Expand Down Expand Up @@ -597,7 +597,7 @@ private static <Z> ListenableFuture<Z> transform(final List<? extends Listenable

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

/**
Expand Down Expand Up @@ -630,14 +630,22 @@ public static ListenableFuture<JoinedResults> join(ListenableFuture<?>... inputs
return Futures.transform(Futures.allAsList(list), new JoinedResults.Transform(list));
}

/**
* @deprecated Use {@link Futures#transform(ListenableFuture, Function)}
*/
@Deprecated
public static <I, O> ListenableFuture<O> syncTransform(
ListenableFuture<I> input, Function<? super I, ? extends O> function) {
return Futures.transform(input, function);
}

/**
* @deprecated Use {@link Futures#transformAsync(ListenableFuture, AsyncFunction)}
*/
@Deprecated
public static <I, O> ListenableFuture<O> asyncTransform(
ListenableFuture<I> input, AsyncFunction<? super I, ? extends O> function) {
return Futures.transform(input, function);
return Futures.transformAsync(input, function);
}

/**
Expand Down

0 comments on commit f35b640

Please sign in to comment.