Last active
July 12, 2016 02:10
-
-
Save cdimascio/4aad11c5d08c40a8897f to your computer and use it in GitHub Desktop.
CompletableFuture to F.Promise Adapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The following adapter is inspired by mattiwintou adapter from Rx Observable to F.Promise | |
https://gist.github.com/matiwinnetou/7b69253acaad996747c4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public F.Promise<Result> rankedPairs() { | |
CompletableFuture cf = ... | |
return RxFuture.toPromise(cf).map(Controller::ok); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dimascio.play; | |
import play.libs.F; | |
import java.util.concurrent.CompletableFuture; | |
public class RxFuture<T> { | |
private final CompletableFuture<T> cf; | |
public RxFuture(final CompletableFuture<T> cf) { | |
this.cf = cf; | |
} | |
public static <E> F.Promise<E> toPromise(final CompletableFuture<E> obs) { | |
return new RxFuture(obs).adopt(); | |
} | |
public F.Promise<T> adopt() { | |
F.RedeemablePromise<T> rPromise = F.RedeemablePromise.empty(); | |
cf.whenCompleteAsync((res, err) -> { | |
if (err != null) { | |
rPromise.failure(err); | |
} else { | |
rPromise.success(res); | |
} | |
}); | |
return rPromise; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment