Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 854312004C8 for ; Mon, 9 May 2016 19:28:16 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 83CA416099C; Mon, 9 May 2016 17:28:16 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 3D672160A1A for ; Mon, 9 May 2016 19:28:14 +0200 (CEST) Received: (qmail 52500 invoked by uid 500); 9 May 2016 17:28:13 -0000 Mailing-List: contact notifications-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list notifications@commons.apache.org Received: (qmail 52453 invoked by uid 99); 9 May 2016 17:28:13 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 May 2016 17:28:13 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 1B916C0DF7 for ; Mon, 9 May 2016 17:28:12 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.009 X-Spam-Level: * X-Spam-Status: No, score=1.009 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.001, T_FILL_THIS_FORM_SHORT=0.01] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id D2hr4jqwr7bb for ; Mon, 9 May 2016 17:27:53 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id ACC9F5F396 for ; Mon, 9 May 2016 17:27:52 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 68A4DE58BA for ; Mon, 9 May 2016 17:27:49 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 4DE793A043C for ; Mon, 9 May 2016 17:27:49 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r987826 [25/48] - in /websites/production/commons/content/proper/commons-csv: ./ apidocs/ apidocs/org/apache/commons/csv/ apidocs/org/apache/commons/csv/class-use/ apidocs/src-html/org/apache/commons/csv/ css/ jacoco/ jacoco/.resources/ jac... Date: Mon, 09 May 2016 17:27:47 -0000 To: notifications@commons.apache.org From: britter@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160509172749.4DE793A043C@svn01-us-west.apache.org> archived-at: Mon, 09 May 2016 17:28:16 -0000 Modified: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVFormat.java.html ============================================================================== --- websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVFormat.java.html (original) +++ websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVFormat.java.html Mon May 9 17:27:44 2016 @@ -23,6 +23,7 @@ import static org.apache.commons.csv.Con import static org.apache.commons.csv.Constants.CRLF; import static org.apache.commons.csv.Constants.DOUBLE_QUOTE_CHAR; import static org.apache.commons.csv.Constants.LF; +import static org.apache.commons.csv.Constants.PIPE; import static org.apache.commons.csv.Constants.TAB; import java.io.IOException; @@ -143,73 +144,69 @@ import java.util.Set; * This class is immutable. * </p> * - * @version $Id: CSVFormat.java 1695190 2015-08-11 02:14:33Z ggregory $ + * @version $Id: CSVFormat.java 1742506 2016-05-06 07:07:43Z britter $ */ public final class CSVFormat implements Serializable { /** * Predefines formats. - * + * * @since 1.2 */ - public static enum Predefined { + public enum Predefined { /** * @see CSVFormat#DEFAULT */ - Default(CSVFormat.DEFAULT), + Default(CSVFormat.DEFAULT), /** * @see CSVFormat#EXCEL */ - Excel(CSVFormat.EXCEL), + Excel(CSVFormat.EXCEL), + + /** + * @see CSVFormat#INFORMIX_UNLOAD + * @since 1.3 + */ + InformixUnload(CSVFormat.INFORMIX_UNLOAD), + + /** + * @see CSVFormat#INFORMIX_UNLOAD_CSV + * @since 1.3 + */ + InformixUnloadCsv(CSVFormat.INFORMIX_UNLOAD_CSV), /** * @see CSVFormat#MYSQL */ - MySQL(CSVFormat.MYSQL), + MySQL(CSVFormat.MYSQL), /** * @see CSVFormat#RFC4180 */ - RFC4180(CSVFormat.RFC4180), + RFC4180(CSVFormat.RFC4180), /** * @see CSVFormat#TDF */ - TDF(CSVFormat.TDF); + TDF(CSVFormat.TDF); private final CSVFormat format; - private Predefined(CSVFormat format) { - this.format = format; - } - + Predefined(final CSVFormat format) { + this.format = format; + } + /** * Gets the format. - * + * * @return the format. */ public CSVFormat getFormat() { - return format; + return format; } - }; - - private static final long serialVersionUID = 1L; - - private final char delimiter; - private final Character quoteCharacter; // null if quoting is disabled - private final QuoteMode quoteMode; - private final Character commentMarker; // null if commenting is disabled - private final Character escapeCharacter; // null if escaping is disabled - private final boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values? - private final boolean allowMissingColumnNames; - private final boolean ignoreEmptyLines; - private final String recordSeparator; // for outputs - private final String nullString; // the string to be used for null values - private final String[] header; // array of header column names - private final String[] headerComments; // array of header comment lines - private final boolean skipHeaderRecord; + } /** * Standard comma separated format, as for {@link #RFC4180} but allowing empty lines. @@ -223,26 +220,11 @@ public final class CSVFormat implements * <li>withRecordSeparator("\r\n")</li> * <li>withIgnoreEmptyLines(true)</li> * </ul> - * @see Predefined#Default - */ - public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, - CRLF, null, null, null, false, false); - - /** - * Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>. * - * <p> - * Settings are: - * </p> - * <ul> - * <li>withDelimiter(',')</li> - * <li>withQuote('"')</li> - * <li>withRecordSeparator("\r\n")</li> - * <li>withIgnoreEmptyLines(false)</li> - * </ul> - * @see Predefined#RFC4180 + * @see Predefined#Default */ - public static final CSVFormat RFC4180 = DEFAULT.withIgnoreEmptyLines(false); + public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, CRLF, + null, null, null, false, false, false, false, false); /** * Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is @@ -270,32 +252,70 @@ public final class CSVFormat implements * Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean) * withAllowMissingColumnNames(true)}. * </p> + * * @see Predefined#Excel */ - public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames(); + public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames(); /** - * Tab-delimited format. + * Default Informix CSV UNLOAD format used by the {@code UNLOAD TO file_name} operation. + * + * <p> + * This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special + * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}. + * </p> * * <p> * Settings are: * </p> * <ul> - * <li>withDelimiter('\t')</li> - * <li>withQuote('"')</li> - * <li>withRecordSeparator("\r\n")</li> - * <li>withIgnoreSurroundingSpaces(true)</li> + * <li>withDelimiter(',')</li> + * <li>withQuote("\"")</li> + * <li>withRecordSeparator('\n')</li> + * <li>withEscape('\\')</li> * </ul> - * @see Predefined#TDF + * + * @see Predefined#MySQL + * @see <a href= + * "http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm"> + * http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm</a> + * @since 1.3 + */ + public static final CSVFormat INFORMIX_UNLOAD = DEFAULT.withDelimiter(PIPE).withEscape(BACKSLASH) + .withQuote(DOUBLE_QUOTE_CHAR).withRecordSeparator(LF); + + /** + * Default Informix CSV UNLOAD format used by the {@code UNLOAD TO file_name} operation. + * + * <p> + * This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special + * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}. + * </p> + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>withDelimiter(',')</li> + * <li>withQuote("\"")</li> + * <li>withRecordSeparator('\n')</li> + * </ul> + * + * @see Predefined#MySQL + * @see <a href= + * "http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm"> + * http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm</a> + * @since 1.3 */ - public static final CSVFormat TDF = DEFAULT.withDelimiter(TAB).withIgnoreSurroundingSpaces(); + public static final CSVFormat INFORMIX_UNLOAD_CSV = DEFAULT.withDelimiter(COMMA).withQuote(DOUBLE_QUOTE_CHAR) + .withRecordSeparator(LF); /** * Default MySQL format used by the {@code SELECT INTO OUTFILE} and {@code LOAD DATA INFILE} operations. * * <p> * This is a tab-delimited format with a LF character as the line separator. Values are not quoted and special - * characters are escaped with '\'. + * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}. * </p> * * <p> @@ -307,14 +327,51 @@ public final class CSVFormat implements * <li>withRecordSeparator('\n')</li> * <li>withIgnoreEmptyLines(false)</li> * <li>withEscape('\\')</li> + * <li>withNullString("\\N")</li> * </ul> * * @see Predefined#MySQL - * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> - * http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a> + * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load + * -data.html</a> */ - public static final CSVFormat MYSQL = DEFAULT.withDelimiter(TAB).withEscape(BACKSLASH).withIgnoreEmptyLines(false) - .withQuote(null).withRecordSeparator(LF); + public static final CSVFormat MYSQL = DEFAULT.withDelimiter(TAB).withEscape(BACKSLASH).withIgnoreEmptyLines(false) + .withQuote(null).withRecordSeparator(LF).withNullString("\\N"); + + /** + * Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>. + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>withDelimiter(',')</li> + * <li>withQuote('"')</li> + * <li>withRecordSeparator("\r\n")</li> + * <li>withIgnoreEmptyLines(false)</li> + * </ul> + * + * @see Predefined#RFC4180 + */ + public static final CSVFormat RFC4180 = DEFAULT.withIgnoreEmptyLines(false); + + private static final long serialVersionUID = 1L; + + /** + * Tab-delimited format. + * + * <p> + * Settings are: + * </p> + * <ul> + * <li>withDelimiter('\t')</li> + * <li>withQuote('"')</li> + * <li>withRecordSeparator("\r\n")</li> + * <li>withIgnoreSurroundingSpaces(true)</li> + * </ul> + * + * @see Predefined#TDF + */ + public static final CSVFormat TDF = DEFAULT.withDelimiter(TAB).withIgnoreSurroundingSpaces(); /** * Returns true if the given character is a line break character. @@ -325,7 +382,7 @@ public final class CSVFormat implements * @return true if <code>c</code> is a line break character */ private static boolean isLineBreak(final char c) { - return c == LF || c == CR; + return c == LF || c == CR; } /** @@ -337,7 +394,7 @@ public final class CSVFormat implements * @return true if <code>c</code> is a line break character (and not null) */ private static boolean isLineBreak(final Character c) { - return c != null && isLineBreak(c.charValue()); + return c != null && isLineBreak(c.charValue()); } /** @@ -361,21 +418,54 @@ public final class CSVFormat implements * @see #TDF */ public static CSVFormat newFormat(final char delimiter) { - return new CSVFormat(delimiter, null, null, null, null, false, false, null, null, null, null, false, false); + return new CSVFormat(delimiter, null, null, null, null, false, false, null, null, null, null, false, false, + false, false, false); } /** * Gets one of the predefined formats from {@link CSVFormat.Predefined}. - * + * * @param format * name * @return one of the predefined formats * @since 1.2 */ public static CSVFormat valueOf(final String format) { - return CSVFormat.Predefined.valueOf(format).getFormat(); + return CSVFormat.Predefined.valueOf(format).getFormat(); } + private final boolean allowMissingColumnNames; + + private final Character commentMarker; // null if commenting is disabled + + private final char delimiter; + + private final Character escapeCharacter; // null if escaping is disabled + + private final String[] header; // array of header column names + + private final String[] headerComments; // array of header comment lines + + private final boolean ignoreEmptyLines; + + private final boolean ignoreHeaderCase; // should ignore header names case + + private final boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values? + + private final String nullString; // the string to be used for null values + + private final Character quoteCharacter; // null if quoting is disabled + + private final QuoteMode quoteMode; + + private final String recordSeparator; // for outputs + + private final boolean skipHeaderRecord; + + private final boolean trailingDelimiter; + + private final boolean trim; + /** * Creates a customized CSV format. * @@ -405,6 +495,11 @@ public final class CSVFormat implements * TODO * @param allowMissingColumnNames * TODO + * @param ignoreHeaderCase + * TODO + * @param trim + * TODO + * @param trailingDelimiter TODO * @throws IllegalArgumentException * if the delimiter is a line break character */ @@ -412,102 +507,94 @@ public final class CSVFormat implements final Character commentStart, final Character escape, final boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String recordSeparator, final String nullString, final Object[] headerComments, final String[] header, final boolean skipHeaderRecord, - final boolean allowMissingColumnNames) { - this.delimiter = delimiter; - this.quoteCharacter = quoteChar; - this.quoteMode = quoteMode; - this.commentMarker = commentStart; - this.escapeCharacter = escape; - this.ignoreSurroundingSpaces = ignoreSurroundingSpaces; - this.allowMissingColumnNames = allowMissingColumnNames; - this.ignoreEmptyLines = ignoreEmptyLines; - this.recordSeparator = recordSeparator; - this.nullString = nullString; - this.headerComments = toStringArray(headerComments); - this.header = header == null ? null : header.clone(); - this.skipHeaderRecord = skipHeaderRecord; - validate(); - } - - private String[] toStringArray(final Object[] values) { - if (values == null) { - return null; - } - final String[] strings = new String[values.length]; - for (int i = 0; i < values.length; i++) { - final Object value = values[i]; - strings[i] = value == null ? null : value.toString(); - } - return strings; - } + final boolean allowMissingColumnNames, final boolean ignoreHeaderCase, final boolean trim, + final boolean trailingDelimiter) { + this.delimiter = delimiter; + this.quoteCharacter = quoteChar; + this.quoteMode = quoteMode; + this.commentMarker = commentStart; + this.escapeCharacter = escape; + this.ignoreSurroundingSpaces = ignoreSurroundingSpaces; + this.allowMissingColumnNames = allowMissingColumnNames; + this.ignoreEmptyLines = ignoreEmptyLines; + this.recordSeparator = recordSeparator; + this.nullString = nullString; + this.headerComments = toStringArray(headerComments); + this.header = header == null ? null : header.clone(); + this.skipHeaderRecord = skipHeaderRecord; + this.ignoreHeaderCase = ignoreHeaderCase; + this.trailingDelimiter = trailingDelimiter; + this.trim = trim; + validate(); + } @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; + if (this == obj) { + return true; } - if (obj == null) { - return false; + if (obj == null) { + return false; } - if (getClass() != obj.getClass()) { - return false; + if (getClass() != obj.getClass()) { + return false; } - final CSVFormat other = (CSVFormat) obj; - if (delimiter != other.delimiter) { - return false; + final CSVFormat other = (CSVFormat) obj; + if (delimiter != other.delimiter) { + return false; } - if (quoteMode != other.quoteMode) { - return false; + if (quoteMode != other.quoteMode) { + return false; } - if (quoteCharacter == null) { - if (other.quoteCharacter != null) { - return false; + if (quoteCharacter == null) { + if (other.quoteCharacter != null) { + return false; } - } else if (!quoteCharacter.equals(other.quoteCharacter)) { - return false; + } else if (!quoteCharacter.equals(other.quoteCharacter)) { + return false; } - if (commentMarker == null) { - if (other.commentMarker != null) { - return false; + if (commentMarker == null) { + if (other.commentMarker != null) { + return false; } - } else if (!commentMarker.equals(other.commentMarker)) { - return false; + } else if (!commentMarker.equals(other.commentMarker)) { + return false; } - if (escapeCharacter == null) { - if (other.escapeCharacter != null) { - return false; + if (escapeCharacter == null) { + if (other.escapeCharacter != null) { + return false; } - } else if (!escapeCharacter.equals(other.escapeCharacter)) { - return false; + } else if (!escapeCharacter.equals(other.escapeCharacter)) { + return false; } - if (nullString == null) { - if (other.nullString != null) { - return false; + if (nullString == null) { + if (other.nullString != null) { + return false; } - } else if (!nullString.equals(other.nullString)) { - return false; + } else if (!nullString.equals(other.nullString)) { + return false; } - if (!Arrays.equals(header, other.header)) { - return false; + if (!Arrays.equals(header, other.header)) { + return false; } - if (ignoreSurroundingSpaces != other.ignoreSurroundingSpaces) { - return false; + if (ignoreSurroundingSpaces != other.ignoreSurroundingSpaces) { + return false; } - if (ignoreEmptyLines != other.ignoreEmptyLines) { - return false; + if (ignoreEmptyLines != other.ignoreEmptyLines) { + return false; } - if (skipHeaderRecord != other.skipHeaderRecord) { - return false; + if (skipHeaderRecord != other.skipHeaderRecord) { + return false; } - if (recordSeparator == null) { - if (other.recordSeparator != null) { - return false; + if (recordSeparator == null) { + if (other.recordSeparator != null) { + return false; } - } else if (!recordSeparator.equals(other.recordSeparator)) { - return false; + } else if (!recordSeparator.equals(other.recordSeparator)) { + return false; } - return true; + return true; } /** @@ -518,23 +605,33 @@ public final class CSVFormat implements * @return the formatted values */ public String format(final Object... values) { - final StringWriter out = new StringWriter(); + final StringWriter out = new StringWriter(); try { - new CSVPrinter(out, this).printRecord(values); - return out.toString().trim(); - } catch (final IOException e) { + new CSVPrinter(out, this).printRecord(values); + return out.toString().trim(); + } catch (final IOException e) { // should not happen because a StringWriter does not do IO. - throw new IllegalStateException(e); + throw new IllegalStateException(e); } } /** + * Specifies whether missing column names are allowed when parsing the header line. + * + * @return {@code true} if missing column names are allowed when parsing the header line, {@code false} to throw an + * {@link IllegalArgumentException}. + */ + public boolean getAllowMissingColumnNames() { + return allowMissingColumnNames; + } + + /** * Returns the character marking the start of a line comment. * * @return the comment start marker, may be {@code null} */ public Character getCommentMarker() { - return commentMarker; + return commentMarker; } /** @@ -543,7 +640,7 @@ public final class CSVFormat implements * @return the delimiter character */ public char getDelimiter() { - return delimiter; + return delimiter; } /** @@ -552,7 +649,7 @@ public final class CSVFormat implements * @return the escape character, may be {@code null} */ public Character getEscapeCharacter() { - return escapeCharacter; + return escapeCharacter; } /** @@ -561,7 +658,7 @@ public final class CSVFormat implements * @return a copy of the header array; {@code null} if disabled, the empty array if to be read from the file */ public String[] getHeader() { - return header != null ? header.clone() : null; + return header != null ? header.clone() : null; } /** @@ -570,27 +667,27 @@ public final class CSVFormat implements * @return a copy of the header comment array; {@code null} if disabled. */ public String[] getHeaderComments() { - return headerComments != null ? headerComments.clone() : null; + return headerComments != null ? headerComments.clone() : null; } /** - * Specifies whether missing column names are allowed when parsing the header line. + * Specifies whether empty lines between records are ignored when parsing input. * - * @return {@code true} if missing column names are allowed when parsing the header line, {@code false} to throw an - * {@link IllegalArgumentException}. + * @return {@code true} if empty lines between records are ignored, {@code false} if they are turned into empty + * records. */ - public boolean getAllowMissingColumnNames() { - return allowMissingColumnNames; + public boolean getIgnoreEmptyLines() { + return ignoreEmptyLines; } /** - * Specifies whether empty lines between records are ignored when parsing input. + * Specifies whether header names will be accessed ignoring case. * - * @return {@code true} if empty lines between records are ignored, {@code false} if they are turned into empty - * records. + * @return {@code true} if header names cases are ignored, {@code false} if they are case sensitive. + * @since 1.3 */ - public boolean getIgnoreEmptyLines() { - return ignoreEmptyLines; + public boolean getIgnoreHeaderCase() { + return ignoreHeaderCase; } /** @@ -599,23 +696,21 @@ public final class CSVFormat implements * @return {@code true} if spaces around values are ignored, {@code false} if they are treated as part of the value. */ public boolean getIgnoreSurroundingSpaces() { - return ignoreSurroundingSpaces; + return ignoreSurroundingSpaces; } /** * Gets the String to convert to and from {@code null}. * <ul> - * <li> - * <strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading + * <li><strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading * records.</li> - * <li> - * <strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li> + * <li><strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li> * </ul> * * @return the String to convert to and from {@code null}. No substitution occurs if {@code null} */ public String getNullString() { - return nullString; + return nullString; } /** @@ -624,7 +719,7 @@ public final class CSVFormat implements * @return the quoteChar character, may be {@code null} */ public Character getQuoteCharacter() { - return quoteCharacter; + return quoteCharacter; } /** @@ -633,7 +728,7 @@ public final class CSVFormat implements * @return the quote policy */ public QuoteMode getQuoteMode() { - return quoteMode; + return quoteMode; } /** @@ -642,7 +737,7 @@ public final class CSVFormat implements * @return the record separator */ public String getRecordSeparator() { - return recordSeparator; + return recordSeparator; } /** @@ -651,26 +746,46 @@ public final class CSVFormat implements * @return whether to skip the header record. */ public boolean getSkipHeaderRecord() { - return skipHeaderRecord; + return skipHeaderRecord; + } + + /** + * Returns whether to add a trailing delimiter. + * + * @return whether to add a trailing delimiter. + * @since 1.3 + */ + public boolean getTrailingDelimiter() { + return trailingDelimiter; + } + + /** + * Returns whether to trim leading and trailing blanks. + * + * @return whether to trim leading and trailing blanks. + */ + public boolean getTrim() { + return trim; } @Override public int hashCode() { - final int prime = 31; - int result = 1; + final int prime = 31; + int result = 1; - result = prime * result + delimiter; - result = prime * result + ((quoteMode == null) ? 0 : quoteMode.hashCode()); - result = prime * result + ((quoteCharacter == null) ? 0 : quoteCharacter.hashCode()); - result = prime * result + ((commentMarker == null) ? 0 : commentMarker.hashCode()); - result = prime * result + ((escapeCharacter == null) ? 0 : escapeCharacter.hashCode()); - result = prime * result + ((nullString == null) ? 0 : nullString.hashCode()); - result = prime * result + (ignoreSurroundingSpaces ? 1231 : 1237); - result = prime * result + (ignoreEmptyLines ? 1231 : 1237); - result = prime * result + (skipHeaderRecord ? 1231 : 1237); - result = prime * result + ((recordSeparator == null) ? 0 : recordSeparator.hashCode()); - result = prime * result + Arrays.hashCode(header); - return result; + result = prime * result + delimiter; + result = prime * result + ((quoteMode == null) ? 0 : quoteMode.hashCode()); + result = prime * result + ((quoteCharacter == null) ? 0 : quoteCharacter.hashCode()); + result = prime * result + ((commentMarker == null) ? 0 : commentMarker.hashCode()); + result = prime * result + ((escapeCharacter == null) ? 0 : escapeCharacter.hashCode()); + result = prime * result + ((nullString == null) ? 0 : nullString.hashCode()); + result = prime * result + (ignoreSurroundingSpaces ? 1231 : 1237); + result = prime * result + (ignoreHeaderCase ? 1231 : 1237); + result = prime * result + (ignoreEmptyLines ? 1231 : 1237); + result = prime * result + (skipHeaderRecord ? 1231 : 1237); + result = prime * result + ((recordSeparator == null) ? 0 : recordSeparator.hashCode()); + result = prime * result + Arrays.hashCode(header); + return result; } /** @@ -681,7 +796,7 @@ public final class CSVFormat implements * @return {@code true} is comments are supported, {@code false} otherwise */ public boolean isCommentMarkerSet() { - return commentMarker != null; + return commentMarker != null; } /** @@ -690,7 +805,7 @@ public final class CSVFormat implements * @return {@code true} if escapes are processed */ public boolean isEscapeCharacterSet() { - return escapeCharacter != null; + return escapeCharacter != null; } /** @@ -699,7 +814,7 @@ public final class CSVFormat implements * @return {@code true} if a nullString is defined */ public boolean isNullStringSet() { - return nullString != null; + return nullString != null; } /** @@ -708,7 +823,7 @@ public final class CSVFormat implements * @return {@code true} if a quoteChar is defined */ public boolean isQuoteCharacterSet() { - return quoteCharacter != null; + return quoteCharacter != null; } /** @@ -725,7 +840,7 @@ public final class CSVFormat implements * If an I/O error occurs */ public CSVParser parse(final Reader in) throws IOException { - return new CSVParser(in, this); + return new CSVParser(in, this); } /** @@ -742,49 +857,64 @@ public final class CSVFormat implements * thrown if the optional header cannot be printed. */ public CSVPrinter print(final Appendable out) throws IOException { - return new CSVPrinter(out, this); + return new CSVPrinter(out, this); } @Override public String toString() { - final StringBuilder sb = new StringBuilder(); - sb.append("Delimiter=<").append(delimiter).append('>'); - if (isEscapeCharacterSet()) { - sb.append(' '); - sb.append("Escape=<").append(escapeCharacter).append('>'); - } - if (isQuoteCharacterSet()) { - sb.append(' '); - sb.append("QuoteChar=<").append(quoteCharacter).append('>'); - } - if (isCommentMarkerSet()) { - sb.append(' '); - sb.append("CommentStart=<").append(commentMarker).append('>'); - } - if (isNullStringSet()) { - sb.append(' '); - sb.append("NullString=<").append(nullString).append('>'); - } - if (recordSeparator != null) { - sb.append(' '); - sb.append("RecordSeparator=<").append(recordSeparator).append('>'); - } - if (getIgnoreEmptyLines()) { - sb.append(" EmptyLines:ignored"); - } - if (getIgnoreSurroundingSpaces()) { - sb.append(" SurroundingSpaces:ignored"); - } - sb.append(" SkipHeaderRecord:").append(skipHeaderRecord); - if (headerComments != null) { - sb.append(' '); - sb.append("HeaderComments:").append(Arrays.toString(headerComments)); - } - if (header != null) { - sb.append(' '); - sb.append("Header:").append(Arrays.toString(header)); + final StringBuilder sb = new StringBuilder(); + sb.append("Delimiter=<").append(delimiter).append('>'); + if (isEscapeCharacterSet()) { + sb.append(' '); + sb.append("Escape=<").append(escapeCharacter).append('>'); + } + if (isQuoteCharacterSet()) { + sb.append(' '); + sb.append("QuoteChar=<").append(quoteCharacter).append('>'); + } + if (isCommentMarkerSet()) { + sb.append(' '); + sb.append("CommentStart=<").append(commentMarker).append('>'); + } + if (isNullStringSet()) { + sb.append(' '); + sb.append("NullString=<").append(nullString).append('>'); + } + if (recordSeparator != null) { + sb.append(' '); + sb.append("RecordSeparator=<").append(recordSeparator).append('>'); + } + if (getIgnoreEmptyLines()) { + sb.append(" EmptyLines:ignored"); + } + if (getIgnoreSurroundingSpaces()) { + sb.append(" SurroundingSpaces:ignored"); + } + if (getIgnoreHeaderCase()) { + sb.append(" IgnoreHeaderCase:ignored"); + } + sb.append(" SkipHeaderRecord:").append(skipHeaderRecord); + if (headerComments != null) { + sb.append(' '); + sb.append("HeaderComments:").append(Arrays.toString(headerComments)); + } + if (header != null) { + sb.append(' '); + sb.append("Header:").append(Arrays.toString(header)); + } + return sb.toString(); + } + + private String[] toStringArray(final Object[] values) { + if (values == null) { + return null; } - return sb.toString(); + final String[] strings = new String[values.length]; + for (int i = 0; i < values.length; i++) { + final Object value = values[i]; + strings[i] = value == null ? null : value.toString(); + } + return strings; } /** @@ -793,53 +923,78 @@ public final class CSVFormat implements * @throws IllegalArgumentException */ private void validate() throws IllegalArgumentException { - if (isLineBreak(delimiter)) { - throw new IllegalArgumentException("The delimiter cannot be a line break"); + if (isLineBreak(delimiter)) { + throw new IllegalArgumentException("The delimiter cannot be a line break"); } - - if (quoteCharacter != null && delimiter == quoteCharacter.charValue()) { - throw new IllegalArgumentException("The quoteChar character and the delimiter cannot be the same ('" + - quoteCharacter + "')"); + + if (quoteCharacter != null && delimiter == quoteCharacter.charValue()) { + throw new IllegalArgumentException( + "The quoteChar character and the delimiter cannot be the same ('" + quoteCharacter + "')"); } - if (escapeCharacter != null && delimiter == escapeCharacter.charValue()) { - throw new IllegalArgumentException("The escape character and the delimiter cannot be the same ('" + - escapeCharacter + "')"); + if (escapeCharacter != null && delimiter == escapeCharacter.charValue()) { + throw new IllegalArgumentException( + "The escape character and the delimiter cannot be the same ('" + escapeCharacter + "')"); } - if (commentMarker != null && delimiter == commentMarker.charValue()) { - throw new IllegalArgumentException("The comment start character and the delimiter cannot be the same ('" + - commentMarker + "')"); + if (commentMarker != null && delimiter == commentMarker.charValue()) { + throw new IllegalArgumentException( + "The comment start character and the delimiter cannot be the same ('" + commentMarker + "')"); } - if (quoteCharacter != null && quoteCharacter.equals(commentMarker)) { - throw new IllegalArgumentException("The comment start character and the quoteChar cannot be the same ('" + - commentMarker + "')"); + if (quoteCharacter != null && quoteCharacter.equals(commentMarker)) { + throw new IllegalArgumentException( + "The comment start character and the quoteChar cannot be the same ('" + commentMarker + "')"); } - if (escapeCharacter != null && escapeCharacter.equals(commentMarker)) { - throw new IllegalArgumentException("The comment start and the escape character cannot be the same ('" + - commentMarker + "')"); + if (escapeCharacter != null && escapeCharacter.equals(commentMarker)) { + throw new IllegalArgumentException( + "The comment start and the escape character cannot be the same ('" + commentMarker + "')"); } - if (escapeCharacter == null && quoteMode == QuoteMode.NONE) { - throw new IllegalArgumentException("No quotes mode set but no escape character is set"); + if (escapeCharacter == null && quoteMode == QuoteMode.NONE) { + throw new IllegalArgumentException("No quotes mode set but no escape character is set"); } - + // validate header - if (header != null) { - final Set<String> dupCheck = new HashSet<String>(); - for (final String hdr : header) { - if (!dupCheck.add(hdr)) { - throw new IllegalArgumentException("The header contains a duplicate entry: '" + hdr + "' in " + - Arrays.toString(header)); + if (header != null) { + final Set<String> dupCheck = new HashSet<String>(); + for (final String hdr : header) { + if (!dupCheck.add(hdr)) { + throw new IllegalArgumentException( + "The header contains a duplicate entry: '" + hdr + "' in " + Arrays.toString(header)); } } } - } + } + + /** + * Returns a new {@code CSVFormat} with the missing column names behavior of the format set to {@code true} + * + * @return A new CSVFormat that is equal to this but with the specified missing column names behavior. + * @see #withAllowMissingColumnNames(boolean) + * @since 1.1 + */ + public CSVFormat withAllowMissingColumnNames() { + return this.withAllowMissingColumnNames(true); + } + + /** + * Returns a new {@code CSVFormat} with the missing column names behavior of the format set to the given value. + * + * @param allowMissingColumnNames + * the missing column names behavior, {@code true} to allow missing column names in the header line, + * {@code false} to cause an {@link IllegalArgumentException} to be thrown. + * @return A new CSVFormat that is equal to this but with the specified missing column names behavior. + */ + public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) { + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); + } /** - * Sets the comment start marker of the format to the specified character. + * Returns a new {@code CSVFormat} with the comment start marker of the format set to the specified character. * * Note that the comment start character is only recognized at the start of a line. * @@ -850,11 +1005,11 @@ public final class CSVFormat implements * thrown if the specified character is a line break */ public CSVFormat withCommentMarker(final char commentMarker) { - return withCommentMarker(Character.valueOf(commentMarker)); + return withCommentMarker(Character.valueOf(commentMarker)); } /** - * Sets the comment start marker of the format to the specified character. + * Returns a new {@code CSVFormat} with the comment start marker of the format set to the specified character. * * Note that the comment start character is only recognized at the start of a line. * @@ -865,16 +1020,16 @@ public final class CSVFormat implements * thrown if the specified character is a line break */ public CSVFormat withCommentMarker(final Character commentMarker) { - if (isLineBreak(commentMarker)) { - throw new IllegalArgumentException("The comment start marker character cannot be a line break"); + if (isLineBreak(commentMarker)) { + throw new IllegalArgumentException("The comment start marker character cannot be a line break"); } - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets the delimiter of the format to the specified character. + * Returns a new {@code CSVFormat} with the delimiter of the format set to the specified character. * * @param delimiter * the delimiter character @@ -883,16 +1038,16 @@ public final class CSVFormat implements * thrown if the specified character is a line break */ public CSVFormat withDelimiter(final char delimiter) { - if (isLineBreak(delimiter)) { - throw new IllegalArgumentException("The delimiter cannot be a line break"); + if (isLineBreak(delimiter)) { + throw new IllegalArgumentException("The delimiter cannot be a line break"); } - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets the escape character of the format to the specified character. + * Returns a new {@code CSVFormat} with the escape character of the format set to the specified character. * * @param escape * the escape character @@ -901,11 +1056,11 @@ public final class CSVFormat implements * thrown if the specified character is a line break */ public CSVFormat withEscape(final char escape) { - return withEscape(Character.valueOf(escape)); + return withEscape(Character.valueOf(escape)); } /** - * Sets the escape character of the format to the specified character. + * Returns a new {@code CSVFormat} with the escape character of the format set to the specified character. * * @param escape * the escape character, use {@code null} to disable @@ -914,44 +1069,36 @@ public final class CSVFormat implements * thrown if the specified character is a line break */ public CSVFormat withEscape(final Character escape) { - if (isLineBreak(escape)) { - throw new IllegalArgumentException("The escape character cannot be a line break"); + if (isLineBreak(escape)) { + throw new IllegalArgumentException("The escape character cannot be a line break"); } - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escape, ignoreSurroundingSpaces, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escape, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord, - allowMissingColumnNames); + allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets the header of the format. The header can either be parsed automatically from the input file with: + * Returns a new {@code CSVFormat} using the first record as header. * - * <pre> - * CSVFormat format = aformat.withHeader(); - * </pre> - * - * or specified manually with: - * - * <pre> - * CSVFormat format = aformat.withHeader(&quot;name&quot;, &quot;email&quot;, &quot;phone&quot;); - * </pre> * <p> - * The header is also used by the {@link CSVPrinter}.. + * Calling this method is equivalent to calling: * </p> + * <pre> + * CSVFormat format = aFormat.withHeader().withSkipHeaderRecord(); + * </pre> * - * @param header - * the header, {@code null} if disabled, empty if parsed automatically, user specified otherwise. - * - * @return A new CSVFormat that is equal to this but with the specified header + * @return A new CSVFormat that is equal to this but using the first record as header. * @see #withSkipHeaderRecord(boolean) + * @see #withHeader(String...) + * @since 1.3 */ - public CSVFormat withHeader(final String... header) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, - ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + public CSVFormat withFirstRecordAsHeader() { + return withHeader().withSkipHeaderRecord(); } /** - * Sets the header of the format. The header can either be parsed automatically from the input file with: + * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can + * either be parsed automatically from the input file with: * * <pre> * CSVFormat format = aformat.withHeader(); @@ -976,11 +1123,12 @@ public final class CSVFormat implements * @since 1.1 */ public CSVFormat withHeader(final ResultSet resultSet) throws SQLException { - return withHeader(resultSet != null ? resultSet.getMetaData() : null); + return withHeader(resultSet != null ? resultSet.getMetaData() : null); } /** - * Sets the header of the format. The header can either be parsed automatically from the input file with: + * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can + * either be parsed automatically from the input file with: * * <pre> * CSVFormat format = aformat.withHeader(); @@ -1005,78 +1153,115 @@ public final class CSVFormat implements * @since 1.1 */ public CSVFormat withHeader(final ResultSetMetaData metaData) throws SQLException { - String[] labels = null; - if (metaData != null) { - final int columnCount = metaData.getColumnCount(); - labels = new String[columnCount]; - for (int i = 0; i < columnCount; i++) { - labels[i] = metaData.getColumnLabel(i + 1); + String[] labels = null; + if (metaData != null) { + final int columnCount = metaData.getColumnCount(); + labels = new String[columnCount]; + for (int i = 0; i < columnCount; i++) { + labels[i] = metaData.getColumnLabel(i + 1); } } - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, - ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, labels, - skipHeaderRecord, allowMissingColumnNames); + return withHeader(labels); } /** - * Sets the header comments of the format. The comments will be printed first, before the headers. This setting is - * ignored by the parser. + * Returns a new {@code CSVFormat} with the header of the format set to the given values. The header can either be + * parsed automatically from the input file with: * * <pre> - * CSVFormat format = aformat.withHeaderComments(&quot;Generated by Apache Commons CSV 1.1.&quot;, new Date()); + * CSVFormat format = aformat.withHeader(); * </pre> * - * @param headerComments - * the headerComments which will be printed by the Printer before the actual CSV data. + * or specified manually with: + * + * <pre> + * CSVFormat format = aformat.withHeader(&quot;name&quot;, &quot;email&quot;, &quot;phone&quot;); + * </pre> + * <p> + * The header is also used by the {@link CSVPrinter}.. + * </p> + * + * @param header + * the header, {@code null} if disabled, empty if parsed automatically, user specified otherwise. * * @return A new CSVFormat that is equal to this but with the specified header * @see #withSkipHeaderRecord(boolean) - * @since 1.1 */ - public CSVFormat withHeaderComments(final Object... headerComments) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + public CSVFormat withHeader(final String... header) { + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets the missing column names behavior of the format to {@code true} + * Returns a new {@code CSVFormat} with the header of the format defined by the enum class: * - * @return A new CSVFormat that is equal to this but with the specified missing column names behavior. - * @see #withAllowMissingColumnNames(boolean) - * @since 1.1 + * <pre> + * public enum Header { + * Name, Email, Phone + * } + * + * CSVFormat format = aformat.withHeader(Header.class); + * </pre> + * <p> + * The header is also used by the {@link CSVPrinter}.. + * </p> + * + * @param headerEnum + * the enum defining the header, {@code null} if disabled, empty if parsed automatically, user + * specified otherwise. + * + * @return A new CSVFormat that is equal to this but with the specified header + * @see #withHeader(String...) + * @see #withSkipHeaderRecord(boolean) + * @since 1.3 */ - public CSVFormat withAllowMissingColumnNames() { - return this.withAllowMissingColumnNames(true); + public CSVFormat withHeader(final Class<? extends Enum<?>> headerEnum) { + String[] header = null; + if (headerEnum != null) { + Enum<?>[] enumValues = headerEnum.getEnumConstants(); + header = new String[enumValues.length]; + for (int i = 0; i < enumValues.length; i++) { + header[i] = enumValues[i].name(); + } + } + return withHeader(header); } /** - * Sets the missing column names behavior of the format. + * Returns a new {@code CSVFormat} with the header comments of the format set to the given values. The comments will + * be printed first, before the headers. This setting is ignored by the parser. * - * @param allowMissingColumnNames - * the missing column names behavior, {@code true} to allow missing column names in the header line, - * {@code false} to cause an {@link IllegalArgumentException} to be thrown. - * @return A new CSVFormat that is equal to this but with the specified missing column names behavior. + * <pre> + * CSVFormat format = aformat.withHeaderComments(&quot;Generated by Apache Commons CSV 1.1.&quot;, new Date()); + * </pre> + * + * @param headerComments + * the headerComments which will be printed by the Printer before the actual CSV data. + * + * @return A new CSVFormat that is equal to this but with the specified header + * @see #withSkipHeaderRecord(boolean) + * @since 1.1 */ - public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + public CSVFormat withHeaderComments(final Object... headerComments) { + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets the empty line skipping behavior of the format to {@code true}. + * Returns a new {@code CSVFormat} with the empty line skipping behavior of the format set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior. * @since {@link #withIgnoreEmptyLines(boolean)} * @since 1.1 */ public CSVFormat withIgnoreEmptyLines() { - return this.withIgnoreEmptyLines(true); + return this.withIgnoreEmptyLines(true); } /** - * Sets the empty line skipping behavior of the format. + * Returns a new {@code CSVFormat} with the empty line skipping behavior of the format set to the given value. * * @param ignoreEmptyLines * the empty line skipping behavior, {@code true} to ignore the empty lines between the records, @@ -1084,24 +1269,50 @@ public final class CSVFormat implements * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior. */ public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); + } + + /** + * Returns a new {@code CSVFormat} with the header ignore case behavior set to {@code true}. + * + * @return A new CSVFormat that will ignore case header name. + * @see #withIgnoreHeaderCase(boolean) + * @since 1.3 + */ + public CSVFormat withIgnoreHeaderCase() { + return this.withIgnoreHeaderCase(true); } /** - * Sets the trimming behavior of the format to {@code true}. + * Returns a new {@code CSVFormat} with whether header names should be accessed ignoring case. + * + * @param ignoreHeaderCase + * the case mapping behavior, {@code true} to access name/values, {@code false} to leave the mapping as + * is. + * @return A new CSVFormat that will ignore case header name if specified as {@code true} + * @since 1.3 + */ + public CSVFormat withIgnoreHeaderCase(final boolean ignoreHeaderCase) { + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); + } + + /** + * Returns a new {@code CSVFormat} with the trimming behavior of the format set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified trimming behavior. * @see #withIgnoreSurroundingSpaces(boolean) * @since 1.1 */ public CSVFormat withIgnoreSurroundingSpaces() { - return this.withIgnoreSurroundingSpaces(true); + return this.withIgnoreSurroundingSpaces(true); } /** - * Sets the trimming behavior of the format. + * Returns a new {@code CSVFormat} with the trimming behavior of the format set to the given value. * * @param ignoreSurroundingSpaces * the trimming behavior, {@code true} to remove the surrounding spaces, {@code false} to leave the @@ -1109,19 +1320,17 @@ public final class CSVFormat implements * @return A new CSVFormat that is equal to this but with the specified trimming behavior. */ public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Performs conversions to and from null for strings on input and output. + * Returns a new {@code CSVFormat} with conversions to and from null for strings on input and output. * <ul> - * <li> - * <strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading + * <li><strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading * records.</li> - * <li> - * <strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li> + * <li><strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li> * </ul> * * @param nullString @@ -1130,13 +1339,13 @@ public final class CSVFormat implements * @return A new CSVFormat that is equal to this but with the specified null conversion string. */ public CSVFormat withNullString(final String nullString) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets the quoteChar of the format to the specified character. + * Returns a new {@code CSVFormat} with the quoteChar of the format set to the specified character. * * @param quoteChar * the quoteChar character @@ -1145,11 +1354,11 @@ public final class CSVFormat implements * thrown if the specified character is a line break */ public CSVFormat withQuote(final char quoteChar) { - return withQuote(Character.valueOf(quoteChar)); + return withQuote(Character.valueOf(quoteChar)); } /** - * Sets the quoteChar of the format to the specified character. + * Returns a new {@code CSVFormat} with the quoteChar of the format set to the specified character. * * @param quoteChar * the quoteChar character, use {@code null} to disable @@ -1158,16 +1367,16 @@ public final class CSVFormat implements * thrown if the specified character is a line break */ public CSVFormat withQuote(final Character quoteChar) { - if (isLineBreak(quoteChar)) { - throw new IllegalArgumentException("The quoteChar cannot be a line break"); + if (isLineBreak(quoteChar)) { + throw new IllegalArgumentException("The quoteChar cannot be a line break"); } - return new CSVFormat(delimiter, quoteChar, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, + return new CSVFormat(delimiter, quoteChar, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord, - allowMissingColumnNames); + allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets the output quote policy of the format to the specified value. + * Returns a new {@code CSVFormat} with the output quote policy of the format set to the specified value. * * @param quoteModePolicy * the quote policy to use for output. @@ -1175,13 +1384,13 @@ public final class CSVFormat implements * @return A new CSVFormat that is equal to this but with the specified quote policy */ public CSVFormat withQuoteMode(final QuoteMode quoteModePolicy) { - return new CSVFormat(delimiter, quoteCharacter, quoteModePolicy, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteModePolicy, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets the record separator of the format to the specified character. + * Returns a new {@code CSVFormat} with the record separator of the format set to the specified character. * * <p> * <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently @@ -1194,11 +1403,11 @@ public final class CSVFormat implements * @return A new CSVFormat that is equal to this but with the the specified output record separator */ public CSVFormat withRecordSeparator(final char recordSeparator) { - return withRecordSeparator(String.valueOf(recordSeparator)); + return withRecordSeparator(String.valueOf(recordSeparator)); } /** - * Sets the record separator of the format to the specified String. + * Returns a new {@code CSVFormat} with the record separator of the format set to the specified String. * * <p> * <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently @@ -1213,13 +1422,13 @@ public final class CSVFormat implements * if recordSeparator is none of CR, LF or CRLF */ public CSVFormat withRecordSeparator(final String recordSeparator) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } /** - * Sets skipping the header record to {@code true}. + * Returns a new {@code CSVFormat} with skipping the header record set to {@code true}. * * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting. * @see #withSkipHeaderRecord(boolean) @@ -1227,11 +1436,11 @@ public final class CSVFormat implements * @since 1.1 */ public CSVFormat withSkipHeaderRecord() { - return this.withSkipHeaderRecord(true); + return this.withSkipHeaderRecord(true); } /** - * Sets whether to skip the header record. + * Returns a new {@code CSVFormat} with whether to skip the header record. * * @param skipHeaderRecord * whether to skip the header record. @@ -1240,9 +1449,59 @@ public final class CSVFormat implements * @see #withHeader(String...) */ public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); + } + + /** + * Returns a new {@code CSVFormat} to add a trailing delimiter. + * + * @return A new CSVFormat that is equal to this but with the trailing delimiter setting. + * @since 1.3 + */ + public CSVFormat withTrailingDelimiter() { + return withTrailingDelimiter(true); + } + + /** + * Returns a new {@code CSVFormat} with whether to add a trailing delimiter. + * + * @param trailingDelimiter + * whether to add a trailing delimiter. + * + * @return A new CSVFormat that is equal to this but with the specified trailing delimiter setting. + * @since 1.3 + */ + public CSVFormat withTrailingDelimiter(final boolean trailingDelimiter) { + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); + } + + /** + * Returns a new {@code CSVFormat} to trim leading and trailing blanks. + * + * @return A new CSVFormat that is equal to this but with the trim setting on. + * @since 1.3 + */ + public CSVFormat withTrim() { + return withTrim(true); + } + + /** + * Returns a new {@code CSVFormat} with whether to trim leading and trailing blanks. + * + * @param trim + * whether to trim leading and trailing blanks. + * + * @return A new CSVFormat that is equal to this but with the specified trim setting. + * @since 1.3 + */ + public CSVFormat withTrim(final boolean trim) { + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, - skipHeaderRecord, allowMissingColumnNames); + skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } } - \ No newline at end of file [... 3 lines stripped ...]