From c8d63912196698612bda55426bf229c4cf565e50 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 14 Sep 2017 22:13:51 +0300 Subject: [PATCH] Removing wildcards from Data and DataNodes --- .../src/main/java/inr/numass/Main.java | 10 +++++---- .../src/main/java/inr/numass/NumassIO.java | 22 +++++++++---------- .../inr/numass/data/MonitorCorrector.java | 6 +++-- .../models/TransmissionInterpolator.java | 9 +++++--- .../sterile/SterileNeutrinoSpectrum.java | 8 +++---- .../inr/numass/tasks/NumassFitScanTask.java | 1 + .../inr/numass/tasks/NumassPrepareTask.java | 2 +- 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/numass-main/src/main/java/inr/numass/Main.java b/numass-main/src/main/java/inr/numass/Main.java index 4722d320..dc030137 100644 --- a/numass-main/src/main/java/inr/numass/Main.java +++ b/numass-main/src/main/java/inr/numass/Main.java @@ -22,6 +22,7 @@ import hep.dataforge.data.FileDataFactory; import hep.dataforge.io.IOManager; import hep.dataforge.io.MetaFileReader; import hep.dataforge.meta.Meta; +import hep.dataforge.providers.Path; import org.apache.commons.cli.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,6 +32,7 @@ import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; import java.io.File; import java.io.FileNotFoundException; +import java.nio.file.Files; import java.util.Locale; import static hep.dataforge.context.Global.out; @@ -87,15 +89,15 @@ public class Main { return; } - File configFile = context.io().getFile(cfgPath); + java.nio.file.Path configFile = context.io().getFile(cfgPath); - if (!configFile.exists()) { + if (!Files.exists(configFile)) { throw new FileNotFoundException("Configuration file not found"); } - Meta config = MetaFileReader.read(configFile).build(); + Meta config = MetaFileReader.read(configFile); - context.putValue(IOManager.ROOT_DIRECTORY_CONTEXT_KEY, configFile.getParentFile().toString()); + context.putValue(IOManager.ROOT_DIRECTORY_CONTEXT_KEY, configFile.getParent().toString()); applyCLItoContext(line, context); diff --git a/numass-main/src/main/java/inr/numass/NumassIO.java b/numass-main/src/main/java/inr/numass/NumassIO.java index 4346b211..068453d2 100644 --- a/numass-main/src/main/java/inr/numass/NumassIO.java +++ b/numass-main/src/main/java/inr/numass/NumassIO.java @@ -28,6 +28,8 @@ import org.apache.commons.io.output.TeeOutputStream; import org.slf4j.LoggerFactory; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -57,7 +59,7 @@ public class NumassIO extends BasicIOManager { ple.setContext(lc); ple.start(); FileAppender appender = new FileAppender<>(); - appender.setFile(new File(getWorkDirectory(), meta().getString("logFileName", "numass.log")).toString()); + appender.setFile(new File(getWorkDirectory().toFile(), meta().getString("logFileName", "numass.log")).toString()); appender.setEncoder(ple); return appender; } @@ -97,26 +99,24 @@ public class NumassIO extends BasicIOManager { return out; } - private OutputStream buildOut(File parentDir, String dirName, String fileName) { - File outputFile; + private OutputStream buildOut(Path parentDir, String dirName, String fileName) { + Path outputFile; - if (!parentDir.exists()) { + if (!Files.exists(parentDir)) { throw new RuntimeException("Working directory does not exist"); } if (dirName != null && !dirName.isEmpty()) { - parentDir = new File(parentDir, dirName); - if (!parentDir.exists()) { - parentDir.mkdirs(); - } + parentDir = parentDir.resolve(dirName); + Files.createDirectories(parentDir); } // String output = source.meta().getString("output", this.meta().getString("output", fileName + ".onComplete")); - outputFile = new File(parentDir, fileName); + outputFile = parentDir.resolve(fileName); try { if (getContext().getBoolean("numass.consoleOutput", false)) { - return new TeeOutputStream(new FileOutputStream(outputFile), System.out); + return new TeeOutputStream(Files.newOutputStream(outputFile), System.out); } else { - return new FileOutputStream(outputFile); + return Files.newOutputStream(outputFile); } } catch (FileNotFoundException ex) { throw new RuntimeException(ex); diff --git a/numass-main/src/main/java/inr/numass/data/MonitorCorrector.java b/numass-main/src/main/java/inr/numass/data/MonitorCorrector.java index 0a829353..4c6eb203 100644 --- a/numass-main/src/main/java/inr/numass/data/MonitorCorrector.java +++ b/numass-main/src/main/java/inr/numass/data/MonitorCorrector.java @@ -21,6 +21,8 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.ParseException; import java.time.LocalDateTime; import java.util.ArrayList; @@ -42,10 +44,10 @@ public class MonitorCorrector { this(Global.instance().io().getFile(path)); } - public MonitorCorrector(File monitorFile) throws ParseException, IOException { + public MonitorCorrector(Path monitorFile) throws ParseException, IOException { list = new ArrayList<>(); - BufferedReader reader = new BufferedReader(new FileReader(monitorFile)); + BufferedReader reader = new BufferedReader(Files.newBufferedReader(monitorFile)); // Scanner sc = new Scanner(monitorFile); double sum = 0; diff --git a/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java b/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java index 85fe07d7..8b58c446 100644 --- a/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java +++ b/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java @@ -29,6 +29,9 @@ import org.apache.commons.math3.analysis.interpolation.LinearInterpolator; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -40,10 +43,10 @@ public class TransmissionInterpolator implements UnivariateFunction { public static TransmissionInterpolator fromFile(Context context, String path, String xName, String yName, int nSmooth, double w, double border) { try { - File dataFile = context.io().getFile(path); - ColumnedDataReader reader = new ColumnedDataReader(new FileInputStream(dataFile)); + Path dataFile = context.io().getFile(path); + ColumnedDataReader reader = new ColumnedDataReader(Files.newInputStream(dataFile)); return new TransmissionInterpolator(reader, xName, yName, nSmooth, w, border); - } catch (FileNotFoundException ex) { + } catch (IOException ex) { throw new RuntimeException(ex); } } diff --git a/numass-main/src/main/java/inr/numass/models/sterile/SterileNeutrinoSpectrum.java b/numass-main/src/main/java/inr/numass/models/sterile/SterileNeutrinoSpectrum.java index 8f71a662..d787ef46 100644 --- a/numass-main/src/main/java/inr/numass/models/sterile/SterileNeutrinoSpectrum.java +++ b/numass-main/src/main/java/inr/numass/models/sterile/SterileNeutrinoSpectrum.java @@ -63,11 +63,9 @@ public class SterileNeutrinoSpectrum extends AbstractParametricFunction { if (configuration.getBoolean("useFSS", true)) { InputStream fssStream; if (configuration.hasValue("fssFile")) { - try { - fssStream = new FileInputStream(context.io().getFile(configuration.getString("fssFile"))); - } catch (FileNotFoundException e) { - throw new RuntimeException("Could not locate FSS file"); - } + fssStream = context.io().optBinary(configuration.getString("fssFile")) + .orElseThrow(()-> new RuntimeException("Could not locate FSS file")) + .getStream(); } else { fssStream = getClass().getResourceAsStream("/data/FS.txt"); } diff --git a/numass-main/src/main/java/inr/numass/tasks/NumassFitScanTask.java b/numass-main/src/main/java/inr/numass/tasks/NumassFitScanTask.java index 6ac362c6..a3edce4e 100644 --- a/numass-main/src/main/java/inr/numass/tasks/NumassFitScanTask.java +++ b/numass-main/src/main/java/inr/numass/tasks/NumassFitScanTask.java @@ -6,6 +6,7 @@ package inr.numass.tasks; import hep.dataforge.actions.Action; +import hep.dataforge.data.Data; import hep.dataforge.data.DataNode; import hep.dataforge.data.DataTree; import hep.dataforge.meta.Meta; diff --git a/numass-main/src/main/java/inr/numass/tasks/NumassPrepareTask.java b/numass-main/src/main/java/inr/numass/tasks/NumassPrepareTask.java index 805a47ce..7f2ba2e0 100644 --- a/numass-main/src/main/java/inr/numass/tasks/NumassPrepareTask.java +++ b/numass-main/src/main/java/inr/numass/tasks/NumassPrepareTask.java @@ -42,7 +42,7 @@ public class NumassPrepareTask extends AbstractTask { //acquiring initial data. Data node could not be empty - DataFilter filter = new DataFilter().configure(config.getMeta("data")); + DataFilter filter = new DataFilter(config.getMeta("data")); DataNode data = filter.filter(input.checked(NumassSet.class));