Class CsvBuilderImpl

    • Constructor Detail

      • CsvBuilderImpl

        public CsvBuilderImpl​()
    • Method Detail

      • setTrim

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

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

        public void setRecord​(String aRecord)
        Sets the CSV line for the CSV line property.
        Specified by:
        setRecord in interface CsvBuilder
        Parameters:
        aRecord - The CSV line to be stored by the CSV line property.
      • getRecord

        public String getRecord​()
        Retrieves the CSV line from the CSV line property.
        Specified by:
        getRecord in interface CsvBuilder
        Returns:
        The CSV line stored by the CSV line property.
      • setFields

        public void setFields​(Object... aFields)
        Sets the CSV elements for the CSV elements property.
        Specified by:
        setFields in interface CsvBuilder
        Parameters:
        aFields - The CSV elements to be stored by the CSV elements property.
      • getFields

        public Collection<Object> getFields​()
        Retrieves the CSV elements from the CSV elements property.
        Specified by:
        getFields in interface CsvBuilder
        Returns:
        The CSV elements stored by the CSV elements property.
      • setFields

        public void setFields​(Collection<Object> aFields)
        Sets the CSV elements for the CSV elements property.
        Specified by:
        setFields in interface CsvBuilder
        Parameters:
        aFields - The CSV elements to be stored by the CSV elements property.
      • getDelimiter

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

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

        public String toRecord​(Object... aFields)
        Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method). Still changing for example the delimiter via CsvBuilder.withDelimiter(char) can cause side effects! For avoiding thread race conditions / side effects regarding the fields (and the resulting record), use this method instead of the combination of CsvBuilder.withFields(String...) with CsvBuilder.toRecord().
        Specified by:
        toRecord in interface CsvBuilder
        Parameters:
        aFields - The CSV elements to be converted into a record.
        Returns:
        The CSV line created from the fields.
      • toRecord

        public String toRecord​(Collection<Object> aFields)
        Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method). Still changing for example the delimiter via CsvBuilder.withDelimiter(char) can cause side effects! For avoiding thread race conditions / side effects regarding the fields (and the resulting record), use this method instead of the combination of CsvBuilder.withFields(String...) with CsvBuilder.toRecord().
        Specified by:
        toRecord in interface CsvBuilder
        Parameters:
        aFields - The CSV elements to be converted into a record.
        Returns:
        The CSV line created from the fields.
      • toRecord

        public String toRecord​(String... aFields)
                        throws IllegalStateException
        Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method). Still changing for example the delimiter via CsvBuilder.withDelimiter(char) can cause side effects! For avoiding thread race conditions / side effects regarding the fields (and the resulting record), use this method instead of the combination of CsvBuilder.withFields(String...) with CsvBuilder.toRecord().
        Specified by:
        toRecord in interface CsvBuilder
        Parameters:
        aFields - The CSV elements to be converted into a record.
        Returns:
        The CSV line created from the fields.
        Throws:
        IllegalStateException
      • toStrings

        public String[] toStrings​(String aRecord)
        Convenience method for the CsvBuilder.toFields(String) method.
        Specified by:
        toStrings in interface CsvBuilder
        Parameters:
        aRecord - The CSV line to be stored by the CSV line property.
        Returns:
        The elements created from the CSV line or null if the record is a comment.
      • setCommentPrefixes

        public void setCommentPrefixes​(String... aCommentPrefixes)
        Sets the prefixes used to identify CSV lines to be ignored.
        Specified by:
        setCommentPrefixes in interface CsvMixin
        Parameters:
        aCommentPrefixes - The prefixes used to identify CSV lines to be ignored.
      • getCommentPrefixes

        public String[] getCommentPrefixes​()
        Gets the prefixes used to identify CSV lines to be ignored.
        Specified by:
        getCommentPrefixes in interface CsvMixin
        Returns:
        return The prefixes used to identify CSV lines to be ignored.
      • clearCommentPrefixes

        public void clearCommentPrefixes​()
        Clears the prefixes used to identify CSV lines to be ignored. No CSV lines will be ignored any more.
        Specified by:
        clearCommentPrefixes in interface CsvMixin
      • fromSeparatedValues

        protected static List<String> fromSeparatedValues​(String aRecord,
                                                          char aDelimiter,
                                                          CsvEscapeMode aCsvEscapeMode,
                                                          boolean isTrimRecords)
        From separated values.
        Parameters:
        aRecord - the record
        aDelimiter - the delimiter
        aCsvEscapeMode - the csv escape mode
        isTrimRecords - When true, then leading and trailing whitespaces are trimmed.
        Returns:
        the list
      • toSeparatedValues

        protected static String toSeparatedValues​(Collection<Object> aFields,
                                                  char aDelimiter,
                                                  CsvEscapeMode aCsvEscapeMode)
        To separated values.
        Parameters:
        aFields - the fields
        aDelimiter - the delimiter
        aCsvEscapeMode - the csv escape mode
        Returns:
        the string
      • toSeparatedValues

        protected static String toSeparatedValues​(Collection<?> aCollection)
        Takes the collection and creates a single String with the according Delimiter.CSV delimiter. Unescaping any escaped chars is taken care of by this method, for example an unescaped delimiter char is escaped in the result.
        Parameters:
        aCollection - The collection to be converted to a single CSV String.
        Returns:
        The single CSV String from the collection.
      • toSeparatedValues

        protected static String toSeparatedValues​(Collection<?> aCollection,
                                                  char aDelimiter)
        Takes the collection and creates a single String with the provided delimiter. Unescaping any escaped chars is taken care of by this method, for example an unescaped delimiter char is escaped in the result.
        Parameters:
        aCollection - The collection to be converted to a single CSV String.
        aDelimiter - The delimiter to use for creating the single CSV String.
        Returns:
        The single CSV String from the collection.
      • toSeparatedValue

        protected static String toSeparatedValue​(Object aValue)
        Escapes a single value according to the rules of CSV like separated values with the according Delimiter.CSV delimiter.
        Parameters:
        aValue - The value which's String representation is to be escaped accordingly.
        Returns:
        A single value escaped according to the rules of CSV like separated values.
      • toSeparatedValue

        protected static String toSeparatedValue​(Object aValue,
                                                 char aDelimiter)
        Escapes a single value according to the rules of CSV like separated values.
        Parameters:
        aValue - The value which's String representation is to be escaped accordingly.
        aDelimiter - The delimiter to use for creating the single CSV String.
        Returns:
        A single value escaped according to the rules of CSV like separated values.
      • fromSeparatedValues

        protected static List<String> fromSeparatedValues​(String aSeparatedValues)
        Takes the String and creates a List with String instances containing the elements as identified from the String by splitting the String at the delimiter positions and adding those chunks to the List with the according Delimiter.CSV delimiter.. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.
        Parameters:
        aSeparatedValues - The String representing the CSV String where contained elements are separated from each other by the given delimiter.
        Returns:
        The List containing the elements as identified from the given CSV String.
      • fromSeparatedValues

        protected static List<String> fromSeparatedValues​(String aSeparatedValues,
                                                          char aDelimiter)
        Takes the String and creates a List with String instances containing the elements as identified from the String by splitting the String at the delimiter positions and adding those chunks to the List. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.
        Parameters:
        aSeparatedValues - The String representing the CSV String where contained elements are separated from each other by the given delimiter.
        aDelimiter - The delimiter used for separating the elements if the single CSV String.
        Returns:
        The List containing the elements as identified from the given CSV String.
      • toSplitValues

        protected static String toSplitValues​(Object[] aArray)
        Takes the array's String representation and creates a single String with the according Delimiter.CSV delimiter. No escaping is taken care of, for escaping either use the according toSeparatedValues(Object[]) method or do escaping manually.
        Parameters:
        aArray - The array to be converted to a single CSV String.
        Returns:
        The single CSV String from the String array.
      • toSplitValues

        protected static String toSplitValues​(Object[] aArray,
                                              char aDelimiter)
        Takes the array's String representation and creates a single String with the provided delimiter. No escaping is taken of, for escaping either use the according toSeparatedValues(Object[], char) method or do escaping manually.
        Parameters:
        aArray - The array to be converted to a single CSV String.
        aDelimiter - The delimiter to use for creating the single CSV String.
        Returns:
        The single CSV String from the String array.
      • toSplitValue

        protected static String toSplitValue​(Object aValue,
                                             char aDelimiter)
        Creates the String representation of a single split value not applying any escaping.
        Parameters:
        aValue - The value which's String representation is to be retrieved.
        aDelimiter - The delimiter to use for creating the single CSV String.
        Returns:
        A single value not escaped as required by plain split values.
      • fromSplitValues

        protected static List<String> fromSplitValues​(String aSplitValues)
        Takes the String and creates a List with String instances containing the elements as identified from the String by splitting the String at the delimiter positions and adding those chunks to the List with the according Delimiter.CSV delimiter.. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.
        Parameters:
        aSplitValues - The String representing the CSV String where contained elements are split from each other by the given delimiter.
        Returns:
        The List containing the elements as identified from the given CSV String.
      • fromSplitValues

        protected static List<String> fromSplitValues​(String aSplitValues,
                                                      char aDelimiter)
        Takes the String and creates a List with String instances containing the elements as identified from the String by splitting the String at the delimiter positions and adding those chunks to the List. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.
        Parameters:
        aSplitValues - The String representing the CSV String where contained elements are split from each other by the given delimiter.
        aDelimiter - The delimiter used for separating the elements if the single CSV String.
        Returns:
        The List containing the elements as identified from the given CSV String.
      • fromSplitValues

        protected static List<String> fromSplitValues​(String aValues,
                                                      String aDelimiters)
        Takes the String and creates a List with String instances containing the elements as identified from the String by splitting the String at the delimiter positions and adding those chunks to the List. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.
        Parameters:
        aValues - The String representing the CSV String where contained elements are separated from each other by the given delimiter.
        aDelimiters - The delimiter characters used for separating the elements.
        Returns:
        The List containing the elements as identified from the given CSV String.
      • toSeparatedValues

        protected static String toSeparatedValues​(Object[] aArray)
        Takes the array's String representation and creates a single String with the according Delimiter.CSV delimiter. Unescaping any escaped chars is taken care of by this method, for example an unescaped delimiter char is escaped in the result.
        Parameters:
        aArray - The array to be converted to a single CSV String.
        Returns:
        The single CSV String from the String array.
      • toSeparatedValues

        protected static String toSeparatedValues​(Object[] aArray,
                                                  char aDelimiter)
        Takes the array's String representation and creates a single String with the provided delimiter. Unescaping any escaped chars is taken care of by this method, for example an unescaped delimiter char is escaped in the result.
        Parameters:
        aArray - The array to be converted to a single CSV String.
        aDelimiter - The delimiter to use for creating the single CSV String.
        Returns:
        The single CSV String from the String array.
      • toEscapedRegexp

        protected static String toEscapedRegexp​(char aDelimiter)
        To escaped regexp.
        Parameters:
        aDelimiter - the delimiter
        Returns:
        the string