Class CSVProcessor<T>

  • All Implemented Interfaces:
    CSVReader<T>, CSVWriter<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
    • 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 file
        columnDefinition - column definition of the csv file (can be null for reading if the CSV file contains a header which is used in read(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 file
        columnsToElementFunc - function to convert the string columns of a csv line to a element
      • CSVProcessor

        protected CSVProcessor​(char separator,
                               List<String> columnDefinition)
    • 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 file
        columnDefinition - column definition of the csv file (can be null for reading if the CSV file contains a header which is used in read(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 file
        columnDefinition - column definition of the csv file (can be null for reading if the CSV file contains a header which is used in read(File, boolean, boolean))
        elementSupplier - supplies the initial object which will be modified using the given fieldConsumer
        fieldConsumer - 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. Use getReader(char, List, Supplier, TriConsumer) for a more convenient way.
        Type Parameters:
        O - element type
        Parameters:
        separator - separator symbol used in csv file
        columnDefinition - column definition of the csv file (can be null for reading if the CSV file contains a header which is used in read(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. Use getProcessor(char, List, BiFunction, Supplier, TriConsumer) for a more convenient way.
        Type Parameters:
        O - element type
        Parameters:
        separator - separator symbol used in csv file
        columnDefinition - column definition of the csv file (can be null for reading if the CSV file contains a header which is used in read(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
        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,
                                                       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 file
        columnDefinition - column definition of the csv file (can be null for reading if the CSV file contains a header which is used in read(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
        elementSupplier - supplies the initial object which will be modified using the given fieldConsumer
        fieldConsumer - 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 given BiFunction
        Specified by:
        read in interface CSVReader<T>
        Parameters:
        csvFile - source file to read
        containsColumnDefinition - flag which signals if the file contains a column definition
        useFileColumnDefinition - 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
      • write

        public boolean write​(File targetFile,
                             List<T> elements,
                             boolean writeColumnDefinition)
        Method for writing the given elements to a csv file
        Specified by:
        write in interface CSVWriter<T>
        Parameters:
        targetFile - target file where to write the elements
        elements - to be written
        writeColumnDefinition - flag which signals if the column definition should be written in the beginning
        Returns:
        true iff elements are written successfully