Skip to content

Instantly share code, notes, and snippets.

@adamw
Created January 21, 2020 12:02
Show Gist options
  • Save adamw/12ba42a53a0532395e3cac4e68a17d30 to your computer and use it in GitHub Desktop.
Save adamw/12ba42a53a0532395e3cac4e68a17d30 to your computer and use it in GitHub Desktop.
import java.util.concurrent.CompletableFuture;
class Example1 {
private final Database database;
Example1(Database database) {
this.database = database;
}
CompletableFuture<Boolean> activateUser(Long userId) {
return database.findUser(userId).thenCompose((u) -> {
if (u != null && !u.isActive()) {
return database.activateUser(userId).thenApply((r) -> true);
} else {
return CompletableFuture.completedFuture(false);
}
});
}
}
interface Database {
CompletableFuture<User> findUser(Long id);
CompletableFuture<Void> activateUser(Long id);
}
interface User {
boolean isActive();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment