Skip to content

Commit f7cd66b

Browse files
committed
第10章示例代码
1 parent 5e2a53e commit f7cd66b

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.wbs.java8lambda.chapter10;
2+
3+
import java.util.Optional;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-11-09 14:40
10+
**/
11+
public class Car {
12+
private Optional<Insurance> insurance;
13+
14+
public Optional<Insurance> getInsurance() {
15+
return insurance;
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.wbs.java8lambda.chapter10;
2+
3+
/**
4+
* TODO
5+
*
6+
* @author: wangbingshuai
7+
* @create: 2019-11-09 14:39
8+
**/
9+
public class Insurance {
10+
private String name;
11+
12+
public String getName() {
13+
return name;
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.wbs.java8lambda.chapter10;
2+
3+
import java.util.Optional;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-11-09 14:42
10+
**/
11+
public class Person {
12+
private Optional<Car> car;
13+
14+
public Optional<Car> getCar() {
15+
return car;
16+
}
17+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.wbs.java8lambda.chapter10;
2+
3+
import java.util.Optional;
4+
import java.util.Properties;
5+
6+
/**
7+
* TODO
8+
*
9+
* @author: wangbingshuai
10+
* @create: 2019-11-09 14:55
11+
**/
12+
public class ReadPositiveIntParam {
13+
14+
public static void main(String[] args) {
15+
Properties props = new Properties();
16+
props.setProperty("a", "5");
17+
props.setProperty("b", "true");
18+
props.setProperty("c", "-3");
19+
20+
System.out.println(readDurationImperative(props, "a"));
21+
System.out.println(readDurationImperative(props, "b"));
22+
System.out.println(readDurationImperative(props, "c"));
23+
System.out.println(readDurationImperative(props, "d"));
24+
25+
System.out.println(readDurationWithOptional(props, "a"));
26+
System.out.println(readDurationWithOptional(props, "b"));
27+
System.out.println(readDurationWithOptional(props, "c"));
28+
System.out.println(readDurationWithOptional(props, "d"));
29+
}
30+
31+
private static int readDurationImperative(Properties props, String name) {
32+
String value = props.getProperty(name);
33+
if (value != null) {
34+
try {
35+
int i = Integer.parseInt(value);
36+
if (i > 0) {
37+
return i;
38+
}
39+
} catch (NumberFormatException nfe) {
40+
41+
}
42+
}
43+
return 0;
44+
}
45+
46+
private static int readDurationWithOptional(Properties props, String name) {
47+
return Optional.ofNullable(props.getProperty(name))
48+
.flatMap(ReadPositiveIntParam::s2i)
49+
.filter(i -> i > 0).orElse(0);
50+
}
51+
52+
private static Optional<Integer> s2i(String s) {
53+
try {
54+
return Optional.of(Integer.parseInt(s));
55+
} catch (NumberFormatException e) {
56+
return Optional.empty();
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)