Author: ggregory Date: Thu Oct 11 16:07:51 2012 New Revision: 1397136 URL: http://svn.apache.org/viewvc?rev=1397136&view=rev Log: Add CVSRecord.isSet(String) API.
Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java?rev=1397136&r1=1397135&r2=1397136&view=diff ============================================================================== --- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java (original) +++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java Thu Oct 11 16:07:51 2012 @@ -69,14 +69,12 @@ public class CSVRecord implements Serial if (mapping == null) { throw new IllegalStateException("No header mapping was specified, the record values can't be accessed by name"); } - final Integer index = mapping.get(name); - return index != null ? values[index.intValue()] : null; } /** - * Checks whether a given columns is mapped. + * Checks whether a given column is mapped. * * @param name * the name of the column to be retrieved. @@ -86,6 +84,17 @@ public class CSVRecord implements Serial return mapping != null ? mapping.containsKey(name) : false; } + /** + * Checks whether a given columns is mapped and has a value. + * + * @param name + * the name of the column to be retrieved. + * @return whether a given columns is mapped. + */ + public boolean isSet(final String name) { + return isMapped(name) && mapping.get(name).intValue() < values.length; + } + public Iterator<String> iterator() { return Arrays.asList(values).iterator(); } Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1397136&r1=1397135&r2=1397136&view=diff ============================================================================== --- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original) +++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Thu Oct 11 16:07:51 2012 @@ -515,6 +515,51 @@ public class CSVParserTest { assertFalse(records.hasNext()); } + @Test + public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception { + final Reader in = new StringReader("a,b,c\n1,2\nx,y,z"); + + final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator(); + + // header record + assertTrue(records.hasNext()); + CSVRecord record = records.next(); + assertTrue(record.isMapped("A")); + assertTrue(record.isMapped("B")); + assertTrue(record.isMapped("C")); + assertTrue(record.isSet("A")); + assertTrue(record.isSet("B")); + assertTrue(record.isSet("C")); + assertEquals("a", record.get("A")); + assertEquals("b", record.get("B")); + assertEquals("c", record.get("C")); + + // 1st record + record = records.next(); + assertTrue(record.isMapped("A")); + assertTrue(record.isMapped("B")); + assertTrue(record.isMapped("C")); + assertTrue(record.isSet("A")); + assertTrue(record.isSet("B")); + assertFalse(record.isSet("C")); + assertEquals("1", record.get("A")); + assertEquals("2", record.get("B")); + + // 2nd record + record = records.next(); + assertTrue(record.isMapped("A")); + assertTrue(record.isMapped("B")); + assertTrue(record.isMapped("C")); + assertTrue(record.isSet("A")); + assertTrue(record.isSet("B")); + assertTrue(record.isSet("C")); + assertEquals("x", record.get("A")); + assertEquals("y", record.get("B")); + assertEquals("z", record.get("C")); + + assertFalse(records.hasNext()); + } + public void testGetHeaderMap() throws Exception { final CSVParser parser = new CSVParser("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C")); final Map<String, Integer> headerMap = parser.getHeaderMap();