Giter Site home page Giter Site logo

future-converter's Introduction

Future Converter

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

Please note that the conversion is not always straightforward. Especially RxObservables are completely different concept than Futures. Nevertheless, the conversion is more or less possible.

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.

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>0.3.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>0.3.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>0.3.0</version>
</dependency>

And then use

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

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

#RxJava Please note that conversion from/to RxJava Observables is not straightforward.

  • When converting Observable to a Future, only one element can be produced by the Observable. If your observable produces multiple values, please limit it using observable.take(1).
  • When converting a Future to an Observable, it's not clear what should happen upon unsubscribe. Since version 0.2.2 RxJava support does not cancel the Future, since there is no good place to keep track of the subscriptions (there may be multiple subscriptions for any given Future).
  • Converting Observable to a Future registers exactly one subscription which is unsubscribed upon Future cancellation.

rxjava-java8

Converts between RxJava Observables and Java 8 CompletableFuture

Import the dependency

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

And then use

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

...
CompletableFuture<String> completable = toCompletableFuture(observable);
...
Observable<String> observable = toObservable(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>0.3.0</version>
</dependency>

And then use

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

...
ListenableFuture<String> listenable = toListenableFuture(observable);
...
Observable<String> observable = toObservable(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>0.3.0</version>
</dependency>

And then use

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

...
ListenableFuture<String> listenable = toListenableFuture(observable);
...
Observable<String> observable = toObservable(listenable);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.