Skip to content

Commit 0400dd6

Browse files
committed
add chapter11
1 parent ea0624f commit 0400dd6

File tree

8 files changed

+244
-1
lines changed

8 files changed

+244
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ This repository contains the code examples from the book [Modern Java in Action]
1616
- Chapter 8. Collection API enhancements | [java](https://github.com/codejsha/modern-java-in-action/tree/main/src/main/java/com/example/demo/chapter08)([test](https://github.com/codejsha/modern-java-in-action/tree/main/src/main/test/com/example/demo/chapter08))
1717
- Chapter 9. Refactoring, testing, and debugging | [java](https://github.com/codejsha/modern-java-in-action/tree/main/src/main/java/com/example/demo/chapter09)([test](https://github.com/codejsha/modern-java-in-action/tree/main/src/main/test/com/example/demo/chapter09))
1818
- Chapter 10. Domain-specific languages using lambdas | [java](https://github.com/codejsha/modern-java-in-action/tree/main/src/main/java/com/example/demo/chapter10)([test](https://github.com/codejsha/modern-java-in-action/tree/main/src/main/test/com/example/demo/chapter10))
19+
- Chapter 11. Using Optional as a better alternative to null | [java](https://github.com/codejsha/modern-java-in-action/tree/main/src/main/java/com/example/demo/chapter11)([test](https://github.com/codejsha/modern-java-in-action/tree/main/src/main/test/com/example/demo/chapter11))
1920

2021
(work in progress)
2122

22-
- Chapter 11. Using Optional as a better alternative to null
2323
- Chapter 12. New Date and Time API
2424
- Chapter 13. Default methods
2525
- Chapter 14. The Java Module System
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.demo.chapter11;
2+
3+
import java.util.Optional;
4+
5+
public record Car(Optional<Insurance> insurance) {
6+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.example.demo.chapter11;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
import java.util.Set;
8+
import java.util.stream.Collectors;
9+
10+
@Slf4j
11+
public class CarInsuranceProcessing {
12+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
13+
public static String getCarInsuranceName(Optional<Person> person) {
14+
return person.flatMap(Person::car)
15+
.flatMap(Car::insurance)
16+
.map(Insurance::name)
17+
.orElse("Unknown");
18+
}
19+
20+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
21+
public static String getCarInsuranceName(Optional<Person> person, int minAge) {
22+
return person.filter(p -> p.age() >= minAge)
23+
.flatMap(Person::car)
24+
.flatMap(Car::insurance)
25+
.map(Insurance::name)
26+
.orElse("Unknown");
27+
}
28+
29+
public static Set<String> getCarInsuranceNames(List<Person> persons) {
30+
return persons.stream()
31+
.map(Person::car)
32+
.map(optCar -> optCar.flatMap(Car::insurance))
33+
.map(optInsurance -> optInsurance.map(Insurance::name))
34+
.flatMap(Optional::stream)
35+
.collect(Collectors.toSet());
36+
}
37+
38+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
39+
public static Optional<Insurance> nullSafeFindCheapestInsurance(Optional<Person> person, Optional<Car> car) {
40+
return person.flatMap(p -> car.map(c -> findCheapestInsurance(p, c)));
41+
42+
// if (person.isPresent() && car.isPresent()) {
43+
// return Optional.of(findCheapestInsurance(person.get(), car.get()));
44+
// } else {
45+
// return Optional.empty();
46+
// }
47+
}
48+
49+
public static Insurance findCheapestInsurance(Person person, Car car) {
50+
// dummy
51+
return car.insurance().orElse(new Insurance("Unknown"));
52+
}
53+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.example.demo.chapter11;
2+
3+
public record Insurance(String name) {
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.demo.chapter11;
2+
3+
import java.util.Optional;
4+
5+
public record Person(Optional<Car> car, int age) {
6+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.demo.chapter11;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.util.Optional;
6+
import java.util.Properties;
7+
8+
@Slf4j
9+
public class PropertyProcessing {
10+
public static int readDurationImperative(Properties props, String name) {
11+
String value = props.getProperty(name);
12+
if (value != null) {
13+
try {
14+
int i = Integer.parseInt(value);
15+
if (i > 0) {
16+
return i;
17+
}
18+
} catch (NumberFormatException e) {
19+
// e.printStackTrace();
20+
}
21+
}
22+
return 0;
23+
}
24+
25+
public static int readDurationWithOptional(Properties props, String name) {
26+
return Optional.ofNullable(props.getProperty(name))
27+
.flatMap(PropertyProcessing::stringToOptionalInt)
28+
.filter(i -> i > 0)
29+
.orElse(0);
30+
}
31+
32+
public static Optional<Integer> stringToOptionalInt(String str) {
33+
try {
34+
return Optional.of(Integer.parseInt(str));
35+
} catch (NumberFormatException e) {
36+
return Optional.empty();
37+
}
38+
}
39+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.example.demo.chapter11;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.List;
8+
import java.util.Optional;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
@Slf4j
13+
class CarInsuranceProcessingTest {
14+
private Insurance cambridgeInsurance;
15+
private Car car1;
16+
private Car car2;
17+
private Person person1;
18+
private Person person2;
19+
private Person person3;
20+
21+
@BeforeEach
22+
void setUp() {
23+
cambridgeInsurance = new Insurance("CambridgeInsurance");
24+
car1 = new Car(Optional.of(cambridgeInsurance));
25+
car2 = new Car(Optional.empty());
26+
person1 = new Person(Optional.of(car1), 20);
27+
person2 = new Person(Optional.empty(), 25);
28+
person3 = new Person(Optional.of(car2), 30);
29+
}
30+
31+
@Test
32+
void testGetCarInsuranceName1() {
33+
var result1 = CarInsuranceProcessing.getCarInsuranceName(Optional.ofNullable(person1));
34+
assertNotNull(result1);
35+
assertEquals(cambridgeInsurance.name(), result1);
36+
37+
var result2 = CarInsuranceProcessing.getCarInsuranceName(Optional.ofNullable(person2));
38+
assertNotNull(result2);
39+
assertEquals("Unknown", result2);
40+
41+
var result3 = CarInsuranceProcessing.getCarInsuranceName(Optional.ofNullable(person3));
42+
assertNotNull(result3);
43+
assertEquals("Unknown", result3);
44+
}
45+
46+
@Test
47+
void testGetCarInsuranceName2() {
48+
var result1 = CarInsuranceProcessing.getCarInsuranceName(Optional.ofNullable(person1), 18);
49+
assertNotNull(result1);
50+
assertEquals(cambridgeInsurance.name(), result1);
51+
52+
var result2 = CarInsuranceProcessing.getCarInsuranceName(Optional.ofNullable(person2), 18);
53+
assertNotNull(result2);
54+
assertEquals("Unknown", result2);
55+
56+
var result3 = CarInsuranceProcessing.getCarInsuranceName(Optional.ofNullable(person3), 18);
57+
assertNotNull(result3);
58+
assertEquals("Unknown", result3);
59+
}
60+
61+
@Test
62+
void testGetCarInsuranceNames() {
63+
var result = CarInsuranceProcessing.getCarInsuranceNames(List.of(person1, person2, person3));
64+
assertNotNull(result);
65+
assertEquals(1, result.size());
66+
assertEquals(cambridgeInsurance.name(), result.iterator().next());
67+
}
68+
69+
@Test
70+
void testNullSafeFindCheapestInsurance() {
71+
var result1 = CarInsuranceProcessing.nullSafeFindCheapestInsurance(
72+
Optional.ofNullable(person1), Optional.ofNullable(car1));
73+
assertNotNull(result1);
74+
assertNotEquals(Optional.empty(), result1);
75+
76+
var result2 = CarInsuranceProcessing.nullSafeFindCheapestInsurance(
77+
Optional.ofNullable(person2), Optional.empty());
78+
assertNotNull(result2);
79+
assertEquals(Optional.empty(), result2);
80+
81+
var result3 = CarInsuranceProcessing.nullSafeFindCheapestInsurance(
82+
Optional.ofNullable(person3), Optional.ofNullable(car1));
83+
assertNotNull(result3);
84+
assertNotEquals(Optional.empty(), result3);
85+
}
86+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.example.demo.chapter11;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Optional;
7+
import java.util.Properties;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
class PropertyProcessingTest {
12+
private Properties props;
13+
14+
@BeforeEach
15+
void setUp() {
16+
props = new Properties();
17+
props.setProperty("a", "5");
18+
props.setProperty("b", "true");
19+
props.setProperty("c", "-3");
20+
}
21+
22+
@Test
23+
void testReadDurationImperative() {
24+
assertEquals(5, PropertyProcessing.readDurationImperative(props, "a"));
25+
assertEquals(0, PropertyProcessing.readDurationImperative(props, "b"));
26+
assertEquals(0, PropertyProcessing.readDurationImperative(props, "c"));
27+
assertEquals(0, PropertyProcessing.readDurationImperative(props, "d"));
28+
}
29+
30+
@Test
31+
void testReadDurationWithOptional() {
32+
assertEquals(5, PropertyProcessing.readDurationWithOptional(props, "a"));
33+
assertEquals(0, PropertyProcessing.readDurationWithOptional(props, "b"));
34+
assertEquals(0, PropertyProcessing.readDurationWithOptional(props, "c"));
35+
assertEquals(0, PropertyProcessing.readDurationWithOptional(props, "d"));
36+
}
37+
38+
@Test
39+
void testStringToOptionalInt() {
40+
var result1 = PropertyProcessing.stringToOptionalInt(props.getProperty("a"));
41+
assertEquals(5, result1.orElse(0));
42+
43+
var result2 = PropertyProcessing.stringToOptionalInt(props.getProperty("b"));
44+
assertEquals(Optional.empty(), result2);
45+
46+
var result3 = PropertyProcessing.stringToOptionalInt(props.getProperty("c"));
47+
assertEquals(-3, result3.orElse(0));
48+
}
49+
}

0 commit comments

Comments
 (0)