• This repository has been archived on 02/Nov/2023
  • Stars
    star
    154
  • Rank 242,095 (Top 5 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Converts between various future types

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);