From 0413955b60cd292550df19068d4f380b7de6fc86 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 13 Mar 2017 20:05:32 -0400 Subject: simplify --- dslog/Logdir.java | 101 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 48 deletions(-) (limited to 'dslog/Logdir.java') diff --git a/dslog/Logdir.java b/dslog/Logdir.java index b798757..ea7683b 100644 --- a/dslog/Logdir.java +++ b/dslog/Logdir.java @@ -2,74 +2,79 @@ package dslog; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; -import java.util.SortedMap; import java.util.Map; +import java.util.SortedMap; import java.util.TreeMap; +import java.util.function.Supplier; class Logdir { public static class LogSet { - public String name; - public String dslog = "␕"; - public String dsevents = "␕"; - public String pdplog = "␕"; + public final String name; + public LogSet(String name) { + this.name = name; + } + public Path dslogPath, dseventsPath, pdplogPath; public String toString() { - return name+" "+dslog+"/"+dsevents+"/"+pdplog; + return name+" "+verstr(this::dslog)+"/"+verstr(this::dsevents)+"/"+verstr(this::pdplog); + } + + private static String verstr(Versioned.Supplier getter) { + try (Versioned thing = getter.get()) { + if (thing == null) { + return "␕"; + } + return ""+thing.version(); + } catch (IOException e) { + return "☠"; + } + } + + public DslogReader dslog() throws IOException { + if (dslogPath == null) { + return null; + } + return new DslogReader(Files.newInputStream(dslogPath)); + } + public DseventsReader dsevents() throws IOException { + if (dseventsPath == null) { + return null; + } + return new DseventsReader(Files.newInputStream(dseventsPath)); + } + public PdplogReader pdplog() throws IOException { + if (pdplogPath == null) { + return null; + } + return new PdplogReader(Files.newInputStream(pdplogPath)); } } - private static LogSet getSet(Map sets, String name) { + private static LogSet getSet(Map sets, String filename) { + String setname = filename.substring(0, filename.lastIndexOf('.')); LogSet set; - synchronized (sets) { - set = sets.get(name); - if (set == null) { - set = new LogSet(); - set.name = name; - sets.put(name, set); - } + set = sets.get(setname); + if (set == null) { + set = new LogSet(setname); + sets.put(set.name, set); } return set; } public static SortedMap opendir(String dirname) throws IOException { SortedMap logsets = new TreeMap(); List errs = new ArrayList(); - Files.list(Paths.get(dirname)).parallel().forEach(filepath->{ - try { - String filename = filepath.getFileName().toString(); - if (filename.endsWith(".dslog")) { - String setname = filename.substring(0, filename.lastIndexOf('.')); - try (DslogReader dslog = new DslogReader(Files.newInputStream(filepath))) { - getSet(logsets, setname).dslog = ""+dslog.version(); - } catch (Exception e) { - getSet(logsets, setname).pdplog = "☠"; - throw e; - } - } else if (filename.endsWith(".dsevents")) { - String setname = filename.substring(0, filename.lastIndexOf('.')); - try (DseventsReader dsevents = new DseventsReader(Files.newInputStream(filepath))) { - getSet(logsets, setname).dsevents = ""+dsevents.version(); - } catch (Exception e) { - getSet(logsets, setname).pdplog = "☠"; - throw e; - } - } else if (filename.endsWith(".pdplog")) { - String setname = filename.substring(0, filename.lastIndexOf('.')); - try (PdplogReader pdplog = new PdplogReader(Files.newInputStream(filepath))) { - getSet(logsets, setname).pdplog = ""+pdplog.version(); - } catch (Exception e) { - getSet(logsets, setname).pdplog = "☠"; - throw e; - } - } - } catch (Exception e) { - e = new IOException("Could not read file: "+filepath.toString(), e); - synchronized (errs) { - errs.add(e); - } - e.printStackTrace(); + Files.list(Paths.get(dirname)).forEach(filepath->{ + String filename = filepath.getFileName().toString(); + if (filename.endsWith(".dslog")) { + getSet(logsets, filename).dslogPath = filepath; + } else if (filename.endsWith(".dsevents")) { + getSet(logsets, filename).dseventsPath = filepath; + } else if (filename.endsWith(".pdplog")) { + getSet(logsets, filename).pdplogPath = filepath; } }); return logsets; -- cgit v1.2.3