Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV-215 CSV Record mutability #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/org/apache/commons/csv/CSVRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -49,6 +54,8 @@ public final class CSVRecord implements Serializable, Iterable<String> {

/** The values of the record */
private final String[] values;
/** Determines the mutability of the record*/
private boolean isMutable;

CSVRecord(final String[] values, final Map<String, Integer> mapping, final String comment, final long recordNumber,
final long characterPosition) {
Expand All @@ -57,6 +64,34 @@ public final class CSVRecord implements Serializable, Iterable<String> {
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;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> map, final boolean allowsNulls) {
assertTrue(map.containsKey("first"));
Expand Down