2016-08-10 31 views
6

我有这个奇怪的类型CompletableFuture<CompletableFuture<byte[]>>,但我想CompletableFuture<byte[]>。这可能吗?什么是CompletableFuture相当于flatMap的功能?

public Future<byte[]> convert(byte[] htmlBytes) { 
    PhantomPdfMessage htmlMessage = new PhantomPdfMessage(); 
    htmlMessage.setId(UUID.randomUUID()); 
    htmlMessage.setTimestamp(new Date()); 
    htmlMessage.setEncodedContent(Base64.getEncoder().encodeToString(htmlBytes)); 

    CompletableFuture<CompletableFuture<byte[]>> thenApply = CompletableFuture.supplyAsync(this::getPhantom, threadPool).thenApply(
     worker -> worker.convert(htmlMessage).thenApply(
      pdfMessage -> Base64.getDecoder().decode(pdfMessage.getEncodedContent()) 
     ) 
    ); 

} 

回答

8

有它的文档在bug,但CompletableFuture#thenCompose家庭的方法是flatMap的等价物。它的声明也应该给你一些线索

public <U> CompletableFuture<U> thenCompose(Function<? super T,? extends CompletionStage<U>> fn) 

thenCompose需要接收CompletableFuture的结果(称之为),并把它传递给Function您提供的,它必须返回自己的CompletableFuture(称之为)。所述CompletableFuture(称之为)由thenCompose返回将被当完成完成。

在您的例子

CompletableFuture<Worker> one = CompletableFuture.supplyAsync(this::getPhantom, threadPool); 
CompletableFuture<PdfMessage /* whatever */> two = one.thenCompose(worker -> worker.convert(htmlMessage)); 
CompletableFuture<byte[]> result = two.thenApply(pdfMessage -> Base64.getDecoder().decode(pdfMessage.getEncodedContent()));