package com.insightfullogic.java8.answers.chapter9; import com.insightfullogic.java8.examples.chapter1.Artist; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; public class CompletableFutureArtistAnalyser implements ArtistAnalyzer { private final Function<String, Artist> artistLookupService; public CompletableFutureArtistAnalyser(Function<String, Artist> artistLookupService) { this.artistLookupService = artistLookupService; } public void isLargerGroup(String artistName, String otherArtistName, Consumer<Boolean> handler) { CompletableFuture<Long> otherArtistMemberCount = CompletableFuture.supplyAsync(() -> getNumberOfMembers(otherArtistName)); CompletableFuture<Long> artistMemberCount = CompletableFuture.completedFuture(getNumberOfMembers(artistName)); artistMemberCount.thenCombine(otherArtistMemberCount, (count, otherCount) -> count > otherCount) .thenAccept(handler::accept); } private long getNumberOfMembers(String artistName) { return artistLookupService.apply(artistName) .getMembers() .count(); } }