com.meliorbis.numerics.io.csv.CSVReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Numerics Show documentation
Show all versions of Numerics Show documentation
A library for working with large multi-dimensional arrays and the functions they represent
package com.meliorbis.numerics.io.csv;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.meliorbis.numerics.Numerics;
import com.meliorbis.numerics.generic.MultiDimensionalArray;
import com.meliorbis.numerics.io.NumericsReader;
/**
* A reader implementation that reads the data from a specified directory, with structures assumed to be subdirectories
* and arrays assumed to be CSV files
*
* @author Tobias Grasl
*/
public class CSVReader implements NumericsReader
{
private final File _dir;
/**
* Constructs a reader that reads from the directory at the specified path, using the provided numerics library
*
* @param path_ Path to directory containing the files
*/
public CSVReader(String path_)
{
this(new File(path_));
}
/**
* Constructs a reader that reads from the directory specified, using the provided numerics library
*
* @param dir_ Directory containing the files
*/
public CSVReader(File dir_)
{
_dir = dir_;
}
@Override
public MultiDimensionalArray getArray(String name_) throws IOException
{
name_ = CSVUtil.ensureExtension(name_);
return readArrayFromDir(name_, _dir);
}
private MultiDimensionalArray readArrayFromDir(String name_, File dir_)
{
return Numerics.instance().readFormattedCSV(new File(dir_, name_));
}
@Override
public Map> getArrays() throws IOException
{
return readMapFromDirectory(_dir);
}
private Map> readMapFromDirectory(File dir_) throws IOException
{
final String[] csvFileNames = dir_.list(CSVUtil.CSV_FILTER);
final HashMap> results =
new HashMap>();
for (String fileName : csvFileNames)
{
// Strip the filename in the array name to use
results.put(fileName.substring(0,fileName.length()-4), readArrayFromDir(fileName, dir_));
}
return results;
}
@Override
public Map> getStruct(String name_) throws IOException
{
return readMapFromDirectory(new File(_dir,name_));
}
}