diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java index d2cbe6ac03..02537d3ad8 100644 --- a/src/main/java/org/apache/commons/csv/CSVRecord.java +++ b/src/main/java/org/apache/commons/csv/CSVRecord.java @@ -25,6 +25,11 @@ import java.util.Map; import java.util.Map.Entry; +class ImmutableRecordCantBeSetException extends Exception{ + public ImmutableRecordCantBeSetException(String message){ + super(message); + } +} /** * A CSV record parsed from a CSV file. * @@ -49,6 +54,8 @@ public final class CSVRecord implements Serializable, Iterable { /** The values of the record */ private final String[] values; + /** Determines the mutability of the record*/ + private boolean isMutable; CSVRecord(final String[] values, final Map mapping, final String comment, final long recordNumber, final long characterPosition) { @@ -57,6 +64,34 @@ public final class CSVRecord implements Serializable, Iterable { this.mapping = mapping; this.comment = comment; this.characterPosition = characterPosition; + /** By default records are immutable*/ + this.isMutable = false; + + } + /** + * Make this instance Mutable. + * */ + public void makeCSVRecordMutable(){ + this.isMutable = true; + } + public boolean isCSVRecordMutable(){ + return this.isMutable; + } + /** + * Sets a value by index. + * + * @param index index + * a column index (0-based) + * @param value + * a string value to replace the current data + * + * @throws ImmutableRecordCantBeSetException incase it's called on an immutable instance. + */ + public void set(int index, String value) throws ImmutableRecordCantBeSetException { + if(!isMutable){ + throw new ImmutableRecordCantBeSetException("Attempt to change Immutable CSV Record"); + } + values[index] = value; } /** diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java index 6347cc51a4..a0399558f4 100644 --- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java +++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java @@ -178,6 +178,28 @@ public void testToMapWithNoHeader() throws Exception { assertTrue("Map is empty.", map.isEmpty()); } } + @Test + public void testSettingRecordMutable() throws Exception{ + try(final CSVParser parser = CSVParser.parse("a,b", CSVFormat.newFormat(','))){ + final CSVRecord shortRec = parser.iterator().next(); + shortRec.makeCSVRecordMutable(); + assertTrue(shortRec.isCSVRecordMutable()); + } + } + @Test + public void testDefaultImmutability() throws Exception{ + try(final CSVParser parser = CSVParser.parse("a,b", CSVFormat.newFormat(','))){ + final CSVRecord shortRec = parser.iterator().next(); + assertFalse(shortRec.isCSVRecordMutable()); + } + } + @Test(expected = ImmutableRecordCantBeSetException.class) + public void testImmutableRecordSetException() throws Exception{ + try(final CSVParser parser = CSVParser.parse("a,b", CSVFormat.newFormat(','))){ + final CSVRecord shortRec = parser.iterator().next(); + shortRec.set(0,"new value"); + } + } private void validateMap(final Map map, final boolean allowsNulls) { assertTrue(map.containsKey("first"));