Skip to content

Commit aa0762d

Browse files
committed
Rename the IgnoreEmptyHeaders property to AllowMissingColumnNames
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1612337 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6234524 commit aa0762d

3 files changed

Lines changed: 29 additions & 31 deletions

File tree

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public final class CSVFormat implements Serializable {
152152
private final Character commentStart; // null if commenting is disabled
153153
private final Character escape; // null if escaping is disabled
154154
private final boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values?
155-
private final boolean ignoreEmptyHeaders;
155+
private final boolean allowMissingColumnNames;
156156
private final boolean ignoreEmptyLines;
157157
private final String recordSeparator; // for outputs
158158
private final String nullString; // the string to be used for null values
@@ -330,15 +330,15 @@ public static CSVFormat newFormat(final char delimiter) {
330330
* @param header
331331
* the header
332332
* @param skipHeaderRecord TODO
333-
* @param ignoreEmptyHeaders TODO
333+
* @param allowMissingColumnNames TODO
334334
* @throws IllegalArgumentException if the delimiter is a line break character
335335
*/
336336
private CSVFormat(final char delimiter, final Character quoteChar,
337337
final Quote quotePolicy, final Character commentStart,
338338
final Character escape, final boolean ignoreSurroundingSpaces,
339339
final boolean ignoreEmptyLines, final String recordSeparator,
340340
final String nullString, final String[] header, final boolean skipHeaderRecord,
341-
final boolean ignoreEmptyHeaders) {
341+
final boolean allowMissingColumnNames) {
342342
if (isLineBreak(delimiter)) {
343343
throw new IllegalArgumentException("The delimiter cannot be a line break");
344344
}
@@ -348,7 +348,7 @@ private CSVFormat(final char delimiter, final Character quoteChar,
348348
this.commentStart = commentStart;
349349
this.escape = escape;
350350
this.ignoreSurroundingSpaces = ignoreSurroundingSpaces;
351-
this.ignoreEmptyHeaders = ignoreEmptyHeaders;
351+
this.allowMissingColumnNames = allowMissingColumnNames;
352352
this.ignoreEmptyLines = ignoreEmptyLines;
353353
this.recordSeparator = recordSeparator;
354354
this.nullString = nullString;
@@ -492,13 +492,13 @@ public String[] getHeader() {
492492
}
493493

494494
/**
495-
* Specifies whether empty headers are ignored when parsing the header line.
495+
* Specifies whether missing column names are allowed when parsing the header line.
496496
*
497-
* @return {@code true} if headers are ignored when parsing the header line, {@code false} to throw an
497+
* @return {@code true} if missing column names are allowed when parsing the header line, {@code false} to throw an
498498
* {@link IllegalArgumentException}.
499499
*/
500-
public boolean isIgnoringEmptyHeaders() {
501-
return ignoreEmptyHeaders;
500+
public boolean isAllowMissingColumnNames() {
501+
return allowMissingColumnNames;
502502
}
503503

504504
/**
@@ -772,7 +772,7 @@ public CSVFormat withCommentMarker(final Character commentMarker) {
772772
}
773773
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentMarker, escape,
774774
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
775-
ignoreEmptyHeaders);
775+
allowMissingColumnNames);
776776
}
777777

778778
/**
@@ -790,7 +790,7 @@ public CSVFormat withDelimiter(final char delimiter) {
790790
}
791791
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
792792
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
793-
ignoreEmptyHeaders);
793+
allowMissingColumnNames);
794794
}
795795

796796
/**
@@ -821,7 +821,7 @@ public CSVFormat withEscape(final Character escape) {
821821
}
822822
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
823823
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
824-
ignoreEmptyHeaders);
824+
allowMissingColumnNames);
825825
}
826826

827827
/**
@@ -844,21 +844,21 @@ public CSVFormat withEscape(final Character escape) {
844844
public CSVFormat withHeader(final String... header) {
845845
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
846846
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
847-
ignoreEmptyHeaders);
847+
allowMissingColumnNames);
848848
}
849849

850850
/**
851-
* Sets the empty header behavior of the format.
851+
* Sets the missing column names behavior of the format.
852852
*
853-
* @param ignoreEmptyHeaders
854-
* the empty header behavior, {@code true} to ignore empty headers in the header line,
853+
* @param allowMissingColumnNames
854+
* the missing column names behavior, {@code true} to allow missing column names in the header line,
855855
* {@code false} to cause an {@link IllegalArgumentException} to be thrown.
856-
* @return A new CSVFormat that is equal to this but with the specified empty header behavior.
856+
* @return A new CSVFormat that is equal to this but with the specified missing column names behavior.
857857
*/
858-
public CSVFormat withIgnoreEmptyHeaders(final boolean ignoreEmptyHeaders) {
858+
public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) {
859859
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
860-
ignoreSurroundingSpaces, ignoreEmptyHeaders, recordSeparator, nullString, header, skipHeaderRecord,
861-
ignoreEmptyHeaders);
860+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
861+
allowMissingColumnNames);
862862
}
863863

864864
/**
@@ -872,7 +872,7 @@ public CSVFormat withIgnoreEmptyHeaders(final boolean ignoreEmptyHeaders) {
872872
public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
873873
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
874874
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
875-
ignoreEmptyHeaders);
875+
allowMissingColumnNames);
876876
}
877877

878878
/**
@@ -886,7 +886,7 @@ public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
886886
public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
887887
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
888888
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
889-
ignoreEmptyHeaders);
889+
allowMissingColumnNames);
890890
}
891891

892892
/**
@@ -907,7 +907,7 @@ public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpac
907907
public CSVFormat withNullString(final String nullString) {
908908
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
909909
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
910-
ignoreEmptyHeaders);
910+
allowMissingColumnNames);
911911
}
912912

913913
/**
@@ -938,7 +938,7 @@ public CSVFormat withQuoteChar(final Character quoteChar) {
938938
}
939939
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
940940
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
941-
ignoreEmptyHeaders);
941+
allowMissingColumnNames);
942942
}
943943

944944
/**
@@ -952,7 +952,7 @@ public CSVFormat withQuoteChar(final Character quoteChar) {
952952
public CSVFormat withQuotePolicy(final Quote quotePolicy) {
953953
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
954954
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
955-
ignoreEmptyHeaders);
955+
allowMissingColumnNames);
956956
}
957957

958958
/**
@@ -986,7 +986,7 @@ public CSVFormat withRecordSeparator(final char recordSeparator) {
986986
public CSVFormat withRecordSeparator(final String recordSeparator) {
987987
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
988988
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
989-
ignoreEmptyHeaders);
989+
allowMissingColumnNames);
990990
}
991991

992992
/**
@@ -1001,6 +1001,6 @@ public CSVFormat withRecordSeparator(final String recordSeparator) {
10011001
public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) {
10021002
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
10031003
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
1004-
ignoreEmptyHeaders);
1004+
allowMissingColumnNames);
10051005
}
10061006
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.nio.charset.Charset;
3030
import java.util.ArrayList;
3131
import java.util.Arrays;
32-
import java.util.Collection;
3332
import java.util.Iterator;
3433
import java.util.LinkedHashMap;
3534
import java.util.List;
@@ -365,7 +364,7 @@ private Map<String, Integer> initializeHeader() throws IOException {
365364
final String header = headerRecord[i];
366365
final boolean containsHeader = hdrMap.containsKey(header);
367366
final boolean emptyHeader = header == null || header.trim().isEmpty();
368-
if (containsHeader && (!emptyHeader || (emptyHeader && !this.format.isIgnoringEmptyHeaders()))) {
367+
if (containsHeader && (!emptyHeader || (emptyHeader && !this.format.isAllowMissingColumnNames()))) {
369368
throw new IllegalArgumentException("The header contains a duplicate name: \"" + header +
370369
"\" in " + Arrays.toString(headerRecord));
371370
}

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.nio.charset.Charset;
4141
import java.util.ArrayList;
4242
import java.util.Iterator;
43-
import java.util.LinkedList;
4443
import java.util.List;
4544
import java.util.Map;
4645
import java.util.NoSuchElementException;
@@ -650,13 +649,13 @@ public void testHeadersMissingException() throws Exception {
650649
@Test
651650
public void testHeadersMissing() throws Exception {
652651
final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
653-
CSVFormat.DEFAULT.withHeader().withIgnoreEmptyHeaders(true).parse(in).iterator();
652+
CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames(true).parse(in).iterator();
654653
}
655654

656655
@Test
657656
public void testHeaderMissingWithNull() throws Exception {
658657
final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
659-
CSVFormat.DEFAULT.withHeader().withNullString("").withIgnoreEmptyHeaders(true).parse(in).iterator();
658+
CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames(true).parse(in).iterator();
660659
}
661660

662661
@Test

0 commit comments

Comments
 (0)