| 8 | import onjava.Timer; |
| 9 | |
| 10 | public class CompletablePizza { |
| 11 | static final int QUANTITY = 5; |
| 12 | public static CompletableFuture<Pizza> |
| 13 | makeCF(Pizza za) { |
| 14 | return CompletableFuture |
| 15 | .completedFuture(za) |
| 16 | .thenApplyAsync(Pizza::roll) |
| 17 | .thenApplyAsync(Pizza::sauce) |
| 18 | .thenApplyAsync(Pizza::cheese) |
| 19 | .thenApplyAsync(Pizza::toppings) |
| 20 | .thenApplyAsync(Pizza::bake) |
| 21 | .thenApplyAsync(Pizza::slice) |
| 22 | .thenApplyAsync(Pizza::box); |
| 23 | } |
| 24 | public static void |
| 25 | show(CompletableFuture<Pizza> cf) { |
| 26 | try { |
| 27 | System.out.println(cf.get()); |
| 28 | } catch(Exception e) { |
| 29 | throw new RuntimeException(e); |
| 30 | } |
| 31 | } |
| 32 | public static void main(String[] args) { |
| 33 | Timer timer = new Timer(); |
| 34 | List<CompletableFuture<Pizza>> pizzas = |
| 35 | IntStream.range(0, QUANTITY) |
| 36 | .mapToObj(Pizza::new) |
| 37 | .map(CompletablePizza::makeCF) |
| 38 | .collect(Collectors.toList()); |
| 39 | System.out.println(timer.duration()); |
| 40 | pizzas.forEach(CompletablePizza::show); |
| 41 | System.out.println(timer.duration()); |
| 42 | } |
| 43 | } |
| 44 | /* Output: |
| 45 | 98 |
| 46 | Pizza 1: ROLLED |
nothing calls this directly
no outgoing calls
no test coverage detected