Add CVSRecord.isSet(String) API.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397136 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6ab9b46e5a
commit
625bafab76
|
@ -69,14 +69,12 @@ public class CSVRecord implements Serializable, Iterable<String> {
|
|||
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 Serializable, Iterable<String> {
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue