Class CsvRecordsWriter<T>

  • Type Parameters:
    T - The type of the Record instances being used.
    All Implemented Interfaces:
    java.lang.AutoCloseable, org.refcodes.mixin.DelimiterAccessor, org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<T>, org.refcodes.mixin.DelimiterAccessor.DelimiterMutator, org.refcodes.mixin.DelimiterAccessor.DelimiterProperty, org.refcodes.mixin.TrimAccessor, org.refcodes.mixin.TrimAccessor.TrimBuilder<T>, org.refcodes.mixin.TrimAccessor.TrimMutator, org.refcodes.mixin.TrimAccessor.TrimProperty, HeaderAccessor<T>, RecordsWriter<T>, org.refcodes.textual.CsvEscapeModeAccessor, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeBuilder<T>, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeMutator, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeProperty, org.refcodes.textual.CsvMixin
    Direct Known Subclasses:
    CsvStringRecordsWriter


    public class CsvRecordsWriter<T>
    extends java.lang.Object
    implements org.refcodes.textual.CsvMixin, RecordsWriter<T>
    The CsvRecordsWriter writes CSV-Data.
    • Field Detail

      • _header

        protected Header<T> _header
      • _outputStream

        protected java.io.PrintStream _outputStream
      • _csvBuilder

        protected org.refcodes.textual.CsvBuilder _csvBuilder
    • Constructor Detail

      • CsvRecordsWriter

        public CsvRecordsWriter​(Header<T> aHeader,
                                java.io.File aFile)
                         throws java.io.FileNotFoundException
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aFile - The File to be used for printing.
        Throws:
        java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
      • CsvRecordsWriter

        public CsvRecordsWriter​(Header<T> aHeader,
                                java.io.File aFile,
                                char aCsvDelimiter)
                         throws java.io.FileNotFoundException
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aFile - The File to be used for printing.
        aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
        Throws:
        java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
      • CsvRecordsWriter

        public CsvRecordsWriter​(java.io.File aFile)
                         throws java.io.FileNotFoundException
        Constructs a CsvRecordsWriter.
        Parameters:
        aFile - The File to be used for printing output.
        Throws:
        java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
      • CsvRecordsWriter

        public CsvRecordsWriter​(java.io.File aFile,
                                char aCsvDelimiter)
                         throws java.io.FileNotFoundException
        Constructs a CsvRecordsWriter.
        Parameters:
        aFile - The File to be used for printing output. aCsvDelimiter The separator to be used when separating the CSV values in the log output.
        aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
        Throws:
        java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
      • CsvRecordsWriter

        public CsvRecordsWriter​(Header<T> aHeader,
                                java.io.OutputStream aOutputStream)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aOutputStream - The OutputStream to be used for printing.
      • CsvRecordsWriter

        public CsvRecordsWriter​(Header<T> aHeader,
                                java.io.OutputStream aOutputStream,
                                char aCsvDelimiter)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aOutputStream - The OutputStream to be used for printing.
        aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      • CsvRecordsWriter

        public CsvRecordsWriter​(java.io.OutputStream aOutputStream)
        Constructs a CsvRecordsWriter.
        Parameters:
        aOutputStream - The OutputStream to be used for printing output.
      • CsvRecordsWriter

        public CsvRecordsWriter​(java.io.PrintStream aPrintStream)
        Constructs a CsvRecordsWriter.
        Parameters:
        aPrintStream - The PrintStream to be used for printing.
      • CsvRecordsWriter

        public CsvRecordsWriter​(Header<T> aHeader,
                                java.io.PrintStream aPrintStream)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aPrintStream - The PrintStream to be used for printing.
      • CsvRecordsWriter

        public CsvRecordsWriter​(Header<T> aHeader,
                                java.io.PrintStream aPrintStream,
                                char aCsvDelimiter)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aPrintStream - The PrintStream to be used for printing.
        aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      • CsvRecordsWriter

        public CsvRecordsWriter​(ColumnFactory<T> aColumnFactory,
                                java.io.File aFile)
                         throws java.io.FileNotFoundException
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aFile - The File to be used for printing.
        Throws:
        java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
      • CsvRecordsWriter

        public CsvRecordsWriter​(ColumnFactory<T> aColumnFactory,
                                java.io.File aFile,
                                char aCsvDelimiter)
                         throws java.io.FileNotFoundException
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aFile - The File to be used for printing.
        aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
        Throws:
        java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
      • CsvRecordsWriter

        public CsvRecordsWriter​(ColumnFactory<T> aColumnFactory,
                                java.io.OutputStream aOutputStream)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aOutputStream - The OutputStream to be used for printing.
      • CsvRecordsWriter

        public CsvRecordsWriter​(ColumnFactory<T> aColumnFactory,
                                java.io.OutputStream aOutputStream,
                                char aCsvDelimiter)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aOutputStream - The OutputStream to be used for printing.
        aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      • CsvRecordsWriter

        public CsvRecordsWriter​(ColumnFactory<T> aColumnFactory,
                                java.io.PrintStream aPrintStream)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aPrintStream - The PrintStream to be used for printing.
      • CsvRecordsWriter

        public CsvRecordsWriter​(ColumnFactory<T> aColumnFactory,
                                java.io.PrintStream aOutputStream,
                                char aCsvDelimiter)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aOutputStream - The PrintStream to be used for printing output.
        aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      • CsvRecordsWriter

        protected CsvRecordsWriter​(Header<T> aHeader,
                                   ColumnFactory<T> aColumnFactory,
                                   java.io.PrintStream aOutputStream,
                                   char aCsvDelimiter)
        Constructs a CsvRecordsWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aOutputStream - The PrintStream to be used for printing output.
        aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
    • Method Detail

      • setCommentPrefixes

        public void setCommentPrefixes​(java.lang.String... aCommentPrefixes)
        Specified by:
        setCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • getCommentPrefixes

        public java.lang.String[] getCommentPrefixes​()
        Specified by:
        getCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • clearCommentPrefixes

        public void clearCommentPrefixes​()
        Specified by:
        clearCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • withCommentPrefixes

        public org.refcodes.textual.CsvMixin withCommentPrefixes​(java.lang.String... aCommentPrefixes)
        Specified by:
        withCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • close

        public void close​()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException
      • getCsvEscapeMode

        public org.refcodes.textual.CsvEscapeMode getCsvEscapeMode​()
        Specified by:
        getCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor
      • isTrim

        public boolean isTrim​()
        Specified by:
        isTrim in interface org.refcodes.mixin.TrimAccessor
      • getDelimiter

        public char getDelimiter​()
        Specified by:
        getDelimiter in interface org.refcodes.mixin.DelimiterAccessor
      • setTrim

        public void setTrim​(boolean isTrim)
        Specified by:
        setTrim in interface org.refcodes.mixin.TrimAccessor.TrimMutator
      • setDelimiter

        public void setDelimiter​(char aCsvDelimiter)
        Specified by:
        setDelimiter in interface org.refcodes.mixin.DelimiterAccessor.DelimiterMutator
      • withTrim

        public CsvRecordsWriter<T> withTrim​(boolean isTrimRecords)
        Specified by:
        withTrim in interface org.refcodes.mixin.TrimAccessor.TrimBuilder<T>
      • withCsvEscapeMode

        public CsvRecordsWriter<T> withCsvEscapeMode​(org.refcodes.textual.CsvEscapeMode aCsvEscapeMode)
        Specified by:
        withCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeBuilder<T>
      • withDelimiter

        public CsvRecordsWriter<T> withDelimiter​(char aCsvDelimiter)
        Specified by:
        withDelimiter in interface org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<T>
      • setCsvEscapeMode

        public void setCsvEscapeMode​(org.refcodes.textual.CsvEscapeMode aCsvEscapeMode)
        Specified by:
        setCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeMutator
      • parseHeader

        public void parseHeader​(java.lang.String... aHeader)
        Parses and sets the Header from the provided String. You must have provided a ColumnFactory for this operation to succeed.
        Specified by:
        parseHeader in interface RecordsWriter<T>
        Parameters:
        aHeader - The header to be created.
      • writeHeader

        public void writeHeader​(java.lang.String... aHeader)
        Parses and sets the Header from the provided String arguments and writes the CSV header to the (standard) output (be it a Stream or a File). You must have provided a ColumnFactory for this operation to succeed.
        Specified by:
        writeHeader in interface RecordsWriter<T>
        Parameters:
        aHeader - The header to be created.
      • writeHeader

        public void writeHeader​(Header<T> aHeader)
        Sets and writes the provided Header to the (standard) output (be it a Stream or a File). .
        Specified by:
        writeHeader in interface RecordsWriter<T>
        Parameters:
        aHeader - The header to be created.
      • writeHeader

        public void writeHeader​()
        Writes the CSV header to the (standard) output (be it a Stream or a File). You must have provided a Header for this operation to succeed.
        Specified by:
        writeHeader in interface RecordsWriter<T>
      • writeNext

        public void writeNext​(java.lang.Object... aRecord)
                       throws java.lang.IllegalArgumentException
        Directly logs the provided String instances to the (standard) output.
        Specified by:
        writeNext in interface RecordsWriter<T>
        Parameters:
        aRecord - The String instances to be logged.
        Throws:
        java.lang.IllegalArgumentException - thrown in case the provided objects do not align with the header (as of HeaderAccessor.getHeader()).
      • writeComment

        public void writeComment​(java.lang.String aComment)
                          throws java.lang.IllegalStateException
        Writes a comment by using the fist defined comment prefix as of getCommentPrefixes() suffixed with a space (" ") followed by the comment (as of the CsvMixin.toComment(String)). If no prefix has been set, then an IllegalStateException will be thrown.
        Parameters:
        aComment - The comment to be written.
        Throws:
        java.lang.IllegalStateException - in case no comment prefixes have been set via setCommentPrefixes(String...) or withCommentPrefixes(String...).