Skip to content

Commit 722d696

Browse files
committed
[CSV-134] Unified parameter validation.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1638699 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3c4c251 commit 722d696

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<action issue="CSV-128" type="fix" dev="ggregory">CSVFormat.EXCEL should ignore empty header names</action>
4545
<action issue="CSV-132" type="fix" dev="ggregory" due-to="Sascha Szott">Incorrect Javadoc referencing org.apache.commons.csv.CSVFormat withQuote()</action>
4646
<action issue="CSV-124" type="update" dev="brentworden" due-to="Kalyan">Improve toString() implementation of CSVRecord</action>
47+
<action issue="CSV-134" type="update" dev="ggregory" due-to="wu wen">Unified parameter validation</action>
4748
<action issue="CSV-129" type="add" dev="ggregory">Add CSVFormat#with 0-arg methods matching boolean arg methods</action>
4849
<action issue="CSV-131" type="add" dev="ggregory" due-to="Holger Stratmann">Save positions of records to enable random access</action>
4950
<action issue="CSV-139" type="add" dev="ggregory">CSVPrinter.printRecord(ResultSet) with metadata</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,6 @@ private CSVFormat(final char delimiter, final Character quoteChar, final QuoteMo
348348
final boolean ignoreEmptyLines, final String recordSeparator, final String nullString,
349349
final Object[] headerComments, final String[] header, final boolean skipHeaderRecord,
350350
final boolean allowMissingColumnNames) {
351-
if (isLineBreak(delimiter)) {
352-
throw new IllegalArgumentException("The delimiter cannot be a line break");
353-
}
354351
this.delimiter = delimiter;
355352
this.quoteCharacter = quoteChar;
356353
this.quoteMode = quoteMode;
@@ -362,18 +359,7 @@ private CSVFormat(final char delimiter, final Character quoteChar, final QuoteMo
362359
this.recordSeparator = recordSeparator;
363360
this.nullString = nullString;
364361
this.headerComments = toStringArray(headerComments);
365-
if (header == null) {
366-
this.header = null;
367-
} else {
368-
final Set<String> dupCheck = new HashSet<String>();
369-
for (final String hdr : header) {
370-
if (!dupCheck.add(hdr)) {
371-
throw new IllegalArgumentException("The header contains a duplicate entry: '" + hdr + "' in " +
372-
Arrays.toString(header));
373-
}
374-
}
375-
this.header = header.clone();
376-
}
362+
this.header = header == null ? null : header.clone();
377363
this.skipHeaderRecord = skipHeaderRecord;
378364
validate();
379365
}
@@ -742,6 +728,10 @@ public String toString() {
742728
* @throws IllegalArgumentException
743729
*/
744730
private void validate() throws IllegalArgumentException {
731+
if (isLineBreak(delimiter)) {
732+
throw new IllegalArgumentException("The delimiter cannot be a line break");
733+
}
734+
745735
if (quoteCharacter != null && delimiter == quoteCharacter.charValue()) {
746736
throw new IllegalArgumentException("The quoteChar character and the delimiter cannot be the same ('" +
747737
quoteCharacter + "')");
@@ -770,6 +760,17 @@ private void validate() throws IllegalArgumentException {
770760
if (escapeCharacter == null && quoteMode == QuoteMode.NONE) {
771761
throw new IllegalArgumentException("No quotes mode set but no escape character is set");
772762
}
763+
764+
// validate header
765+
if (header != null) {
766+
final Set<String> dupCheck = new HashSet<String>();
767+
for (final String hdr : header) {
768+
if (!dupCheck.add(hdr)) {
769+
throw new IllegalArgumentException("The header contains a duplicate entry: '" + hdr + "' in " +
770+
Arrays.toString(header));
771+
}
772+
}
773+
}
773774
}
774775

775776
/**

0 commit comments

Comments
 (0)