Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
64 lines (44 loc) · 2.06 KB
/
Copy pathMain.java
File metadata and controls
64 lines (44 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package modern.challenge;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
public class Main {
private static final Logger logger = Logger.getLogger(Main.class.getName());
@SuppressWarnings("unchecked")
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.setProperty("java.util.logging.SimpleFormatter.format",
"[%1$tT] [%4$-7s] %5$s %n");
logger.info("Exemplify thenCompose():");
CustomerAsyncs.fetchOrderTotal("Customer #1");
logger.info("Exemplify thenCombine():");
CustomerAsyncs.deliverParcel("#332");
logger.info("Exemplify allOf():");
List<String> invoices = Arrays.asList("#2334", "#122", "#55");
CompletableFuture<String>[] cfInvoices = invoices.stream()
.map(CustomerAsyncs::downloadInvoices)
.toArray(CompletableFuture[]::new);
CompletableFuture<Void> cfDownloaded = CompletableFuture.allOf(cfInvoices);
cfDownloaded.get();
logger.info("All invoices have been downloaded ...");
List<String> results = cfDownloaded.thenApply(e -> {
List<String> downloaded = new ArrayList<>();
for (CompletableFuture<String> cfInvoice : cfInvoices) {
downloaded.add(cfInvoice.join());
}
return downloaded;
}).get();
results.forEach(r -> logger.info(r));
logger.info("Done!\n");
logger.info("Exemplify anyOf():");
List<String> customers = Arrays.asList("#1", "#4", "#2", "#7", "#6", "#5");
CompletableFuture<String>[] cfCustomers = customers.stream()
.map(CustomerAsyncs::raffle)
.toArray(CompletableFuture[]::new);
CompletableFuture<Object> cfWinner = CompletableFuture.anyOf(cfCustomers);
Object winner = cfWinner.get();
logger.info(() -> "Winner: " + winner);
}
}