|
| 1 | +package lambdasinaction.chap10; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.concurrent.*; |
| 5 | +import java.util.stream.*; |
| 6 | + |
| 7 | +public class BestPriceFinder { |
| 8 | + |
| 9 | + private final List<Shop> shops = Arrays.asList(new Shop("BestPrice"), |
| 10 | + new Shop("LetsSaveBig"), |
| 11 | + new Shop("MyFavoriteShop"), |
| 12 | + new Shop("BuyItAll"), |
| 13 | + new Shop("ShopEasy")); |
| 14 | + |
| 15 | + private final Executor executor = Executors.newFixedThreadPool(shops.size(), new ThreadFactory() { |
| 16 | + @Override |
| 17 | + public Thread newThread(Runnable r) { |
| 18 | + Thread t = new Thread(r); |
| 19 | + t.setDaemon(true); |
| 20 | + return t; |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | +/* |
| 25 | + public List<String> findPriceSequential(String product) { |
| 26 | + return shops.stream() |
| 27 | + .map(shop -> shop.getName() + " price is " + shop.calculatePrice(product)) |
| 28 | + .collect(Collectors.<String>toList()); |
| 29 | + } |
| 30 | +
|
| 31 | + public List<String> findPriceParallel(String product) { |
| 32 | + return shops.parallelStream() |
| 33 | + .map(shop -> shop.getName() + " price is " + shop.calculatePrice(product)) |
| 34 | + .collect(Collectors.<String>toList()); |
| 35 | + } |
| 36 | +
|
| 37 | + public List<String> findPrice(String product) { |
| 38 | + List<CompletableFuture<String>> priceFutures = |
| 39 | + shops.stream() |
| 40 | + .map(shop -> CompletableFuture.supplyAsync(() -> shop.getName() + " price is " + shop.calculatePrice(product))) |
| 41 | + .collect(Collectors.<CompletableFuture<String>>toList()); |
| 42 | +
|
| 43 | + List<String> prices = priceFutures.stream() |
| 44 | + .map(CompletableFuture::join) |
| 45 | + .collect(Collectors.<String>toList()); |
| 46 | + return prices; |
| 47 | + //return sequence(priceFutures).join(); |
| 48 | + } |
| 49 | +/*/ |
| 50 | + public List<String> findPriceSequential(String product) { |
| 51 | + return shops.stream() |
| 52 | + .map(shop -> shop.getPrice(product)) |
| 53 | + .map(Quote::parse) |
| 54 | + .map(Discount::applyDiscount) |
| 55 | + .collect(Collectors.<String>toList()); |
| 56 | + } |
| 57 | + |
| 58 | + public List<String> findPriceParallel(String product) { |
| 59 | + return shops.parallelStream() |
| 60 | + .map(shop -> shop.getPrice(product)) |
| 61 | + .map(Quote::parse) |
| 62 | + .map(Discount::applyDiscount) |
| 63 | + .collect(Collectors.<String>toList()); |
| 64 | + } |
| 65 | + |
| 66 | + public List<String> findPrice(String product) { |
| 67 | + List<CompletableFuture<String>> priceFutures = findPriceStream(product) |
| 68 | + .collect(Collectors.<CompletableFuture<String>>toList()); |
| 69 | + |
| 70 | + return priceFutures.stream() |
| 71 | + .map(CompletableFuture::join) |
| 72 | + .collect(Collectors.<String>toList()); |
| 73 | + } |
| 74 | + |
| 75 | + public Stream<CompletableFuture<String>> findPriceStream(String product) { |
| 76 | + return shops.stream() |
| 77 | + .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor)) |
| 78 | + .map(future -> future.thenApply(Quote::parse)) |
| 79 | + .map(future -> future.thenCompose(quote -> CompletableFuture.supplyAsync(() -> Discount.applyDiscount(quote), executor))); |
| 80 | + } |
| 81 | +} |
0 commit comments