Skip to content

Commit

Permalink
Merge pull request #2 from spotify/krka/readme
Browse files Browse the repository at this point in the history
Add rationale for FuturesExtra.syncTransform
  • Loading branch information
spkrka committed Feb 21, 2015
2 parents 91ab270 + ae0898f commit 0317e86
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ 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.

Ideally you would want to do something like this:
```java
public static <A, B> ListenableFuture<B> example(ListenableFuture<A> future) {
return Futures.transform(future, a -> toB(a));
}
```

This doesn't actually work though, because Futures.transform has two variants: one that takes a Function and one that takes an AsyncFunction.
Hence the compiler can't determine which variant to use without additional type information.

To work around that you have to cast 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

0 comments on commit 0317e86

Please sign in to comment.