commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1397094 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVRecord.java test/java/org/apache/commons/csv/CSVParserTest.java
Date Thu, 11 Oct 2012 14:56:16 GMT
Author: ggregory
Date: Thu Oct 11 14:56:16 2012
New Revision: 1397094

URL: http://svn.apache.org/viewvc?rev=1397094&view=rev
Log:
Add CVSRecord.isMapped(String) API.

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/CSVParserTest.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=1397094&r1=1397093&r2=1397094&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 Thu Oct 11
14:56:16 2012
@@ -60,14 +60,14 @@ public class CSVRecord implements Serial
      * Returns a value by name.
      *
      * @param name
-     *            the name of the column to be retrieved
+     *            the name of the column to be retrieved.
      * @return the column value, or {@code null} if the column name is not found
      * @throws IllegalStateException
      *             if no header mapping was provided
      */
     public String get(String name) {
         if (mapping == null) {
-            throw new IllegalStateException("No header was specified, the record values can't
be accessed by name");
+            throw new IllegalStateException("No header mapping was specified, the record
values can't be accessed by name");
         }
 
         Integer index = mapping.get(name);
@@ -75,6 +75,17 @@ public class CSVRecord implements Serial
         return index != null ? values[index.intValue()] : null;
     }
 
+    /**
+     * Checks whether a given columns is mapped.
+     * 
+     * @param name
+     *            the name of the column to be retrieved.
+     * @return whether a given columns is mapped.
+     */
+    public boolean isMapped(String name) {
+        return mapping != null ? mapping.containsKey(name) : false;
+    }
+    
     public Iterator<String> iterator() {
         return Arrays.asList(values).iterator();
     }

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1397094&r1=1397093&r2=1397094&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Thu Oct
11 14:56:16 2012
@@ -503,6 +503,10 @@ public class CSVParserTest {
         for (int i = 0; i < 3; i++) {
             assertTrue(records.hasNext());
             CSVRecord record = records.next();
+            assertTrue(record.isMapped("A"));
+            assertTrue(record.isMapped("B"));
+            assertTrue(record.isMapped("C"));
+            assertFalse(record.isMapped("NOT MAPPED"));
             assertEquals(record.get(0), record.get("A"));
             assertEquals(record.get(1), record.get("B"));
             assertEquals(record.get(2), record.get("C"));



Mime
View raw message