Skip to content

Commit 9256af7

Browse files
committed
Turn CSVRecord into a List
CSVRecord implements get(int) and size() methods so it’s already pretty much a list. However, because it does not implement the List interface, users cannot take advantage of convenience methods such as subList nor can they pass records around to methods accepting lists. Make CSVRecord extend AbstractList so that it implements List interface. Because AbstractList pravides iterator() implementation, this allows us to delete said method (alongside toList() method) reducing amount of code.
1 parent 0ab2b08 commit 9256af7

3 files changed

Lines changed: 17 additions & 23 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<action issue="CSV-233" type="add" dev="ggregory" due-to="Gary Gregory">Add predefined CSVFormats for printing MongoDB CSV and TSV.</action>
4343
<action issue="CSV-208" type="fix" dev="ggregory" due-to="Jurrie Overgoor">Fix escape character for POSTGRESQL_TEXT and POSTGRESQL_CSV formats.</action>
4444
<action issue="CSV-232" type="fix" dev="ggregory" due-to="Jurrie Overgoor, Gary Gregory">Site link "Source Repository" does not work.</action>
45+
<action type="add" dev="mina86" due-to="Michal Nazarewicz">Turn CSVRecord into a List.</action>
4546
</release>
4647
<release version="1.6" date="2018-09-22" description="Feature and bug fix release">
4748
<action issue="CSV-231" type="update" dev="britter">Add more documentation to CSVPrinter.</action>

src/main/java/org/apache/commons/csv/CSVRecord.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.commons.csv;
1919

2020
import java.io.Serializable;
21+
import java.util.AbstractList;
2122
import java.util.Arrays;
2223
import java.util.HashMap;
2324
import java.util.Iterator;
@@ -28,7 +29,7 @@
2829
/**
2930
* A CSV record parsed from a CSV file.
3031
*/
31-
public final class CSVRecord implements Serializable, Iterable<String> {
32+
public final class CSVRecord extends AbstractList<String> implements Serializable {
3233

3334
private static final String[] EMPTY_STRING_ARRAY = new String[0];
3435

@@ -75,6 +76,7 @@ public String get(final Enum<?> e) {
7576
* a column index (0-based)
7677
* @return the String at the given index
7778
*/
79+
@Override
7880
public String get(final int i) {
7981
return values[i];
8082
}
@@ -197,16 +199,6 @@ public boolean isSet(final String name) {
197199
return isMapped(name) && mapping.get(name).intValue() < values.length;
198200
}
199201

200-
/**
201-
* Returns an iterator over the values of this record.
202-
*
203-
* @return an iterator over the values of this record.
204-
*/
205-
@Override
206-
public Iterator<String> iterator() {
207-
return toList().iterator();
208-
}
209-
210202
/**
211203
* Puts all values of this record into the given Map.
212204
*
@@ -232,21 +224,11 @@ <M extends Map<String, String>> M putIn(final M map) {
232224
*
233225
* @return the number of values.
234226
*/
227+
@Override
235228
public int size() {
236229
return values.length;
237230
}
238231

239-
/**
240-
* Converts the values to a List.
241-
*
242-
* TODO: Maybe make this public?
243-
*
244-
* @return a new List
245-
*/
246-
private List<String> toList() {
247-
return Arrays.asList(values);
248-
}
249-
250232
/**
251233
* Copies this record into a new Map. The new map is not connect
252234
*

src/test/java/org/apache/commons/csv/CSVRecordTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
public class CSVRecordTest {
3838

39-
private enum EnumFixture { UNKNOWN_COLUMN }
39+
private enum EnumFixture { first, second, third, UNKNOWN_COLUMN }
4040

4141
private String[] values;
4242
private CSVRecord record, recordWithHeader;
@@ -67,6 +67,13 @@ public void testGetString() {
6767
assertEquals(values[2], recordWithHeader.get("third"));
6868
}
6969

70+
@Test
71+
public void testGetEnum() {
72+
assertEquals(values[0], recordWithHeader.get(EnumFixture.first));
73+
assertEquals(values[1], recordWithHeader.get(EnumFixture.second));
74+
assertEquals(values[2], recordWithHeader.get(EnumFixture.third));
75+
}
76+
7077
@Test(expected = IllegalArgumentException.class)
7178
public void testGetStringInconsistentRecord() {
7279
header.put("fourth", Integer.valueOf(4));
@@ -193,4 +200,8 @@ private void validateMap(final Map<String, String> map, final boolean allowsNull
193200
assertEquals(null, map.get("fourth"));
194201
}
195202

203+
@Test
204+
public void testToString() {
205+
assertEquals("CSVRecord [comment=null, mapping=null, recordNumber=0, values=[A, B, C]]", record.toString());
206+
}
196207
}

0 commit comments

Comments
 (0)