forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseTrash.java
More file actions
91 lines (90 loc) · 2.99 KB
/
ParseTrash.java
File metadata and controls
91 lines (90 loc) · 2.99 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// patterns/trash/ParseTrash.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Open a file and parse its contents into
// Trash objects, placing each into a List
// {java patterns.trash.ParseTrash}
package patterns.trash;
import java.util.*;
import java.util.stream.*;
import java.io.*;
import java.nio.file.*;
import java.nio.file.Files;
public class ParseTrash {
public static <T extends Trash> void
fillBin(String pckg, Fillable<T> bin) {
try {
Files.lines(Paths.get("trash", "Trash.dat"))
// Remove empty lines and comment lines:
.filter(line -> line.trim().length() != 0)
.filter(line -> !line.startsWith("//"))
.forEach( line -> {
String type = "patterns." + pckg + "." +
line.substring(
0, line.indexOf(':')).trim();
double weight = Double.valueOf(
line.substring(line.indexOf(':') + 1)
.trim());
bin.addTrash(Trash.factory(
new Trash.Info(type, weight)));
});
} catch(IOException |
NumberFormatException |
Trash.TrashClassNotFoundException |
Trash.CannotCreateTrashException e) {
throw new RuntimeException(e);
}
}
// Special case to handle List:
public static <T extends Trash> void
fillBin(String pckg, List<T> bin) {
fillBin(pckg, new FillableList<>(bin));
}
// Basic test:
public static void main(String[] args) {
List<Trash> t = new ArrayList<>();
fillBin("trash", t);
t.forEach(System.out::println);
}
}
/* Output:
Loading patterns.trash.Glass
Loading patterns.trash.Paper
Loading patterns.trash.Aluminum
Loading patterns.trash.Cardboard
patterns.trash.Glass w:54.0 v:0.23
patterns.trash.Paper w:22.0 v:0.10
patterns.trash.Paper w:11.0 v:0.10
patterns.trash.Glass w:17.0 v:0.23
patterns.trash.Aluminum w:89.0 v:1.67
patterns.trash.Paper w:88.0 v:0.10
patterns.trash.Aluminum w:76.0 v:1.67
patterns.trash.Cardboard w:96.0 v:0.23
patterns.trash.Aluminum w:25.0 v:1.67
patterns.trash.Aluminum w:34.0 v:1.67
patterns.trash.Glass w:11.0 v:0.23
patterns.trash.Glass w:68.0 v:0.23
patterns.trash.Glass w:43.0 v:0.23
patterns.trash.Aluminum w:27.0 v:1.67
patterns.trash.Cardboard w:44.0 v:0.23
patterns.trash.Aluminum w:18.0 v:1.67
patterns.trash.Paper w:91.0 v:0.10
patterns.trash.Glass w:63.0 v:0.23
patterns.trash.Glass w:50.0 v:0.23
patterns.trash.Glass w:80.0 v:0.23
patterns.trash.Aluminum w:81.0 v:1.67
patterns.trash.Cardboard w:12.0 v:0.23
patterns.trash.Glass w:12.0 v:0.23
patterns.trash.Glass w:54.0 v:0.23
patterns.trash.Aluminum w:36.0 v:1.67
patterns.trash.Aluminum w:93.0 v:1.67
patterns.trash.Glass w:93.0 v:0.23
patterns.trash.Paper w:80.0 v:0.10
patterns.trash.Glass w:36.0 v:0.23
patterns.trash.Glass w:12.0 v:0.23
patterns.trash.Glass w:60.0 v:0.23
patterns.trash.Paper w:66.0 v:0.10
patterns.trash.Aluminum w:36.0 v:1.67
patterns.trash.Cardboard w:22.0 v:0.23
*/