Class CSVProcessor<T>
- java.lang.Object
-
- science.aist.jack.persistence.filesystem.implementation.CSVProcessor<T>
-
- Direct Known Subclasses:
ReflectionCSVProcessor
public class CSVProcessor<T> extends Object implements CSVReader<T>, CSVWriter<T>
Class for accessing domain classes in CSV files. Allows to read and write .csv files containing domain types
- Since:
- 2.0
- Author:
- Christoph Praschl
-
-
Field Summary
Fields Modifier and Type Field Description protected List<String>
columnDefinition
protected BiFunction<List<String>,List<String>,T>
columnsToElementFunc
protected BiFunction<T,List<String>,List<String>>
elementToFunc
protected char
separator
-
Constructor Summary
Constructors Modifier Constructor Description protected
CSVProcessor(char separator, List<String> columnDefinition)
protected
CSVProcessor(char separator, List<String> columnDefinition, BiFunction<T,List<String>,List<String>> elementToColumnFunc, BiFunction<List<String>,List<String>,T> columnsToElementFunc)
Creates a CSVPointProcessor with given separator and column definition
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <O> CSVProcessor<O>
getProcessor(char separator, List<String> columnDefinition, BiFunction<O,List<String>,List<String>> elementToColumnFunc, BiFunction<List<String>,List<String>,O> columnsToElementFunc)
Factory method for creating a CSVProcessor object for writing and reading CSV files This method requires a lot of manual handling using the BiFunctions.static <O> CSVProcessor<O>
getProcessor(char separator, List<String> columnDefinition, BiFunction<O,List<String>,List<String>> elementToColumnFunc, Supplier<O> elementSupplier, TriConsumer<O,String,String> fieldConsumer)
Factory method for creating a CSVProcessor object for writing and reading CSV files with a fieldconsumerstatic <O> CSVReader<O>
getReader(char separator, List<String> columnDefinition, @NonNull BiFunction<List<String>,List<String>,O> columnsToElementFunc)
Factory method for creating a CSVProcessor object for reading CSV files This method requires a lot of manual handling using the BiFunctions.static <O> CSVReader<O>
getReader(char separator, List<String> columnDefinition, @NonNull Supplier<O> elementSupplier, @NonNull TriConsumer<O,String,String> fieldConsumer)
Factory method for creating a CSVProcessor object for reading CSV files with a fieldconsumerstatic <O> CSVWriter<O>
getWriter(char separator, List<String> columnDefinition, @NonNull BiFunction<O,List<String>,List<String>> elementToColumnFunc)
Factory method for creating a CSVProcessor object for writing CSV files;protected List<String>
normalizeColumnDefinition(List<String> columnHeader)
Defines the normalisation of the header files.protected List<String>
normalizeRow(List<String> csvLine)
Defines the normalisation of a singular csv line after the header.List<T>
read(File csvFile, boolean containsColumnDefinition, boolean useFileColumnDefinition)
Method for reading the given csv file and converting the lines to the specific element type using the givenBiFunction
boolean
write(File targetFile, List<T> elements, boolean writeColumnDefinition)
Method for writing the given elements to a csv file
-
-
-
Constructor Detail
-
CSVProcessor
protected CSVProcessor(char separator, List<String> columnDefinition, BiFunction<T,List<String>,List<String>> elementToColumnFunc, BiFunction<List<String>,List<String>,T> columnsToElementFunc)
Creates a CSVPointProcessor with given separator and column definition- Parameters:
separator
- separator symbol used in csv filecolumnDefinition
- column definition of the csv file (can be null for reading if the CSV file contains a header which is used inread(File, boolean, boolean)
)elementToColumnFunc
- function to convert the elements to a csv line. First parameter == current element; second parameter == columnDefinition; Returns the list of strings representing the columns in the csv filecolumnsToElementFunc
- function to convert the string columns of a csv line to a element
-
-
Method Detail
-
getWriter
public static <O> CSVWriter<O> getWriter(char separator, List<String> columnDefinition, @NonNull @NonNull BiFunction<O,List<String>,List<String>> elementToColumnFunc)
Factory method for creating a CSVProcessor object for writing CSV files;- Type Parameters:
O
- element type- Parameters:
separator
- separator symbol used in csv filecolumnDefinition
- column definition of the csv file (can be null for reading if the CSV file contains a header which is used inread(File, boolean, boolean)
)elementToColumnFunc
- function to convert the elements to a csv line which should be written. First parameter == current element; second parameter == columnDefinition; Returns the list of strings representing the columns in the csv file- Returns:
- CSV Processor
-
getReader
public static <O> CSVReader<O> getReader(char separator, List<String> columnDefinition, @NonNull @NonNull Supplier<O> elementSupplier, @NonNull @NonNull TriConsumer<O,String,String> fieldConsumer)
Factory method for creating a CSVProcessor object for reading CSV files with a fieldconsumer- Type Parameters:
O
- element type- Parameters:
separator
- separator symbol used in csv filecolumnDefinition
- column definition of the csv file (can be null for reading if the CSV file contains a header which is used inread(File, boolean, boolean)
)elementSupplier
- supplies the initial object which will be modified using the given fieldConsumerfieldConsumer
- consumes the supplied object (created with elementSupplier), the actual column value (second parameter) and the associated column name (third parameter). Should modify the supplied object using the given column- Returns:
- CSV Processor
-
getReader
public static <O> CSVReader<O> getReader(char separator, List<String> columnDefinition, @NonNull @NonNull BiFunction<List<String>,List<String>,O> columnsToElementFunc)
Factory method for creating a CSVProcessor object for reading CSV files This method requires a lot of manual handling using the BiFunctions. UsegetReader(char, List, Supplier, TriConsumer)
for a more convenient way.- Type Parameters:
O
- element type- Parameters:
separator
- separator symbol used in csv filecolumnDefinition
- column definition of the csv file (can be null for reading if the CSV file contains a header which is used inread(File, boolean, boolean)
)columnsToElementFunc
- function to convert the read string columns of a csv line to a element- Returns:
- CSV Processor
-
getProcessor
public static <O> CSVProcessor<O> getProcessor(char separator, List<String> columnDefinition, BiFunction<O,List<String>,List<String>> elementToColumnFunc, BiFunction<List<String>,List<String>,O> columnsToElementFunc)
Factory method for creating a CSVProcessor object for writing and reading CSV files This method requires a lot of manual handling using the BiFunctions. UsegetProcessor(char, List, BiFunction, Supplier, TriConsumer)
for a more convenient way.- Type Parameters:
O
- element type- Parameters:
separator
- separator symbol used in csv filecolumnDefinition
- column definition of the csv file (can be null for reading if the CSV file contains a header which is used inread(File, boolean, boolean)
)elementToColumnFunc
- function to convert the elements to a csv line which should be written. First parameter == current element; second parameter == columnDefinition; Returns the list of strings representing the columns in the csv filecolumnsToElementFunc
- function to convert the read string columns of a csv line to a element- Returns:
- CSV Processor
-
getProcessor
public static <O> CSVProcessor<O> getProcessor(char separator, List<String> columnDefinition, BiFunction<O,List<String>,List<String>> elementToColumnFunc, Supplier<O> elementSupplier, TriConsumer<O,String,String> fieldConsumer)
Factory method for creating a CSVProcessor object for writing and reading CSV files with a fieldconsumer- Type Parameters:
O
- element type- Parameters:
separator
- separator symbol used in csv filecolumnDefinition
- column definition of the csv file (can be null for reading if the CSV file contains a header which is used inread(File, boolean, boolean)
)elementToColumnFunc
- function to convert the elements to a csv line which should be written. First parameter == current element; second parameter == columnDefinition; Returns the list of strings representing the columns in the csv fileelementSupplier
- supplies the initial object which will be modified using the given fieldConsumerfieldConsumer
- consumes the supplied object (created with elementSupplier), the actual column value (second parameter) and the associated column name (third parameter). Should modify the supplied object using the given column- Returns:
- CSV Processor
-
normalizeColumnDefinition
protected List<String> normalizeColumnDefinition(List<String> columnHeader)
Defines the normalisation of the header files. Adapter method- Parameters:
columnHeader
- the split header definition of the csv file- Returns:
- the normalized column header
-
normalizeRow
protected List<String> normalizeRow(List<String> csvLine)
Defines the normalisation of a singular csv line after the header. Adapter method- Parameters:
csvLine
- the split line of a csv file.- Returns:
- the normalized csv lines
-
read
public List<T> read(File csvFile, boolean containsColumnDefinition, boolean useFileColumnDefinition)
Method for reading the given csv file and converting the lines to the specific element type using the givenBiFunction
- Specified by:
read
in interfaceCSVReader<T>
- Parameters:
csvFile
- source file to readcontainsColumnDefinition
- flag which signals if the file contains a column definitionuseFileColumnDefinition
- flag if the column definition in the csv file should be used instead of the definition in the constructor (only used if containsColumndefinition == true)- Returns:
- the read elements
-
-