Author: ggregory
Date: Thu Aug  1 21:54:37 2013
New Revision: 1509449

URL: http://svn.apache.org/r1509449
Log:
Add CSVRecord#getLong(String) API and tests.

Added:
    
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordLongTest.java
   (with props)
Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.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=1509449&r1=1509448&r2=1509449&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 Aug  1 21:54:37 2013
@@ -123,7 +123,7 @@ public class CSVRecord implements Serial
         String s = this.get(name);
         return s != null ? Boolean.parseBoolean(s) : false;
     }
-   
+
     /**
      * Returns the comment for this record, if any.
      *
@@ -135,6 +135,23 @@ public class CSVRecord implements Serial
     }
 
     /**
+     * Returns a value by name.
+     *
+     * @param name
+     *            the name of the column to be retrieved.
+     * @return the column value
+     * @throws IllegalStateException
+     *             if no header mapping was provided
+     * @throws IllegalArgumentException
+     *             if the record is inconsistent
+     * @see #isConsistent()
+     */
+    public long getLong(String name) {
+        String s = this.get(name);
+        return s != null ? Long.parseLong(s) : 0;
+    }
+
+    /**
      * Returns the number of this record in the parsed CSV file.
      *
      * @return the number of this record.

Added: 
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordLongTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordLongTest.java?rev=1509449&view=auto
==============================================================================
--- 
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordLongTest.java
 (added)
+++ 
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordLongTest.java
 Thu Aug  1 21:54:37 2013
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.csv;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CSVRecordLongTest {
+
+    private CSVRecord record;
+
+    /**
+     * @return
+     * @throws IOException
+     */
+    private CSVRecord createTestRecord() throws IOException {
+        String csv = "A, B, C, D, E\n-1, 0, 1, " + Long.MAX_VALUE + ", " + 
Long.MIN_VALUE;
+        CSVRecord record = CSVParser.parseString(csv, 
CSVFormat.DEFAULT.withHeader().withIgnoreSurroundingSpaces(true))
+                .iterator().next();
+        return record;
+    }
+
+    @Before
+    public void setUp() throws IOException {
+        this.record = createTestRecord();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetLongByMissingString() {
+        Assert.assertEquals(null, Long.valueOf(record.getLong("ABSENT")));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetLongByNullString() {
+        Assert.assertEquals(null, Long.valueOf(record.getLong(null)));
+    }
+
+    @Test
+    public void testGetLongByString() {
+        Assert.assertEquals(-1, record.getLong("A"));
+        Assert.assertEquals(0, record.getLong("B"));
+        Assert.assertEquals(1, record.getLong("C"));
+        Assert.assertEquals(Long.MAX_VALUE, record.getLong("D"));
+        Assert.assertEquals(Long.MIN_VALUE, record.getLong("E"));
+    }
+
+}

Propchange: 
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordLongTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVRecordLongTest.java
------------------------------------------------------------------------------
    svn:keywords = Id


Reply via email to