| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 442ef0e commit 150e8bd
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,8 +16,7 @@ | |||
| 16 | 16 | <dependency> | |
| 17 | 17 | <groupId>junit</groupId> | |
| 18 | 18 | <artifactId>junit</artifactId> | |
| 19 | - <version>4.8.1</version> | ||
| 20 | - <scope>test</scope> | ||
| 19 | + <version>4.11</version> | ||
| 21 | 20 | </dependency> | |
| 22 | 21 | </dependencies> | |
| 23 | 22 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + package lambdasinaction.chap9; | ||
| 2 | + | ||
| 3 | + import java.util.*; | ||
| 4 | + | ||
| 5 | + public class Car { | ||
| 6 | + | ||
| 7 | + private Optional<Insurance> insurance; | ||
| 8 | + | ||
| 9 | + public Optional<Insurance> getInsurance() { | ||
| 10 | + return insurance; | ||
| 11 | + } | ||
| 12 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + package lambdasinaction.chap9; | ||
| 2 | + | ||
| 3 | + public class Insurance { | ||
| 4 | + | ||
| 5 | + private String name; | ||
| 6 | + | ||
| 7 | + public String getName() { | ||
| 8 | + return name; | ||
| 9 | + } | ||
| 10 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,13 @@ | |||
| 1 | + package lambdasinaction.chap9; | ||
| 2 | + | ||
| 3 | + import java.util.*; | ||
| 4 | + | ||
| 5 | + public class OptionalMain { | ||
| 6 | + | ||
| 7 | + public String getCarInsuranceName(Optional<Person> person) { | ||
| 8 | + return person.flatMap(Person::getCar) | ||
| 9 | + .flatMap(Car::getInsurance) | ||
| 10 | + .map(Insurance::getName) | ||
| 11 | + .orElse("Unknown"); | ||
| 12 | + } | ||
| 13 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + package lambdasinaction.chap9; | ||
| 2 | + | ||
| 3 | + import java.util.*; | ||
| 4 | + | ||
| 5 | + public class Person { | ||
| 6 | + | ||
| 7 | + private Optional<Car> car; | ||
| 8 | + | ||
| 9 | + public Optional<Car> getCar() { | ||
| 10 | + return car; | ||
| 11 | + } | ||
| 12 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,57 @@ | |||
| 1 | + package lambdasinaction.chap9; | ||
| 2 | + | ||
| 3 | + import org.junit.*; | ||
| 4 | + | ||
| 5 | + import java.util.*; | ||
| 6 | + | ||
| 7 | + import static java.util.Optional.*; | ||
| 8 | + import static org.junit.Assert.assertEquals; | ||
| 9 | + | ||
| 10 | + public class ReadPositiveIntParam { | ||
| 11 | + | ||
| 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 | + | ||
| 30 | + public static int readDurationImperative(Properties props, String name) { | ||
| 31 | + String value = props.getProperty(name); | ||
| 32 | + if (value != null) { | ||
| 33 | + try { | ||
| 34 | + int i = Integer.parseInt(value); | ||
| 35 | + if (i > 0) { | ||
| 36 | + return i; | ||
| 37 | + } | ||
| 38 | + } catch (NumberFormatException nfe) { } | ||
| 39 | + } | ||
| 40 | + return 0; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public static int readDurationWithOptional(Properties props, String name) { | ||
| 44 | + return ofNullable(props.getProperty(name)) | ||
| 45 | + .flatMap(ReadPositiveIntParam::s2i) | ||
| 46 | + .filter(i -> i > 0).orElse(0); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + public static Optional<Integer> s2i(String s) { | ||
| 50 | + try { | ||
| 51 | + return of(Integer.parseInt(s)); | ||
| 52 | + } catch (NumberFormatException e) { | ||
| 53 | + return empty(); | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments