Skip to content

Commit 6a2a02f

Browse files
committed
CSV-264: Fixes requested by contributor.
Added back public API methods. Fixed since version on new methods added by this PR. Updated check-supressions to have new line number. Reverted pre-existing tests to use public API methods that were added back.
1 parent 10a519d commit 6a2a02f

5 files changed

Lines changed: 37 additions & 8 deletions

File tree

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,11 +884,21 @@ public String format(final Object... values) {
884884
}
885885
}
886886

887+
/**
888+
* Returns true if and only if duplicate names are allowed in the headers.
889+
*
890+
* @return whether duplicate header names are allowed
891+
* @since 1.7
892+
*/
893+
public boolean getAllowDuplicateHeaderNames() {
894+
return duplicateHeaderMode == DuplicateHeaderMode.ALLOW_ALL;
895+
}
896+
887897
/**
888898
* Returns how duplicate headers are handled.
889899
*
890900
* @return if duplicate header values are allowed, allowed conditionally, or disallowed.
891-
* @since 1.7
901+
* @since 1.9
892902
*/
893903
public DuplicateHeaderMode getDuplicateHeaderMode() {
894904
return duplicateHeaderMode;
@@ -1687,13 +1697,30 @@ public CSVFormat withAllowDuplicateHeaderNames() {
16871697
return withDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_ALL);
16881698
}
16891699

1700+
/**
1701+
* Returns a new {@code CSVFormat} with duplicate header names behavior set to the given value.
1702+
*
1703+
* @param allowDuplicateHeaderNames the duplicate header names behavior, true to allow, false to disallow.
1704+
* @return a new {@code CSVFormat} with duplicate header names behavior set to the given value.
1705+
* @since 1.7
1706+
*/
1707+
public CSVFormat withAllowDuplicateHeaderNames(final boolean allowDuplicateHeaderNames) {
1708+
final DuplicateHeaderMode mode =
1709+
(allowDuplicateHeaderNames) ? DuplicateHeaderMode.ALLOW_ALL : DuplicateHeaderMode.ALLOW_EMPTY;
1710+
1711+
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
1712+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
1713+
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush,
1714+
mode);
1715+
}
1716+
16901717
/**
16911718
* Returns a new {@code CSVFormat} with duplicate header names behavior set to the given value.
16921719
*
16931720
* @param duplicateHeaderMode the duplicate header names behavior, to allow, allow conditionally,
16941721
* or disable duplicates.
16951722
* @return a new {@code CSVFormat} with duplicate header names behavior set to the given value.
1696-
* @since 1.7
1723+
* @since 1.9
16971724
*/
16981725
public CSVFormat withDuplicateHeaderMode(final DuplicateHeaderMode duplicateHeaderMode) {
16991726
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,11 @@ private Headers createHeaders() throws IOException {
505505
}
506506

507507
final boolean containsHeader = header != null && hdrMap.containsKey(header);
508-
final DuplicateHeaderMode headerRule = this.format.getDuplicateHeaderMode();
508+
final DuplicateHeaderMode headerMode = this.format.getDuplicateHeaderMode();
509+
final boolean duplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_ALL;
510+
final boolean emptyDuplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_EMPTY;
509511

510-
if (containsHeader && headerRule != DuplicateHeaderMode.ALLOW_ALL && !(emptyHeader && headerRule == DuplicateHeaderMode.ALLOW_EMPTY)) {
512+
if (containsHeader && !duplicatesAllowed && !(emptyHeader && emptyDuplicatesAllowed)) {
511513
throw new IllegalArgumentException(
512514
String.format(
513515
"The header contains a duplicate name: \"%s\" in %s. If this is valid then use CSVFormat.withDuplicateHeaderMode().",

src/site/resources/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
2020
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
2121
<suppressions>
22-
<suppress checks="LineLength" files="[\\/]CSVParser\.java$" lines="511"/>
22+
<suppress checks="LineLength" files="[\\/]CSVParser\.java$" lines="515"/>
2323
</suppressions>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public void testDuplicateHeaderElements() {
100100
public void testDuplicateHeaderElementsFalse() {
101101
assertThrows(
102102
IllegalArgumentException.class,
103-
() -> CSVFormat.DEFAULT.withDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW).withHeader("A", "A"));
103+
() -> CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(false).withHeader("A", "A"));
104104
}
105105

106106
public void testDuplicateHeaderElementsTrue() {
107-
CSVFormat.DEFAULT.withDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_ALL).withHeader("A", "A");
107+
CSVFormat.DEFAULT.withAllowDuplicateHeaderNames(true).withHeader("A", "A");
108108
}
109109

110110
@Test

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public void testDefaultFormat() throws IOException {
298298
@Test
299299
public void testDuplicateHeadersNotAllowed() {
300300
assertThrows(IllegalArgumentException.class, () -> CSVParser.parse("a,b,a\n1,2,3\nx,y,z",
301-
CSVFormat.DEFAULT.withHeader().withDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW)));
301+
CSVFormat.DEFAULT.withHeader().withAllowDuplicateHeaderNames(false)));
302302
}
303303

304304
@Test

0 commit comments

Comments
 (0)