Replace File by Path

This commit is contained in:
Alexander Nozik 2017-09-14 19:56:02 +03:00
parent 06f7bdeb6f
commit 675bd291b8
4 changed files with 16 additions and 91 deletions

View File

@ -1,76 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inr.numass.data;
import hep.dataforge.context.Context;
import hep.dataforge.context.Global;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* A temporary utility to store set directions to avoid multiple file reading
*
* @author Alexander Nozik
*/
@Deprecated
public class SetDirectionUtility {
private static final String FILE_NAME = "numass_set_direction.map";
private static final Map<String, Boolean> directionMap = new HashMap<>();
private static boolean isLoaded = false;
static synchronized boolean isReversed(String setName, Function<String, Boolean> provider) {
if (!isLoaded) {
load(Global.instance());
}
return directionMap.computeIfAbsent(setName, provider);
}
public static File cacheFile(Context context) {
return new File(context.io().getTmpDirectory(), FILE_NAME);
}
@SuppressWarnings("unchecked")
public static synchronized void load(Context context) {
context.getLogger().info("Loading set direction utility");
File file = cacheFile(context);
if (file.exists()) {
directionMap.clear();
try (FileInputStream fst = new FileInputStream(file)) {
try (ObjectInputStream st = new ObjectInputStream(fst)) {
directionMap.putAll((Map<String, Boolean>) st.readObject());
context.getLogger().info("Set directions successfully loaded from file");
} catch (ClassNotFoundException | IOException ex) {
context.getLogger().error("Failed to load numass direction mapping", ex);
}
} catch (IOException ex) {
context.getLogger().error("Failed to load numass direction mapping", ex);
}
}
isLoaded = true;
}
public static synchronized void save(Context context) {
try {
File file = cacheFile(context);
if (!file.exists()) {
file.createNewFile();
}
try (ObjectOutputStream st = new ObjectOutputStream(new FileOutputStream(file))) {
st.writeObject(directionMap);
context.getLogger().info("Set directions successfully saved to file");
}
} catch (IOException ex) {
context.getLogger().error("Failed to save numass direction mapping", ex);
}
}
}

View File

@ -16,8 +16,8 @@ import hep.dataforge.tables.ValueMap;
import hep.dataforge.values.Values; import hep.dataforge.values.Values;
import inr.numass.utils.NumassUtils; import inr.numass.utils.NumassUtils;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional; import java.util.Optional;
/** /**
@ -30,7 +30,7 @@ public class SubstractSpectrumAction extends OneToOneAction<Table, Table> {
protected Table execute(Context context, String name, Table input, Laminate inputMeta) { protected Table execute(Context context, String name, Table input, Laminate inputMeta) {
try { try {
String referencePath = inputMeta. getString("file", "empty.dat"); String referencePath = inputMeta. getString("file", "empty.dat");
File referenceFile = context.io().getFile(referencePath); Path referenceFile = context.io().getFile(referencePath);
Table referenceTable = new ColumnedDataReader(referenceFile).toTable(); Table referenceTable = new ColumnedDataReader(referenceFile).toTable();
ListTable.Builder builder = new ListTable.Builder(input.getFormat()); ListTable.Builder builder = new ListTable.Builder(input.getFormat());
input.getRows().forEach(point -> { input.getRows().forEach(point -> {

View File

@ -22,8 +22,8 @@ import hep.dataforge.tables.ValueMap;
import hep.dataforge.values.Values; import hep.dataforge.values.Values;
import inr.numass.data.SpectrumDataAdapter; import inr.numass.data.SpectrumDataAdapter;
import java.io.File; import java.io.IOException;
import java.io.FileNotFoundException; import java.nio.file.Path;
import java.util.Locale; import java.util.Locale;
import java.util.Scanner; import java.util.Scanner;
@ -35,10 +35,10 @@ import static java.util.Locale.setDefault;
*/ */
public class OldDataReader { public class OldDataReader {
public static Table readConfig(String path) throws FileNotFoundException { public static Table readConfig(String path) throws IOException {
String[] list = {"X", "time", "ushift"}; String[] list = {"X", "time", "ushift"};
ListTable.Builder res = new ListTable.Builder(list); ListTable.Builder res = new ListTable.Builder(list);
File file = Global.instance().io().getFile(path); Path file = Global.instance().io().getFile(path);
Scanner sc = new Scanner(file); Scanner sc = new Scanner(file);
sc.nextLine(); sc.nextLine();
@ -60,7 +60,7 @@ public class OldDataReader {
public static Table readData(String path, double Elow) { public static Table readData(String path, double Elow) {
SpectrumDataAdapter factory = new SpectrumDataAdapter(); SpectrumDataAdapter factory = new SpectrumDataAdapter();
ListTable.Builder res = new ListTable.Builder(factory.getFormat()); ListTable.Builder res = new ListTable.Builder(factory.getFormat());
File file = Global.instance().io().getFile(path); Path file = Global.instance().io().getFile(path);
double x; double x;
int count; int count;
int time; int time;
@ -70,7 +70,7 @@ public class OldDataReader {
Scanner sc; Scanner sc;
try { try {
sc = new Scanner(file); sc = new Scanner(file);
} catch (FileNotFoundException ex) { } catch (IOException ex) {
throw new RuntimeException(ex.getMessage()); throw new RuntimeException(ex.getMessage());
} }
double dummy; double dummy;
@ -112,7 +112,7 @@ public class OldDataReader {
public static Table readDataAsGun(String path, double Elow) { public static Table readDataAsGun(String path, double Elow) {
SpectrumDataAdapter factory = new SpectrumDataAdapter(); SpectrumDataAdapter factory = new SpectrumDataAdapter();
ListTable.Builder res = new ListTable.Builder(factory.getFormat()); ListTable.Builder res = new ListTable.Builder(factory.getFormat());
File file = Global.instance().io().getFile(path); Path file = Global.instance().io().getFile(path);
double x; double x;
long count; long count;
int time; int time;
@ -122,7 +122,7 @@ public class OldDataReader {
Scanner sc; Scanner sc;
try { try {
sc = new Scanner(file); sc = new Scanner(file);
} catch (FileNotFoundException ex) { } catch (IOException ex) {
throw new RuntimeException(ex.getMessage()); throw new RuntimeException(ex.getMessage());
} }
double dummy; double dummy;
@ -145,7 +145,7 @@ public class OldDataReader {
public static Table readSpectrumData(String path) { public static Table readSpectrumData(String path) {
SpectrumDataAdapter factory = new SpectrumDataAdapter(); SpectrumDataAdapter factory = new SpectrumDataAdapter();
ListTable.Builder res = new ListTable.Builder(factory.getFormat()); ListTable.Builder res = new ListTable.Builder(factory.getFormat());
File file = Global.instance().io().getFile(path); Path file = Global.instance().io().getFile(path);
double x; double x;
double count; double count;
double time; double time;
@ -158,7 +158,7 @@ public class OldDataReader {
Scanner sc; Scanner sc;
try { try {
sc = new Scanner(file); sc = new Scanner(file);
} catch (FileNotFoundException ex) { } catch (IOException ex) {
throw new RuntimeException(ex.getMessage()); throw new RuntimeException(ex.getMessage());
} }

View File

@ -8,8 +8,9 @@ import hep.dataforge.meta.SimpleConfigurable;
import inr.numass.data.storage.NumassStorage; import inr.numass.data.storage.NumassStorage;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException; import java.text.ParseException;
/** /**
@ -27,8 +28,8 @@ public class ServerRunner extends SimpleConfigurable implements AutoCloseable {
public ServerRunner() throws IOException, ParseException { public ServerRunner() throws IOException, ParseException {
// Global.instance().pluginManager().load(StorageManager.class); // Global.instance().pluginManager().load(StorageManager.class);
File configFile = new File(SERVER_CONFIG_PATH); Path configFile = context.io().getFile(SERVER_CONFIG_PATH);
if (configFile.exists()) { if (Files.exists(configFile)) {
context.getLogger().info("Trying to read server configuration from {}", SERVER_CONFIG_PATH); context.getLogger().info("Trying to read server configuration from {}", SERVER_CONFIG_PATH);
configure(MetaFileReader.read(configFile)); configure(MetaFileReader.read(configFile));
} }