An open API service indexing awesome lists of open source software.

https://github.com/lukas-krecan/future-converter

Converts between various future types
https://github.com/lukas-krecan/future-converter

Last synced: 7 months ago
JSON representation

Converts between various future types

Awesome Lists containing this project

README

          

Future Converter [![Build Status](https://travis-ci.org/lukas-krecan/future-converter.png?branch=master)](https://travis-ci.org/lukas-krecan/future-converter) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/net.javacrumbs.future-converter/future-converter/badge.svg)](https://maven-badges.herokuapp.com/maven-central/net.javacrumbs.future-converter/future-converter)
================

Converts between various future types, [RxJava](https://github.com/Netflix/RxJava) Single, [RxJava 2](https://github.com/Netflix/RxJava) Single,
Spring 4 [ListenableFuture](http://docs.spring.io/spring/docs/4.0.0.BUILD-SNAPSHOT/javadoc-api/org/springframework/util/concurrent/ListenableFuture.html),
Java 8 [CompletableFuture](http://download.java.net/lambda/b88/docs/api/java/util/concurrent/CompletableFuture.html) and
Guava [ListenableFuture](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/ListenableFuture.html).

I am aware of the following quirks:
* It is not [possible to cancel CompletableFuture](http://stackoverflow.com/questions/23320407/how-to-cancel-java-8-completable-future) if it's blocked.

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

```xml

net.javacrumbs.future-converter
future-converter-FROM-TO
1.2.0

```
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](http://docs.spring.io/spring/docs/4.0.0.BUILD-SNAPSHOT/javadoc-api/org/springframework/util/concurrent/ListenableFuture.html) and Java 8 [CompletableFuture](http://download.java.net/lambda/b88/docs/api/java/util/concurrent/CompletableFuture.html)

Import the dependency

```xml

net.javacrumbs.future-converter
future-converter-spring-java8
1.2.0

```

And then use

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

...
CompletableFuture completable = toCompletableFuture(listenable);
...
ListenableFuture listenable = toListenableFuture(completable);
```

## spring-guava
Converts between Spring 4 [ListenableFuture](http://docs.spring.io/spring/docs/4.0.0.BUILD-SNAPSHOT/javadoc-api/org/springframework/util/concurrent/ListenableFuture.html)
and Guava [ListenableFuture](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/ListenableFuture.html)

Import the dependency

```xml

net.javacrumbs.future-converter
future-converter-spring-guava
1.2.0

```

And then use

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

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

## java8-guava
Converts between Java 8 [CompletableFuture](http://download.java.net/lambda/b88/docs/api/java/util/concurrent/CompletableFuture.html)
and Guava [ListenableFuture](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/ListenableFuture.html)

Import the dependency

```xml

net.javacrumbs.future-converter
future-converter-java8-guava
1.2.0

```

And then use

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

...
ListenableFuture guavaListenableFuture = toListenableFuture(completable);
...
CompletableFuture 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](https://github.com/Netflix/RxJava) Single and Java 8 [CompletableFuture](http://download.java.net/lambda/b88/docs/api/java/util/concurrent/CompletableFuture.html)

Import the dependency

```xml

net.javacrumbs.future-converter
future-converter-rxjava-java8
1.2.0

```

And then use

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

...
CompletableFuture completable = toCompletableFuture(single);
...
Single single = toSingle(completable);
```

## spring-rxjava
Converts between [RxJava](https://github.com/Netflix/RxJava) Observables and Spring 4 [ListenableFuture](http://docs.spring.io/spring/docs/4.0.0.BUILD-SNAPSHOT/javadoc-api/org/springframework/util/concurrent/ListenableFuture.html)

Import the dependency

```xml

net.javacrumbs.future-converter
future-converter-spring-rxjava
1.2.0

```

And then use

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

...
ListenableFuture listenable = toListenableFuture(single);
...
Single single = toSingle(listenable);
```

## guava-rxjava
Converts between [RxJava](https://github.com/Netflix/RxJava) Observables and Guava [ListenableFuture](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/ListenableFuture.html)

Import the dependency

```xml

net.javacrumbs.future-converter
future-converter-guava-rxjava
1.2.0

```

And then use

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

...
ListenableFuture listenable = toListenableFuture(single);
...
Single single = toSingle(listenable);
```