Class CsvPreference.Builder

  • Enclosing class:
    CsvPreference

    public static class CsvPreference.Builder
    extends java.lang.Object
    Builds immutable CsvPreference instances. The builder pattern allows for additional preferences to be added in the future.
    • Constructor Detail

      • Builder

        public Builder​(CsvPreference preference)
        Constructs a Builder with all of the values from an existing CsvPreference instance. Useful if you want to base your preferences off one of the existing CsvPreference constants.
        Parameters:
        preference - the existing preference
      • Builder

        public Builder​(char quoteChar,
                       int delimiterChar,
                       java.lang.String endOfLineSymbols)
        Constructs a Builder with the mandatory preference values.
        Parameters:
        quoteChar - matching pairs of this character are used to escape columns containing the delimiter
        delimiterChar - the character separating each column
        endOfLineSymbols - one or more symbols terminating the line, e.g. "\n". Only used for writing.
        Throws:
        java.lang.IllegalArgumentException - if quoteChar and delimiterChar are the same character
        java.lang.NullPointerException - if endOfLineSymbols is null
    • Method Detail

      • surroundingSpacesNeedQuotes

        public CsvPreference.Builder surroundingSpacesNeedQuotes​(boolean surroundingSpacesNeedQuotes)
        Flag indicating whether spaces at the beginning or end of a cell should be ignored if they're not surrounded by quotes (applicable to both reading and writing CSV). The default is false, as spaces "are considered part of a field and should not be ignored" according to RFC 4180.
        Parameters:
        surroundingSpacesNeedQuotes - flag indicating whether spaces at the beginning or end of a cell should be ignored if they're not surrounded by quotes
        Returns:
        the updated Builder
        Since:
        2.0.0
      • ignoreEmptyLines

        public CsvPreference.Builder ignoreEmptyLines​(boolean ignoreEmptyLines)
        Flag indicating whether empty lines (i.e. containing only end of line symbols) should be ignored. The default is true.
        Parameters:
        ignoreEmptyLines - flag indicating whether empty lines should be ignored
        Returns:
        the updated Builder
        Since:
        2.2.1
      • skipComments

        public CsvPreference.Builder skipComments​(CommentMatcher commentMatcher)
        Enables the skipping of comments. You can supply your own comment matcher or use one of the predefined ones: CommentStartsWith or CommentMatches
        Parameters:
        commentMatcher - the comment matcher to use
        Returns:
        the updated Builder
        Throws:
        java.lang.NullPointerException - if commentMatcher is null
        Since:
        2.1.0
      • useEncoder

        public CsvPreference.Builder useEncoder​(CsvEncoder encoder)
        Uses a custom CsvEncoder to escape CSV for writing.
        Parameters:
        encoder - the custom encoder
        Returns:
        the updated Builder
        Throws:
        java.lang.NullPointerException - if encoder is null
        Since:
        2.1.0
      • useQuoteMode

        public CsvPreference.Builder useQuoteMode​(QuoteMode quoteMode)
        Uses a custom QuoteMode to determine if surrounding quotes should be applied when writing (only applicable if a column doesn't contain any special characters and wouldn't otherwise be quoted). You can supply your own quote mode or use one of the predefined ones: AlwaysQuoteMode or ColumnQuoteMode
        Parameters:
        quoteMode - the quote mode
        Returns:
        the updated Builder
        Throws:
        java.lang.NullPointerException - if quoteMode is null
        Since:
        2.1.0
      • maxLinesPerRow

        public CsvPreference.Builder maxLinesPerRow​(int maxLinesPerRow)
        The maximum number of lines that a row can span before an exception is thrown (only applicable when reading CSV). This option allows CSV readers to fail fast when encountering CSV with mismatching quotes - the normal behaviour would be to continue reading until the matching quote is found, which could potentially mean reading the whole file (and exhausting all available memory). Zero or a negative value will disable this option. The default is 0.
        Parameters:
        maxLinesPerRow - the maximum number of lines a row can span before an exception is thrown
        Returns:
        the updated Builder
        Since:
        2.4.0
      • build

        public CsvPreference build()
        Builds the CsvPreference instance.
        Returns:
        the immutable CsvPreference instance