diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java index b1b3f995..e063edc3 100644 --- a/src/main/java/org/apache/commons/csv/CSVRecord.java +++ b/src/main/java/org/apache/commons/csv/CSVRecord.java @@ -214,6 +214,17 @@ public final class CSVRecord implements Serializable, Iterable { return isMapped(name) && getHeaderMapRaw().get(name).intValue() < values.length; } + /** + * Checks whether a column with given index has a value. + * + * @param i + * a column index (0-based) + * @return whether a column with given index has a value + */ + public boolean isSet(final int i) { + return 0 <= i && i < values.length; + } + /** * Returns an iterator over the values of this record. * diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java index 68310027..06e771c4 100644 --- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java +++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java @@ -134,12 +134,22 @@ public class CSVRecordTest { } @Test - public void testIsSet() { + public void testIsSetString() { assertFalse(record.isSet("first")); assertTrue(recordWithHeader.isSet("first")); assertFalse(recordWithHeader.isSet("fourth")); } + @Test + public void testIsSetInt() { + assertFalse(record.isSet(-1)); + assertTrue(record.isSet(0)); + assertTrue(record.isSet(2)); + assertFalse(record.isSet(3)); + assertTrue(recordWithHeader.isSet(1)); + assertFalse(recordWithHeader.isSet(1000)); + } + @Test public void testIterator() { int i = 0;