Skip to content

Commit b040637

Browse files
committed
Technical proofing by Jean-Francois Morin
1 parent 7325163 commit b040637

File tree

15 files changed

+236
-135
lines changed

15 files changed

+236
-135
lines changed

src/main/java/lambdasinaction/chap10/AsyncShop.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package lambdasinaction.chap10;
22

3-
import java.text.*;
4-
import java.util.*;
5-
import java.util.concurrent.*;
3+
import static lambdasinaction.chap10.Util.delay;
4+
import static lambdasinaction.chap10.Util.format;
65

7-
import static lambdasinaction.chap10.Util.*;
6+
import java.util.Random;
7+
import java.util.concurrent.CompletableFuture;
8+
import java.util.concurrent.Future;
89

910
public class AsyncShop {
1011

src/main/java/lambdasinaction/chap10/AsyncShopClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package lambdasinaction.chap10;
22

3-
import java.util.concurrent.*;
3+
import java.util.concurrent.Future;
44

55
public class AsyncShopClient {
66

src/main/java/lambdasinaction/chap10/BestPriceFinder.java

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package lambdasinaction.chap10;
22

3-
import java.util.*;
4-
import java.util.concurrent.*;
5-
import java.util.stream.*;
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.Executor;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.ThreadFactory;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.Stream;
611

712
public class BestPriceFinder {
813

@@ -20,68 +25,42 @@ public Thread newThread(Runnable r) {
2025
return t;
2126
}
2227
});
23-
/*
24-
public List<String> findPriceSequential(String product) {
25-
return shops.stream()
26-
.map(shop -> shop.getName() + " price is " + shop.calculatePrice(product))
27-
.collect(Collectors.toList());
28-
}
29-
30-
public List<String> findPriceParallel(String product) {
31-
return shops.parallelStream()
32-
.map(shop -> shop.getName() + " price is " + shop.calculatePrice(product))
33-
.collect(Collectors.toList());
34-
}
35-
36-
public List<String> findPrice(String product) {
37-
List<CompletableFuture<String>> priceFutures =
38-
shops.stream()
39-
.map(shop -> CompletableFuture.supplyAsync(() -> shop.getName() + " price is "
40-
+ shop.calculatePrice(product), executor))
41-
.collect(Collectors.toList());
4228

43-
List<String> prices = priceFutures.stream()
44-
.map(CompletableFuture::join)
45-
.collect(Collectors.toList());
46-
return prices;
47-
//return sequence(priceFutures).join();
48-
}
49-
/*/
50-
public List<String> findPriceSequential(String product) {
29+
public List<String> findPricesSequential(String product) {
5130
return shops.stream()
5231
.map(shop -> shop.getPrice(product))
5332
.map(Quote::parse)
5433
.map(Discount::applyDiscount)
5534
.collect(Collectors.toList());
5635
}
5736

58-
public List<String> findPriceParallel(String product) {
37+
public List<String> findPricesParallel(String product) {
5938
return shops.parallelStream()
6039
.map(shop -> shop.getPrice(product))
6140
.map(Quote::parse)
6241
.map(Discount::applyDiscount)
6342
.collect(Collectors.toList());
6443
}
6544

66-
public List<String> findPrice(String product) {
67-
List<CompletableFuture<String>> priceFutures = findPriceStream(product)
45+
public List<String> findPricesFuture(String product) {
46+
List<CompletableFuture<String>> priceFutures = findPricesStream(product)
6847
.collect(Collectors.<CompletableFuture<String>>toList());
6948

7049
return priceFutures.stream()
7150
.map(CompletableFuture::join)
7251
.collect(Collectors.toList());
7352
}
7453

75-
public Stream<CompletableFuture<String>> findPriceStream(String product) {
54+
public Stream<CompletableFuture<String>> findPricesStream(String product) {
7655
return shops.stream()
7756
.map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor))
7857
.map(future -> future.thenApply(Quote::parse))
7958
.map(future -> future.thenCompose(quote -> CompletableFuture.supplyAsync(() -> Discount.applyDiscount(quote), executor)));
8059
}
8160

82-
public void printPricesStream() {
61+
public void printPricesStream(String product) {
8362
long start = System.nanoTime();
84-
CompletableFuture[] futures = findPriceStream("myPhone")
63+
CompletableFuture[] futures = findPricesStream(product)
8564
.map(f -> f.thenAccept(s -> System.out.println(s + " (done in " + ((System.nanoTime() - start) / 1_000_000) + " msecs)")))
8665
.toArray(size -> new CompletableFuture[size]);
8766
CompletableFuture.allOf(futures).join();
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package lambdasinaction.chap10;
22

3-
import java.util.*;
4-
import java.util.concurrent.*;
5-
import java.util.function.*;
6-
7-
import static java.util.stream.Collectors.toList;
3+
import java.util.List;
4+
import java.util.function.Supplier;
85

96
public class BestPriceFinderMain {
107

118
private static BestPriceFinder bestPriceFinder = new BestPriceFinder();
129

1310
public static void main(String[] args) {
14-
//execute("sequential", () -> bestPriceFinder.findPriceSequential("myPhone"));
15-
//execute("parallel", () -> bestPriceFinder.findPriceParallel("myPhone"));
16-
execute("composed CompletableFuture", () -> bestPriceFinder.findPrice("myPhone"));
17-
//bestPriceFinder.printPricesStream();
11+
execute("sequential", () -> bestPriceFinder.findPricesSequential("myPhone27S"));
12+
execute("parallel", () -> bestPriceFinder.findPricesParallel("myPhone27S"));
13+
execute("composed CompletableFuture", () -> bestPriceFinder.findPricesFuture("myPhone27S"));
14+
bestPriceFinder.printPricesStream("myPhone27S");
1815
}
1916

2017
private static void execute(String msg, Supplier<List<String>> s) {
@@ -23,4 +20,5 @@ private static void execute(String msg, Supplier<List<String>> s) {
2320
long duration = (System.nanoTime() - start) / 1_000_000;
2421
System.out.println(msg + " done in " + duration + " msecs");
2522
}
23+
2624
}

src/main/java/lambdasinaction/chap10/Discount.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package lambdasinaction.chap10;
22

3-
import java.util.*;
4-
5-
import static lambdasinaction.chap10.Util.*;
3+
import static lambdasinaction.chap10.Util.delay;
4+
import static lambdasinaction.chap10.Util.format;
65

76
public class Discount {
87

src/main/java/lambdasinaction/chap10/Shop.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package lambdasinaction.chap10;
22

3-
import java.util.*;
3+
import static lambdasinaction.chap10.Util.delay;
4+
import static lambdasinaction.chap10.Util.format;
45

5-
import static lambdasinaction.chap10.Util.*;
6+
import java.util.Random;
67

78
public class Shop {
89

src/main/java/lambdasinaction/chap10/Util.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package lambdasinaction.chap10;
22

3-
import java.text.*;
4-
import java.util.*;
5-
import java.util.concurrent.*;
6-
import java.util.stream.*;
3+
import java.text.DecimalFormat;
4+
import java.text.DecimalFormatSymbols;
5+
import java.util.List;
6+
import java.util.Locale;
7+
import java.util.Random;
8+
import java.util.concurrent.CompletableFuture;
9+
import java.util.stream.Collectors;
710

811
public class Util {
912

src/main/java/lambdasinaction/chap11/DateTimeExamples.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package lambdasinaction.chap11;
22

3-
import java.text.*;
4-
import java.time.*;
5-
import java.time.chrono.*;
6-
import java.time.format.*;
7-
import java.time.temporal.*;
8-
import java.util.*;
9-
10-
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
11-
import static java.time.temporal.ChronoUnit.DAYS;
12-
import static java.time.temporal.TemporalAdjusters.*;
3+
import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
4+
import static java.time.temporal.TemporalAdjusters.nextOrSame;
5+
6+
import java.text.DateFormat;
7+
import java.text.SimpleDateFormat;
8+
import java.time.DayOfWeek;
9+
import java.time.Duration;
10+
import java.time.Instant;
11+
import java.time.LocalDate;
12+
import java.time.LocalDateTime;
13+
import java.time.LocalTime;
14+
import java.time.Month;
15+
import java.time.chrono.JapaneseDate;
16+
import java.time.format.DateTimeFormatter;
17+
import java.time.format.DateTimeFormatterBuilder;
18+
import java.time.temporal.ChronoField;
19+
import java.time.temporal.ChronoUnit;
20+
import java.time.temporal.Temporal;
21+
import java.time.temporal.TemporalAdjuster;
22+
import java.util.Calendar;
23+
import java.util.Date;
24+
import java.util.Locale;
1325

1426
public class DateTimeExamples {
1527

@@ -20,9 +32,9 @@ protected DateFormat initialValue() {
2032
};
2133

2234
public static void main(String[] args) {
23-
//useOldDate();
35+
useOldDate();
2436
useLocalDate();
25-
//useTemporalAdjuster();
37+
useTemporalAdjuster();
2638
useDateFormatter();
2739
}
2840

@@ -33,7 +45,7 @@ private static void useOldDate() {
3345
System.out.println(formatters.get().format(date));
3446

3547
Calendar calendar = Calendar.getInstance();
36-
calendar.set(2014, 2, 18);
48+
calendar.set(2014, Calendar.FEBRUARY, 18);
3749
System.out.println(calendar);
3850
}
3951

@@ -101,22 +113,22 @@ private static void useTemporalAdjuster() {
101113
date = date.with(nextOrSame(DayOfWeek.FRIDAY));
102114
System.out.println(date);
103115
date = date.with(temporal -> {
104-
int dow = temporal.get(DAY_OF_WEEK);
116+
DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
105117
int dayToAdd = 1;
106-
if (dow == 5) dayToAdd = 3;
107-
if (dow == 6) dayToAdd = 2;
108-
return temporal.plus(dayToAdd, DAYS);
118+
if (dow == DayOfWeek.FRIDAY) dayToAdd = 3;
119+
if (dow == DayOfWeek.SATURDAY) dayToAdd = 2;
120+
return temporal.plus(dayToAdd, ChronoUnit.DAYS);
109121
});
110122
System.out.println(date);
111123
}
112124

113125
private static class NextWorkingDay implements TemporalAdjuster {
114126
@Override
115127
public Temporal adjustInto(Temporal temporal) {
116-
int dow = temporal.get(ChronoField.DAY_OF_WEEK);
128+
DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
117129
int dayToAdd = 1;
118-
if (dow == 5) dayToAdd = 3;
119-
if (dow == 6) dayToAdd = 2;
130+
if (dow == DayOfWeek.FRIDAY) dayToAdd = 3;
131+
if (dow == DayOfWeek.SATURDAY) dayToAdd = 2;
120132
return temporal.plus(dayToAdd, ChronoUnit.DAYS);
121133
}
122134
}

src/main/java/lambdasinaction/chap12/SubsetsMain.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
import java.util.Collections;
66
import java.util.List;
77

8-
98
public class SubsetsMain {
109

1110
public static void main(String[] args) {
1211
List<List<Integer>> subs = subsets(Arrays.asList(1, 4, 9));
13-
1412
subs.forEach(System.out::println);
15-
1613
}
1714

18-
1915
public static List<List<Integer>> subsets(List<Integer> l) {
2016
if (l.isEmpty()) {
2117
List<List<Integer>> ans = new ArrayList<>();
@@ -30,9 +26,11 @@ public static List<List<Integer>> subsets(List<Integer> l) {
3026
}
3127

3228
public static List<List<Integer>> insertAll(Integer first, List<List<Integer>> lists) {
33-
List<List<Integer>> result = new ArrayList<>(); for (List<Integer> l : lists) {
34-
List<Integer> copyList = new ArrayList<>(l);
29+
List<List<Integer>> result = new ArrayList<>();
30+
for (List<Integer> l : lists) {
31+
List<Integer> copyList = new ArrayList<>();
3532
copyList.add(first);
33+
copyList.addAll(l);
3634
result.add(copyList);
3735
}
3836
return result;
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package lambdasinaction.chap13;
22

3-
43
import java.util.function.Function;
54

65
public class Combinators {
76

87
public static void main(String[] args) {
9-
System.out.println(repeat(3, (Integer x)->2*x).apply(10));
8+
System.out.println(repeat(3, (Integer x) -> 2 * x).apply(10));
109
}
1110

12-
static <A,B,C> Function<A,C> compose(Function<B,C> g, Function<A,B> f) {
11+
static <A, B, C> Function<A, C> compose(Function<B, C> g, Function<A, B> f) {
1312
return x -> g.apply(f.apply(x));
1413
}
1514

16-
static <A> Function<A,A> repeat(int n, Function<A,A> f) {
17-
return n==0?x->x : compose(f, (Function<A,A>) repeat(n-1, f));
15+
static <A> Function<A, A> repeat(int n, Function<A, A> f) {
16+
return n == 0 ? x -> x : compose(f, repeat(n - 1, f));
1817
}
1918
}

0 commit comments

Comments
 (0)