maxday
maxday

Reputation: 1342

CompletionStage, CompletableFuture Void - what to return?

Here is my method :

public CompletionStage<Void> insert(List<HashAction> hashActionList) {
    if(!hashActionList.isEmpty()) {
        return ...
    }
    // what to return here ?
}

I have no idea of what to return if my list is empty. Not sure that null is good since I would have to check for null afterwards.

I tried

return CompletableFuture.completedFuture(null);

But I'm not really convinced since I chose randomly one implementation of CompletionStage

Upvotes: 4

Views: 8003

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40078

You can just return CompletableFuture<Void> by just having empty async run method

public CompletionStage<Void> insert(List<String> hashActionList) {
    if(!hashActionList.isEmpty()) {
        return null;
    }
    return CompletableFuture.runAsync(()->{});
}

OR you can use thenAccept to return CompletionStage<Void> and avoid null

return CompletableFuture.completedFuture(null).thenAccept(i->{});

Upvotes: 1

Related Questions