Skip to content

Commit d01e6b5

Browse files
committed
[CSV-105] Add Map conversion API to CSVRecord.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1559905 13f79535-47bb-0310-9956-ffa450edef68
1 parent 64de57a commit d01e6b5

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
import java.io.Serializable;
2121
import java.util.Arrays;
22+
import java.util.HashMap;
2223
import java.util.Iterator;
2324
import java.util.Map;
25+
import java.util.Map.Entry;
2426

2527
/**
2628
* A CSV record parsed from a CSV file.
@@ -167,6 +169,19 @@ public Iterator<String> iterator() {
167169
return Arrays.asList(values).iterator();
168170
}
169171

172+
/**
173+
* Puts all values of this record into the given Map.
174+
*
175+
* @param map The Map to populate.
176+
* @return the given map.
177+
*/
178+
public Map<String, String> putIn(Map<String, String> map) {
179+
for (Entry<String, Integer> entry : mapping.entrySet()) {
180+
map.put(entry.getKey(), values[entry.getValue().intValue()]);
181+
}
182+
return map;
183+
}
184+
170185
/**
171186
* Returns the number of values in this record.
172187
*
@@ -176,6 +191,15 @@ public int size() {
176191
return values.length;
177192
}
178193

194+
/**
195+
* Converts this record into a Map.
196+
*
197+
* @return A new Map. The map is empty if the record has no headers.
198+
*/
199+
public Map<String, String> toMap() {
200+
return putIn(new HashMap<String, String>(values.length));
201+
}
202+
179203
@Override
180204
public String toString() {
181205
return Arrays.toString(values);

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.HashMap;
2525
import java.util.Map;
26+
import java.util.concurrent.ConcurrentHashMap;
2627

2728
import org.junit.Before;
2829
import org.junit.Test;
@@ -37,7 +38,7 @@ private enum EnumFixture { UNKNOWN_COLUMN }
3738

3839
@Before
3940
public void setUp() throws Exception {
40-
values = new String[] { "first", "second", "third" };
41+
values = new String[] { "A", "B", "C" };
4142
record = new CSVRecord(values, null, null, 0);
4243
header = new HashMap<String, Integer>();
4344
header.put("first", Integer.valueOf(0));
@@ -123,4 +124,31 @@ public void testIterator() {
123124
}
124125
}
125126

127+
@Test
128+
public void testPutInMap() {
129+
Map<String, String> map = new ConcurrentHashMap<String, String>();
130+
this.recordWithHeader.putIn(map);
131+
this.validateMap(map, false);
132+
}
133+
134+
@Test
135+
public void testToMap() {
136+
Map<String, String> map = this.recordWithHeader.toMap();
137+
this.validateMap(map, true);
138+
}
139+
140+
private void validateMap(Map<String, String> map, boolean allowsNulls) {
141+
assertTrue(map.containsKey("first"));
142+
assertTrue(map.containsKey("second"));
143+
assertTrue(map.containsKey("third"));
144+
assertFalse(map.containsKey("fourth"));
145+
if (allowsNulls) {
146+
assertFalse(map.containsKey(null));
147+
}
148+
assertEquals("A", map.get("first"));
149+
assertEquals("B", map.get("second"));
150+
assertEquals("C", map.get("third"));
151+
assertEquals(null, map.get("fourth"));
152+
}
153+
126154
}

0 commit comments

Comments
 (0)