javaconcurrency

Filtering CompletableFutures


When working with CompletableFuture (as opposed to Project Rector's Mono), is there some alternative to Project Reactor's filter() method? That is, I want the future to be empty if a passed predicate evaluates to false

In other words, I want a prettier alternative to this:

    private void consumeResultIfPresent() {
        if (isConditionMet())
            getCompletableFuture().thenAccept(this::consumeResult);
    }
    private void consumeResultIfPresent_butPrettier() {
        getCompletableFuture().filter(v -> isConditionMet()).thenAccept(this::consumeResult);
    }

Sadly, we don't have Project Reactor in the classpath, so I have to resort to using Java's standard concurrent libraries (which are not that bad but seemingly underfeatured)

"Fat" consumers that do their own filtering are suboptimal

This is the only (remotely) similar question I found


Solution

  • As far as I know, there's no such thing as an empty CompletableFuture. Every CompletableFuture is associated with a possibly null value.

    If the condition is not dependent on the value and can be evaluated before the CompletableFuture is done, your first method is just fine.

    If the condition needs to be evalualated when the CompletableFuture is done, you'll need to use thenAccept, thenApply or thenRun. Using Optional so we can use lambda expressions instead of blocks:

    private void consumeResultIfPresent_butPrettier() {
        getCompletableFuture().thenAccept(value -> Optional.ofNullable(value)
                .filter(v -> isConditionMet())
                .ifPresent(this::consumeResult));
    }
    

    If you want to have an "empty" CompletableFuture the only thing I can think of is a CompetableFuture<Optional<T>> instead of a CompletableFuture<T>. You sill need thenAccept, but it can work on the possibly empty Optional.

    private void consumeResultIfPresent_butPrettier() {
        getCompletableFuture().thenAccept(o -> o
                .filter(v -> isConditionMet())
                .ifPresent(this::consumeResult));
    }