You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/20 11:22:29 UTC
[commons-csv] branch master updated: feat: document duplicate header behavior
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git
The following commit(s) were added to refs/heads/master by this push:
new 7f335011 feat: document duplicate header behavior
new bc61b750 Merge pull request #309 from SethFalco/header-docs
7f335011 is described below
commit 7f335011ba5392ddd09a878320010017c26a47ca
Author: Seth Falco <se...@falco.fun>
AuthorDate: Wed Feb 22 23:42:40 2023 +0000
feat: document duplicate header behavior
---
.../java/org/apache/commons/csv/CSVRecord.java | 6 ++--
.../java/org/apache/commons/csv/CSVRecordTest.java | 32 ++++++++++++++++++++++
2 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java
index 5524d9e8..0e70ad75 100644
--- a/src/main/java/org/apache/commons/csv/CSVRecord.java
+++ b/src/main/java/org/apache/commons/csv/CSVRecord.java
@@ -88,7 +88,7 @@ public final class CSVRecord implements Serializable, Iterable<String> {
}
/**
- * Returns a value by name.
+ * Returns a value by name. If multiple instances of the header name exists, only the last occurence is returned.
*
* <p>
* Note: This requires a field mapping obtained from the original parser.
@@ -311,7 +311,9 @@ public final class CSVRecord implements Serializable, Iterable<String> {
}
/**
- * Copies this record into a new Map of header name to record value.
+ * Copies this record into a new Map of header name to record value. If multiple instances of a header name exists,
+ * only the last occurence is mapped.
+ *
* <p>
* Editing the map does not update this instance.
* </p>
diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java
index 4833c26c..db1ef40c 100644
--- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertAll;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -341,6 +342,37 @@ public class CSVRecordTest {
assertTrue(recordWithHeader.toString().contains("values="));
}
+ @Test
+ public void testDuplicateHeaderGet() throws IOException {
+ final String csv = "A,A,B,B\n1,2,5,6\n";
+ final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
+
+ try (final CSVParser parser = CSVParser.parse(csv, format)) {
+ final CSVRecord record = parser.nextRecord();
+
+ assertAll("Test that it gets the last instance of a column when there are duplicate headings",
+ () -> assertEquals("2", record.get("A")),
+ () -> assertEquals("6", record.get("B"))
+ );
+ }
+ }
+
+ @Test
+ public void testDuplicateHeaderToMap() throws IOException {
+ final String csv = "A,A,B,B\n1,2,5,6\n";
+ final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build();
+
+ try (final CSVParser parser = CSVParser.parse(csv, format)) {
+ final CSVRecord record = parser.nextRecord();
+ final Map<String, String> map = record.toMap();
+
+ assertAll("Test that it gets the last instance of a column when there are duplicate headings",
+ () -> assertEquals("2", map.get("A")),
+ () -> assertEquals("6", map.get("B"))
+ );
+ }
+ }
+
private void validateMap(final Map<String, String> map, final boolean allowsNulls) {
assertTrue(map.containsKey(EnumHeader.FIRST.name()));
assertTrue(map.containsKey(EnumHeader.SECOND.name()));