Java Futures

Bridge gaps and help overcome inconveniences with CompletableFuture. This module includes, among other features, methods to collect the results of multiple futures into a list, provides equivalent alternatives to CompletableFuture’s monadic methods (thenApply, thenCompose, etc.) that can deal with checked exceptions, and methods for asynchronous “try-with-resources” constructs.

License

License

Categories

Categories

Java Languages Net
GroupId

GroupId

net.florianschoppmann.java
ArtifactId

ArtifactId

java-futures
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Java Futures
Bridge gaps and help overcome inconveniences with CompletableFuture. This module includes, among other features, methods to collect the results of multiple futures into a list, provides equivalent alternatives to CompletableFuture’s monadic methods (thenApply, thenCompose, etc.) that can deal with checked exceptions, and methods for asynchronous “try-with-resources” constructs.
Project URL

Project URL

https://github.com/fschopp/java-futures
Source Code Management

Source Code Management

https://github.com/fschopp/java-futures

Download java-futures

How to add to project

<!-- https://jarcasting.com/artifacts/net.florianschoppmann.java/java-futures/ -->
<dependency>
    <groupId>net.florianschoppmann.java</groupId>
    <artifactId>java-futures</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/net.florianschoppmann.java/java-futures/
implementation 'net.florianschoppmann.java:java-futures:1.1.0'
// https://jarcasting.com/artifacts/net.florianschoppmann.java/java-futures/
implementation ("net.florianschoppmann.java:java-futures:1.1.0")
'net.florianschoppmann.java:java-futures:jar:1.1.0'
<dependency org="net.florianschoppmann.java" name="java-futures" rev="1.1.0">
  <artifact name="java-futures" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.florianschoppmann.java', module='java-futures', version='1.1.0')
)
libraryDependencies += "net.florianschoppmann.java" % "java-futures" % "1.1.0"
[net.florianschoppmann.java/java-futures "1.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.code.findbugs : jsr305 Optional jar 3.0.1

test (1)

Group / Artifact Type Version
org.testng : testng jar 6.9.10

Project Modules

There are no modules declared in this project.

Bridge gaps and help overcome inconveniences with CompletableFuture.

Status

Build Status Maven Central

Overview

  • requires Java 8
  • methods that collect the results of multiple completion stages into a List future, both with short-circuit semantics (fail early if an input completion stage fails) and without (guaranteed not to be completed while there are uncompleted input stages)
  • equivalent methods for supplyAsync, thenApply, thenCompose, etc. that accept functions throwing checked exceptions
  • method similar to Scala’s Promise#completeWith
  • method for unwrapping CompletionException
  • method for exception mapping
  • methods for dealing with asynchronous “try-with-resources” scenarios
  • tests have virtually full code coverage

License

Revised BSD (3-Clause) License

Binary Releases

Published releases (compiled for Java 8 and up) are available on Maven Central.

<dependency>
    <groupId>net.florianschoppmann.java</groupId>
    <artifactId>java-futures</artifactId>
    <version>1.1.0</version>
</dependency>

Documentation

For current snapshot:

Release documentation is available by replacing “snapshot” in the URL by the respective version number (such as “1.1.0”).

Usage Examples

The following examples show some use cases of class Futures provided by this project.

Collect the results of multiple completion stages into a List future

CompletableFuture<Integer> sum(Set<CompletionStage<Integer>> set) {
    return Futures.shortCircuitCollect(set)
        .thenApply(list -> list.stream().mapToInt(Integer::intValue).sum());
}

Equivalent methods allowing for checked exceptions

CompletableFuture<Long> fileSize(CompletionStage<Path> filePathStage,
        Executor executor) {
    // Note that Files#size(Path) throws an IOException, hence it would have to
    // be wrapped in the lambda expression passed to
    // CompletionStage#thenApply(Function).
    return Futures.thenApplyAsync(filePathStage, Files::size, executor);
}

Versions

Version
1.1.0
1.0.0