Skip to content

Commit e8148f0

Browse files
feature: align build to Java 11, add test suite, CI, and code-quality fixes
Co-Authored-By: Bobby Nobakht <bobby.nobakht@cognition.ai>
1 parent 4786c46 commit e8148f0

10 files changed

Lines changed: 149 additions & 56 deletions

File tree

.github/workflows/build.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Set up JDK 11
16+
uses: actions/setup-java@v4
17+
with:
18+
distribution: temurin
19+
java-version: '11'
20+
cache: maven
21+
22+
- name: Compile
23+
run: mvn -B compile
24+
25+
- name: Test
26+
run: mvn -B test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/target
22
/local
3+
dependency-reduced-pom.xml
34

45
# Eclipse, Netbeans and IntelliJ files
56
/.*
67
!.gitignore
8+
!.github
79
/nbproject
810
/*.ipr
911
/*.iws

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ The source code for all examples can be found in the directory [src/main/java/la
2929
* Appendix D: Lambdas and JVM bytecode
3030
We will update the repository as we update the book. Stay tuned!
3131

32-
### Make sure to have JDK8 installed
33-
The latest binary can be found here: http://www.oracle.com/technetwork/java/javase/overview/java8-2100321.html
32+
### Make sure to have JDK 11 (or newer) installed
33+
The 2nd-edition examples use APIs introduced after Java 8 (e.g. `Optional.or`, `Optional.stream`,
34+
`Collectors.flatMapping`/`filtering`, `Stream.takeWhile`), so the project targets Java 11
35+
(`maven.compiler.release=11` in `pom.xml`). A JDK 11 or later distribution is required to build.
36+
You can download a JDK from [Eclipse Temurin (Adoptium)](https://adoptium.net/) or
37+
[Oracle Java](https://www.oracle.com/java/technologies/downloads/).
3438

3539
$ java -version
3640

37-
java version "1.8.0_05"
38-
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
39-
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
40-
41-
42-
You can download a preview version here: https://jdk8.java.net/
41+
openjdk version "11.0.22" 2024-01-16
42+
OpenJDK Runtime Environment (build 11.0.22+7)
43+
OpenJDK 64-Bit Server VM (build 11.0.22+7, mixed mode)
4344

4445
### Compile/Run the examples
4546
Using maven:
@@ -55,5 +56,5 @@ Alternatively you can compile the files manually inside the directory src/main/j
5556

5657
You can also import the project in your favorite IDE:
5758
* In IntelliJ use "File->Open" menu and navigate to the folder where the project resides
58-
* In Eclipse use "File->Import->Existing Maven Projects" (also modify "Reduntant super interfaces" to report as Warnings instead of Errors
59+
* In Eclipse use "File->Import->Existing Maven Projects" (also modify "Redundant super interfaces" to report as Warnings instead of Errors
5960
* In Netbeans use "File->Open Project" menu

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,26 @@
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.release>11</maven.compiler.release>
14+
<jmh.version>1.37</jmh.version>
1315
</properties>
1416

1517
<dependencies>
1618
<dependency>
1719
<groupId>org.openjdk.jmh</groupId>
1820
<artifactId>jmh-core</artifactId>
19-
<version>1.17.4</version>
21+
<version>${jmh.version}</version>
2022
</dependency>
2123
<dependency>
2224
<groupId>org.openjdk.jmh</groupId>
2325
<artifactId>jmh-generator-annprocess</artifactId>
24-
<version>1.17.4</version>
26+
<version>${jmh.version}</version>
2527
</dependency>
2628
<dependency>
2729
<groupId>junit</groupId>
2830
<artifactId>junit</artifactId>
29-
<version>4.11</version>
31+
<version>4.13.2</version>
32+
<scope>test</scope>
3033
</dependency>
3134
</dependencies>
3235

@@ -35,15 +38,12 @@
3538
<plugin>
3639
<groupId>org.apache.maven.plugins</groupId>
3740
<artifactId>maven-compiler-plugin</artifactId>
38-
<version>3.1</version>
39-
<configuration>
40-
<source>1.9</source>
41-
<target>1.9</target>
42-
</configuration>
41+
<version>3.13.0</version>
4342
</plugin>
4443
<plugin>
4544
<groupId>org.apache.maven.plugins</groupId>
4645
<artifactId>maven-shade-plugin</artifactId>
46+
<version>3.5.3</version>
4747
<executions>
4848
<execution>
4949
<phase>package</phase>

src/main/java/lambdasinaction/chap1/FilteringApples.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ public static void main(String ... args){
99

1010
List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
1111
new Apple(155, "green"),
12-
new Apple(120, "red"));
12+
new Apple(120, "red"));
1313

1414
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
1515
List<Apple> greenApples = filterApples(inventory, FilteringApples::isGreenApple);
1616
System.out.println(greenApples);
17-
17+
1818
// [Apple{color='green', weight=155}]
1919
List<Apple> heavyApples = filterApples(inventory, FilteringApples::isHeavyApple);
2020
System.out.println(heavyApples);
21-
21+
2222
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
2323
List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
2424
System.out.println(greenApples2);
25-
25+
2626
// [Apple{color='green', weight=155}]
2727
List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150);
2828
System.out.println(heavyApples2);
29-
29+
3030
// []
31-
List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 ||
31+
List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 ||
3232
"brown".equals(a.getColor()));
3333
System.out.println(weirdApples);
3434
}
@@ -54,7 +54,7 @@ public static List<Apple> filterHeavyApples(List<Apple> inventory){
5454
}
5555

5656
public static boolean isGreenApple(Apple apple) {
57-
return "green".equals(apple.getColor());
57+
return "green".equals(apple.getColor());
5858
}
5959

6060
public static boolean isHeavyApple(Apple apple) {
@@ -69,7 +69,7 @@ public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p
6969
}
7070
}
7171
return result;
72-
}
72+
}
7373

7474
public static class Apple {
7575
private int weight = 0;

src/main/java/lambdasinaction/chap10/ReadPositiveIntParam.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
package lambdasinaction.chap10;
22

3-
import org.junit.*;
4-
53
import java.util.*;
64

75
import static java.util.Optional.*;
8-
import static org.junit.Assert.assertEquals;
96

107
public class ReadPositiveIntParam {
118

12-
@Test
13-
public void testMap() {
14-
Properties props = new Properties();
15-
props.setProperty("a", "5");
16-
props.setProperty("b", "true");
17-
props.setProperty("c", "-3");
18-
19-
assertEquals(5, readDurationImperative(props, "a"));
20-
assertEquals(0, readDurationImperative(props, "b"));
21-
assertEquals(0, readDurationImperative(props, "c"));
22-
assertEquals(0, readDurationImperative(props, "d"));
23-
24-
assertEquals(5, readDurationWithOptional(props, "a"));
25-
assertEquals(0, readDurationWithOptional(props, "b"));
26-
assertEquals(0, readDurationWithOptional(props, "c"));
27-
assertEquals(0, readDurationWithOptional(props, "d"));
28-
}
29-
309
public static int readDurationImperative(Properties props, String name) {
3110
String value = props.getProperty(name);
3211
if (value != null) {

src/main/java/lambdasinaction/chap11/Util.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,11 @@ public static void delay() {
2525

2626
public static double format(double number) {
2727
synchronized (formatter) {
28-
return new Double(formatter.format(number));
28+
return Double.parseDouble(formatter.format(number));
2929
}
3030
}
3131

3232
public static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures) {
33-
/*
34-
CompletableFuture<Void> allDoneFuture =
35-
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
36-
return allDoneFuture.thenApply(v ->
37-
futures.stream().
38-
map(future -> future.join()).
39-
collect(Collectors.<T>toList())
40-
);
41-
*/
4233
return CompletableFuture.supplyAsync(() -> futures.stream().
4334
map(future -> future.join()).
4435
collect(Collectors.<T>toList()));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package lambdasinaction.chap1;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import lambdasinaction.chap1.FilteringApples.Apple;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertTrue;
13+
14+
public class FilteringApplesTest {
15+
16+
private final List<Apple> inventory = Arrays.asList(
17+
new Apple(80, "green"),
18+
new Apple(155, "green"),
19+
new Apple(120, "red"));
20+
21+
@Test
22+
public void filterGreenApplesReturnsOnlyGreen() {
23+
List<Apple> green = FilteringApples.filterGreenApples(inventory);
24+
assertEquals(2, green.size());
25+
assertTrue(green.stream().allMatch(a -> "green".equals(a.getColor())));
26+
}
27+
28+
@Test
29+
public void filterHeavyApplesReturnsApplesOver150() {
30+
List<Apple> heavy = FilteringApples.filterHeavyApples(inventory);
31+
assertEquals(1, heavy.size());
32+
assertEquals(Integer.valueOf(155), heavy.get(0).getWeight());
33+
}
34+
35+
@Test
36+
public void filterApplesWithPredicate() {
37+
List<Apple> redApples = FilteringApples.filterApples(inventory, a -> "red".equals(a.getColor()));
38+
assertEquals(1, redApples.size());
39+
assertEquals("red", redApples.get(0).getColor());
40+
}
41+
42+
@Test
43+
public void isGreenAppleAndIsHeavyApple() {
44+
assertTrue(FilteringApples.isGreenApple(new Apple(50, "green")));
45+
assertFalse(FilteringApples.isGreenApple(new Apple(50, "red")));
46+
assertTrue(FilteringApples.isHeavyApple(new Apple(200, "red")));
47+
assertFalse(FilteringApples.isHeavyApple(new Apple(100, "red")));
48+
}
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package lambdasinaction.chap10;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Properties;
6+
7+
import static lambdasinaction.chap10.ReadPositiveIntParam.readDurationImperative;
8+
import static lambdasinaction.chap10.ReadPositiveIntParam.readDurationWithOptional;
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class ReadPositiveIntParamTest {
12+
13+
@Test
14+
public void testMap() {
15+
Properties props = new Properties();
16+
props.setProperty("a", "5");
17+
props.setProperty("b", "true");
18+
props.setProperty("c", "-3");
19+
20+
assertEquals(5, readDurationImperative(props, "a"));
21+
assertEquals(0, readDurationImperative(props, "b"));
22+
assertEquals(0, readDurationImperative(props, "c"));
23+
assertEquals(0, readDurationImperative(props, "d"));
24+
25+
assertEquals(5, readDurationWithOptional(props, "a"));
26+
assertEquals(0, readDurationWithOptional(props, "b"));
27+
assertEquals(0, readDurationWithOptional(props, "c"));
28+
assertEquals(0, readDurationWithOptional(props, "d"));
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package lambdasinaction.chap11;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class UtilTest {
8+
9+
@Test
10+
public void formatRoundsToTwoDecimals() {
11+
assertEquals(3.14, Util.format(3.14159), 0.0001);
12+
assertEquals(5.0, Util.format(5.0), 0.0001);
13+
assertEquals(2.5, Util.format(2.5), 0.0001);
14+
}
15+
}

0 commit comments

Comments
 (0)