Converts between various future types
Clone or download
Latest commit 652b845 Dec 21, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
apifuture-common [maven-release-plugin] prepare for next development iteration Dec 21, 2018
common-test [maven-release-plugin] prepare for next development iteration Dec 21, 2018
common [maven-release-plugin] prepare for next development iteration Dec 21, 2018
guava-common [maven-release-plugin] prepare for next development iteration Dec 21, 2018
guava-rxjava [maven-release-plugin] prepare for next development iteration Dec 21, 2018
guava-rxjava2 [maven-release-plugin] prepare for next development iteration Dec 21, 2018
java8-apifuture [maven-release-plugin] prepare for next development iteration Dec 21, 2018
java8-common [maven-release-plugin] prepare for next development iteration Dec 21, 2018
java8-guava [maven-release-plugin] prepare for next development iteration Dec 21, 2018
rxjava-common [maven-release-plugin] prepare for next development iteration Dec 21, 2018
rxjava-java8 [maven-release-plugin] prepare for next development iteration Dec 21, 2018
rxjava2-common [maven-release-plugin] prepare for next development iteration Dec 21, 2018
rxjava2-java8 [maven-release-plugin] prepare for next development iteration Dec 21, 2018
spring-common [maven-release-plugin] prepare for next development iteration Dec 21, 2018
spring-guava [maven-release-plugin] prepare for next development iteration Dec 21, 2018
spring-java8 [maven-release-plugin] prepare for next development iteration Dec 21, 2018
spring-rxjava [maven-release-plugin] prepare for next development iteration Dec 21, 2018
spring-rxjava2 [maven-release-plugin] prepare for next development iteration Dec 21, 2018
.gitignore Returning .idea folders to gitignore. Dec 17, 2018
.travis.yml README updated Dec 18, 2018
LICENSE.txt Release preparation Apr 10, 2014
README.md README updated Dec 21, 2018
RELEASENOTES.md README updated Dec 21, 2018
header-definition.xml License plugin is updated. Dec 17, 2018
header.txt Removing the name from headers. Dec 18, 2018
pom.xml [maven-release-plugin] prepare for next development iteration Dec 21, 2018

README.md

Future Converter Build Status Maven Central

Converts between various future types, RxJava Single, RxJava 2 Single, Spring 4 ListenableFuture, Java 8 CompletableFuture and Guava ListenableFuture.

I am aware of the following quirks:

The project has pretty good test coverage, but testing asynchronous stuff is tricky. If you find any bug, please let me know.

General usage

Import the dependency

<dependency>
    <groupId>net.javacrumbs.future-converter</groupId>
    <artifactId>future-converter-FROM-TO</artifactId>
    <version>1.2.0</version>
</dependency>

where you replace FROM and TO by library you want to use. For example future-converter-spring-guava. Then you just use static methods from net.javacrumbs.futureconverter.FROMTO.FutureConverter class.

spring-java8

Converts between Spring 4 ListenableFuture and Java 8 CompletableFuture

Import the dependency

<dependency>
    <groupId>net.javacrumbs.future-converter</groupId>
    <artifactId>future-converter-spring-java8</artifactId>
    <version>1.2.0</version>
</dependency>

And then use

import static net.javacrumbs.futureconverter.springjava.FutureConverter.*;

...
CompletableFuture<String> completable = toCompletableFuture(listenable);
...
ListenableFuture<String> listenable = toListenableFuture(completable);

spring-guava

Converts between Spring 4 ListenableFuture and Guava ListenableFuture

Import the dependency

<dependency>
    <groupId>net.javacrumbs.future-converter</groupId>
    <artifactId>future-converter-spring-guava</artifactId>
    <version>1.2.0</version>
</dependency>

And then use

import static net.javacrumbs.futureconverter.springguava.FutureConverter.*;

...
com.google.common.util.concurrent.ListenableFuture<String> guavaListenableFuture
        = toGuavaListenableFuture(springListenableFuture);
...
org.springframework.util.concurrent.ListenableFuture<String> springListenableFuture
        = toSpringListenableFuture(guavaListenableFuture);

java8-guava

Converts between Java 8 CompletableFuture and Guava ListenableFuture

Import the dependency

<dependency>
    <groupId>net.javacrumbs.future-converter</groupId>
    <artifactId>future-converter-java8-guava</artifactId>
    <version>1.2.0</version>
</dependency>

And then use

import static net.javacrumbs.futureconverter.java8guava.FutureConverter.*;

...
ListenableFuture<String> guavaListenableFuture = toListenableFuture(completable);
...
CompletableFuture<String> completable = toCompletableFuture(listenable);;

RxJava

Since version 1.1.0 we are using rx.Single for integration with RxJava

Please note that

  • When converting a Future to a Single, we cancel the original future on unsubscribe. If tou need the feature to continue running, use something like single.toObservable().publish().refCount().toSingle()
  • Converting Single to a Future registers exactly one subscription which is unsubscribed upon Future cancellation.

rxjava-java8

Converts between RxJava Single and Java 8 CompletableFuture

Import the dependency

<dependency>
    <groupId>net.javacrumbs.future-converter</groupId>
    <artifactId>future-converter-rxjava-java8</artifactId>
    <version>1.2.0</version>
</dependency>

And then use

import static net.javacrumbs.futureconverter.java8rx.FutureConverter.*;

...
CompletableFuture<String> completable = toCompletableFuture(single);
...
Single<String> single = toSingle(completable);

spring-rxjava

Converts between RxJava Observables and Spring 4 ListenableFuture

Import the dependency

<dependency>
    <groupId>net.javacrumbs.future-converter</groupId>
    <artifactId>future-converter-spring-rxjava</artifactId>
    <version>1.2.0</version>
</dependency>

And then use

import static net.javacrumbs.futureconverter.springrx.FutureConverter.*;

...
ListenableFuture<String> listenable = toListenableFuture(single);
...
Single<String> single = toSingle(listenable);

guava-rxjava

Converts between RxJava Observables and Guava ListenableFuture

Import the dependency

<dependency>
    <groupId>net.javacrumbs.future-converter</groupId>
    <artifactId>future-converter-guava-rxjava</artifactId>
    <version>1.2.0</version>
</dependency>

And then use

import static net.javacrumbs.futureconverter.guavarx.FutureConverter.*;

...
ListenableFuture<String> listenable = toListenableFuture(single);
...
Single<String> single = toSingle(listenable);