Skip to content

Commit 3eededc

Browse files
committed
Add CVSRecord.isMapped(String) API.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397094 13f79535-47bb-0310-9956-ffa450edef68
1 parent 67bbc35 commit 3eededc

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,32 @@ public String get(int i) {
6060
* Returns a value by name.
6161
*
6262
* @param name
63-
* the name of the column to be retrieved
63+
* the name of the column to be retrieved.
6464
* @return the column value, or {@code null} if the column name is not found
6565
* @throws IllegalStateException
6666
* if no header mapping was provided
6767
*/
6868
public String get(String name) {
6969
if (mapping == null) {
70-
throw new IllegalStateException("No header was specified, the record values can't be accessed by name");
70+
throw new IllegalStateException("No header mapping was specified, the record values can't be accessed by name");
7171
}
7272

7373
Integer index = mapping.get(name);
7474

7575
return index != null ? values[index.intValue()] : null;
7676
}
7777

78+
/**
79+
* Checks whether a given columns is mapped.
80+
*
81+
* @param name
82+
* the name of the column to be retrieved.
83+
* @return whether a given columns is mapped.
84+
*/
85+
public boolean isMapped(String name) {
86+
return mapping != null ? mapping.containsKey(name) : false;
87+
}
88+
7889
public Iterator<String> iterator() {
7990
return Arrays.asList(values).iterator();
8091
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ public void testProvidedHeader() throws Exception {
503503
for (int i = 0; i < 3; i++) {
504504
assertTrue(records.hasNext());
505505
CSVRecord record = records.next();
506+
assertTrue(record.isMapped("A"));
507+
assertTrue(record.isMapped("B"));
508+
assertTrue(record.isMapped("C"));
509+
assertFalse(record.isMapped("NOT MAPPED"));
506510
assertEquals(record.get(0), record.get("A"));
507511
assertEquals(record.get(1), record.get("B"));
508512
assertEquals(record.get(2), record.get("C"));

0 commit comments

Comments
 (0)