OnJava8-Examples/patterns/trash/ParseTrash.java

92 lines
3.0 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// patterns/trash/ParseTrash.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
// Open a file and parse its contents into
2016-01-25 18:05:55 -08:00
// Trash objects, placing each into a List
2016-07-28 12:48:23 -06:00
// {java patterns.trash.ParseTrash}
2015-06-15 17:47:35 -07:00
package patterns.trash;
import java.util.*;
2015-11-03 12:00:44 -08:00
import java.util.stream.*;
2015-06-15 17:47:35 -07:00
import java.io.*;
2015-09-07 16:34:48 -06:00
import java.nio.file.*;
2015-11-03 12:00:44 -08:00
import java.nio.file.Files;
2015-06-15 17:47:35 -07:00
public class ParseTrash {
public static <T extends Trash> void
2015-11-03 12:00:44 -08:00
fillBin(String pckg, Fillable<T> bin) {
2015-06-15 17:47:35 -07:00
try {
2016-07-07 15:57:38 -06:00
Files.lines(Paths.get("trash", "Trash.dat"))
2015-11-03 12:00:44 -08:00
// 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();
2015-06-15 17:47:35 -07:00
double weight = Double.valueOf(
2015-11-03 12:00:44 -08:00
line.substring(line.indexOf(':') + 1)
.trim());
2015-06-15 17:47:35 -07:00
bin.addTrash(Trash.factory(
new Trash.Info(type, weight)));
2015-11-03 12:00:44 -08:00
});
2015-06-15 17:47:35 -07:00
} catch(IOException |
NumberFormatException |
2015-11-03 12:00:44 -08:00
Trash.TrashClassNotFoundException |
2015-06-15 17:47:35 -07:00
Trash.CannotCreateTrashException e) {
throw new RuntimeException(e);
}
}
2015-11-03 12:00:44 -08:00
// Special case to handle List:
2015-06-15 17:47:35 -07:00
public static <T extends Trash> void
2015-11-03 12:00:44 -08:00
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);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
2015-12-02 09:20:27 -08:00
/* 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
*/