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 inr.numass.utils.NumassUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
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) {
try {
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();
ListTable.Builder builder = new ListTable.Builder(input.getFormat());
input.getRows().forEach(point -> {

View File

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

View File

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