forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteAround.java
More file actions
54 lines (40 loc) · 1.48 KB
/
Copy pathExecuteAround.java
File metadata and controls
54 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.brianway.learning.java8.lambda;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Lambda实践: 环绕执行模式
* 1. 行为参数化
* 2. 使用函数式接口来传递行为
* 3. 执行一个行为
* 4. 传递 Lambda
*/
public class ExecuteAround {
private static final String RESOURCE_ROOT = ExecuteAround.class
.getResource("/").getPath() + "/data-lambda.txt";
public static void main(String... args) throws IOException {
// method we want to refactor to make more flexible
String result = processFileLimited();
System.out.println(result);
System.out.println("---");
String oneLine = processFile(BufferedReader::readLine);
System.out.println(oneLine);
String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine());
System.out.println(twoLines);
}
public static String processFileLimited() throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader(RESOURCE_ROOT))) {
return br.readLine();
}
}
public static String processFile(BufferedReaderProcessor p) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(RESOURCE_ROOT))) {
return p.process(br);
}
}
@FunctionalInterface
public interface BufferedReaderProcessor {
String process(BufferedReader b) throws IOException;
}
}