http://mail-archives.apache.org/mod_mbox/commons-dev/201401.mbox/%3C52D5C3A7.4060004%40sandglass-software.com%3E
http://mail-archives.apache.org/mod_mbox/commons-dev/201401.mbox/%3C52D7C612.8040601%40sandglass-software.com%3E
Adrian Crum
Sandglass Software
www.sandglass-software.com
On 1/21/2014 7:58 AM, Gary Gregory wrote:
On Tue, Jan 21, 2014 at 6:20 AM, Adrian Crum <
adrian.c...@sandglass-software.com> wrote:
This looks really ugly. How do I update the CSVRecord using Map.put()?
Is there a reason you didn't use the design I proposed?
What design is that? Obviously not in this thread but a reference would be
helpful.
Gary
Adrian Crum
Sandglass Software
www.sandglass-software.com
On 1/20/2014 9:12 PM, ggreg...@apache.org wrote:
Author: ggregory
Date: Tue Jan 21 02:12:02 2014
New Revision: 1559905
URL: http://svn.apache.org/r1559905
Log:
[CSV-105] Add Map conversion API to CSVRecord.
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/
csv/CSVRecord.java
commons/proper/csv/trunk/src/test/java/org/apache/commons/
csv/CSVRecordTest.java
Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/
csv/CSVRecord.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/
main/java/org/apache/commons/csv/CSVRecord.java?rev=
1559905&r1=1559904&r2=1559905&view=diff
============================================================
==================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
(original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
Tue Jan 21 02:12:02 2014
@@ -19,8 +19,10 @@ package org.apache.commons.csv;
import java.io.Serializable;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
+import java.util.Map.Entry;
/**
* A CSV record parsed from a CSV file.
@@ -168,6 +170,19 @@ public final class CSVRecord implements
}
/**
+ * Puts all values of this record into the given Map.
+ *
+ * @param map The Map to populate.
+ * @return the given map.
+ */
+ public Map<String, String> putIn(Map<String, String> map) {
+ for (Entry<String, Integer> entry : mapping.entrySet()) {
+ map.put(entry.getKey(), values[entry.getValue().
intValue()]);
+ }
+ return map;
+ }
+
+ /**
* Returns the number of values in this record.
*
* @return the number of values.
@@ -176,6 +191,15 @@ public final class CSVRecord implements
return values.length;
}
+ /**
+ * Converts this record into a Map.
+ *
+ * @return A new Map. The map is empty if the record has no headers.
+ */
+ public Map<String, String> toMap() {
+ return putIn(new HashMap<String, String>(values.length));
+ }
+
@Override
public String toString() {
return Arrays.toString(values);
Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/
csv/CSVRecordTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/
test/java/org/apache/commons/csv/CSVRecordTest.java?rev=
1559905&r1=1559904&r2=1559905&view=diff
============================================================
==================
---
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java
(original)
+++
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordTest.java
Tue Jan 21 02:12:02 2014
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertTru
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
import org.junit.Before;
import org.junit.Test;
@@ -37,7 +38,7 @@ public class CSVRecordTest {
@Before
public void setUp() throws Exception {
- values = new String[] { "first", "second", "third" };
+ values = new String[] { "A", "B", "C" };
record = new CSVRecord(values, null, null, 0);
header = new HashMap<String, Integer>();
header.put("first", Integer.valueOf(0));
@@ -123,4 +124,31 @@ public class CSVRecordTest {
}
}
+ @Test
+ public void testPutInMap() {
+ Map<String, String> map = new ConcurrentHashMap<String,
String>();
+ this.recordWithHeader.putIn(map);
+ this.validateMap(map, false);
+ }
+
+ @Test
+ public void testToMap() {
+ Map<String, String> map = this.recordWithHeader.toMap();
+ this.validateMap(map, true);
+ }
+
+ private void validateMap(Map<String, String> map, boolean
allowsNulls) {
+ assertTrue(map.containsKey("first"));
+ assertTrue(map.containsKey("second"));
+ assertTrue(map.containsKey("third"));
+ assertFalse(map.containsKey("fourth"));
+ if (allowsNulls) {
+ assertFalse(map.containsKey(null));
+ }
+ assertEquals("A", map.get("first"));
+ assertEquals("B", map.get("second"));
+ assertEquals("C", map.get("third"));
+ assertEquals(null, map.get("fourth"));
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org