Fixed path issue using nio Path

This commit is contained in:
Bruce Eckel 2015-09-07 16:34:48 -06:00
parent 06855b96c5
commit 5a25feafef
6 changed files with 16 additions and 13 deletions

View File

@ -56,7 +56,7 @@ public class DoubleDispatch {
ArrayList<Trash> bin = new ArrayList<>(); ArrayList<Trash> bin = new ArrayList<>();
TrashBinSet bins = new TrashBinSet(); TrashBinSet bins = new TrashBinSet();
// ParseTrash still works, without changes: // ParseTrash still works, without changes:
ParseTrash.fillBin("DDTrash.dat", bin); ParseTrash.fillBin("doubledispatch", "DDTrash.dat", bin);
// Sort from the master bin into the // Sort from the master bin into the
// individually-typed bins: // individually-typed bins:
bins.sortIntoBins(bin); bins.sortIntoBins(bin);

View File

@ -44,7 +44,7 @@ public class DynaTrash {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void main(String[] args) { public static void main(String[] args) {
TypeMap<Trash> bin = new TypeMap<>(); TypeMap<Trash> bin = new TypeMap<>();
ParseTrash.fillBin("Trash.dat", ParseTrash.fillBin("recycleap", "Trash.dat",
new TypeMapAdapter(bin)); new TypeMapAdapter(bin));
Iterator<Class> keys = bin.keys(); Iterator<Class> keys = bin.keys();
while(keys.hasNext()) while(keys.hasNext())

View File

@ -9,7 +9,7 @@ public class RecycleAP {
public static void main(String[] args) { public static void main(String[] args) {
ArrayList<Trash> bin = new ArrayList<>(); ArrayList<Trash> bin = new ArrayList<>();
// Fill up the Trash bin: // Fill up the Trash bin:
ParseTrash.fillBin("Trash.dat", bin); ParseTrash.fillBin("recycleap", "Trash.dat", bin);
List<Glass> glassBin = new ArrayList<>(); List<Glass> glassBin = new ArrayList<>();
List<Paper> paperBin = new ArrayList<>(); List<Paper> paperBin = new ArrayList<>();
List<Aluminum> alBin = new ArrayList<>(); List<Aluminum> alBin = new ArrayList<>();

View File

@ -41,7 +41,7 @@ public class RecycleB {
static Tbin<Trash> bin = new Tbin<>(Trash.class); static Tbin<Trash> bin = new Tbin<>(Trash.class);
public static void main(String[] args) { public static void main(String[] args) {
// Fill up the Trash bin: // Fill up the Trash bin:
ParseTrash.fillBin("Trash.dat", bin); ParseTrash.fillBin("recycleap", "Trash.dat", bin);
TbinList<Trash> trashBins = new TbinList<>(); TbinList<Trash> trashBins = new TbinList<>();
trashBins.add(new Tbin<>(Aluminum.class)); trashBins.add(new Tbin<>(Aluminum.class));

View File

@ -5,22 +5,25 @@
package patterns.trash; package patterns.trash;
import java.util.*; import java.util.*;
import java.io.*; import java.io.*;
import java.nio.file.*;
public class ParseTrash { public class ParseTrash {
public static <T extends Trash> void public static <T extends Trash> void
fillBin(String filename, Fillable<T> bin) { fillBin(String dir, String fname, Fillable<T> bin) {
try { try {
try(BufferedReader data = new BufferedReader( Path p = FileSystems.getDefault()
new FileReader(filename))) { .getPath("..", dir, fname);
try(BufferedReader data = Files.newBufferedReader(p)){
String buf; String buf;
while((buf = data.readLine())!= null) { while((buf = data.readLine())!= null) {
if(buf.trim().length() == 0) if(buf.trim().length() == 0)
continue; // Skip empty lines continue; // Skip empty lines
if(buf.startsWith("//"))
continue; // Skip comments
String type = buf.substring(0, String type = buf.substring(0,
buf.indexOf(':')).trim(); buf.indexOf(':')).trim();
double weight = Double.valueOf( double weight = Double.valueOf(
buf.substring(buf.indexOf(':') + 1) buf.substring(buf.indexOf(':') + 1).trim());
.trim());
bin.addTrash(Trash.factory( bin.addTrash(Trash.factory(
new Trash.Info(type, weight))); new Trash.Info(type, weight)));
} }
@ -34,7 +37,7 @@ public class ParseTrash {
} }
// Special case to handle ArrayList: // Special case to handle ArrayList:
public static <T extends Trash> void public static <T extends Trash> void
fillBin(String filename, ArrayList<T> bin) { fillBin(String dir, String fname, ArrayList<T> bin) {
fillBin(filename, new FillableList<>(bin)); fillBin(dir, fname, new FillableList<>(bin));
} }
} }

View File

@ -83,7 +83,7 @@ public class TrashVisitor {
public static void main(String[] args) { public static void main(String[] args) {
ArrayList<Trash> bin = new ArrayList<>(); ArrayList<Trash> bin = new ArrayList<>();
// ParseTrash still works, without changes: // ParseTrash still works, without changes:
ParseTrash.fillBin("VTrash.dat", bin); ParseTrash.fillBin("trashvisitor", "VTrash.dat", bin);
// You could even iterate through // You could even iterate through
// a list of visitors! // a list of visitors!
PriceVisitor pv = new PriceVisitor(); PriceVisitor pv = new PriceVisitor();