Skip to content

Commit 625bafa

Browse files
committed
Add CVSRecord.isSet(String) API.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397136 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6ab9b46 commit 625bafa

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,12 @@ public String get(final String name) {
6969
if (mapping == null) {
7070
throw new IllegalStateException("No header mapping was specified, the record values can't be accessed by name");
7171
}
72-
7372
final Integer index = mapping.get(name);
74-
7573
return index != null ? values[index.intValue()] : null;
7674
}
7775

7876
/**
79-
* Checks whether a given columns is mapped.
77+
* Checks whether a given column is mapped.
8078
*
8179
* @param name
8280
* the name of the column to be retrieved.
@@ -86,6 +84,17 @@ public boolean isMapped(final String name) {
8684
return mapping != null ? mapping.containsKey(name) : false;
8785
}
8886

87+
/**
88+
* Checks whether a given columns is mapped and has a value.
89+
*
90+
* @param name
91+
* the name of the column to be retrieved.
92+
* @return whether a given columns is mapped.
93+
*/
94+
public boolean isSet(final String name) {
95+
return isMapped(name) && mapping.get(name).intValue() < values.length;
96+
}
97+
8998
public Iterator<String> iterator() {
9099
return Arrays.asList(values).iterator();
91100
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,51 @@ public void testProvidedHeader() throws Exception {
515515
assertFalse(records.hasNext());
516516
}
517517

518+
@Test
519+
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
520+
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
521+
522+
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
523+
524+
// header record
525+
assertTrue(records.hasNext());
526+
CSVRecord record = records.next();
527+
assertTrue(record.isMapped("A"));
528+
assertTrue(record.isMapped("B"));
529+
assertTrue(record.isMapped("C"));
530+
assertTrue(record.isSet("A"));
531+
assertTrue(record.isSet("B"));
532+
assertTrue(record.isSet("C"));
533+
assertEquals("a", record.get("A"));
534+
assertEquals("b", record.get("B"));
535+
assertEquals("c", record.get("C"));
536+
537+
// 1st record
538+
record = records.next();
539+
assertTrue(record.isMapped("A"));
540+
assertTrue(record.isMapped("B"));
541+
assertTrue(record.isMapped("C"));
542+
assertTrue(record.isSet("A"));
543+
assertTrue(record.isSet("B"));
544+
assertFalse(record.isSet("C"));
545+
assertEquals("1", record.get("A"));
546+
assertEquals("2", record.get("B"));
547+
548+
// 2nd record
549+
record = records.next();
550+
assertTrue(record.isMapped("A"));
551+
assertTrue(record.isMapped("B"));
552+
assertTrue(record.isMapped("C"));
553+
assertTrue(record.isSet("A"));
554+
assertTrue(record.isSet("B"));
555+
assertTrue(record.isSet("C"));
556+
assertEquals("x", record.get("A"));
557+
assertEquals("y", record.get("B"));
558+
assertEquals("z", record.get("C"));
559+
560+
assertFalse(records.hasNext());
561+
}
562+
518563
public void testGetHeaderMap() throws Exception {
519564
final CSVParser parser = new CSVParser("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C"));
520565
final Map<String, Integer> headerMap = parser.getHeaderMap();

0 commit comments

Comments
 (0)