Skip to content

Commit 4bb75e3

Browse files
committed
Add CSVRecord#getLong(String) API and tests.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1509449 13f79535-47bb-0310-9956-ffa450edef68
1 parent ac03c2e commit 4bb75e3

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public boolean getBoolean(String name) {
123123
String s = this.get(name);
124124
return s != null ? Boolean.parseBoolean(s) : false;
125125
}
126-
126+
127127
/**
128128
* Returns the comment for this record, if any.
129129
*
@@ -134,6 +134,23 @@ public String getComment() {
134134
return comment;
135135
}
136136

137+
/**
138+
* Returns a value by name.
139+
*
140+
* @param name
141+
* the name of the column to be retrieved.
142+
* @return the column value
143+
* @throws IllegalStateException
144+
* if no header mapping was provided
145+
* @throws IllegalArgumentException
146+
* if the record is inconsistent
147+
* @see #isConsistent()
148+
*/
149+
public long getLong(String name) {
150+
String s = this.get(name);
151+
return s != null ? Long.parseLong(s) : 0;
152+
}
153+
137154
/**
138155
* Returns the number of this record in the parsed CSV file.
139156
*
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.csv;
18+
19+
import java.io.IOException;
20+
21+
import org.junit.Assert;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
public class CSVRecordLongTest {
26+
27+
private CSVRecord record;
28+
29+
/**
30+
* @return
31+
* @throws IOException
32+
*/
33+
private CSVRecord createTestRecord() throws IOException {
34+
String csv = "A, B, C, D, E\n-1, 0, 1, " + Long.MAX_VALUE + ", " + Long.MIN_VALUE;
35+
CSVRecord record = CSVParser.parseString(csv, CSVFormat.DEFAULT.withHeader().withIgnoreSurroundingSpaces(true))
36+
.iterator().next();
37+
return record;
38+
}
39+
40+
@Before
41+
public void setUp() throws IOException {
42+
this.record = createTestRecord();
43+
}
44+
45+
@Test(expected = IllegalArgumentException.class)
46+
public void testGetLongByMissingString() {
47+
Assert.assertEquals(null, Long.valueOf(record.getLong("ABSENT")));
48+
}
49+
50+
@Test(expected = IllegalArgumentException.class)
51+
public void testGetLongByNullString() {
52+
Assert.assertEquals(null, Long.valueOf(record.getLong(null)));
53+
}
54+
55+
@Test
56+
public void testGetLongByString() {
57+
Assert.assertEquals(-1, record.getLong("A"));
58+
Assert.assertEquals(0, record.getLong("B"));
59+
Assert.assertEquals(1, record.getLong("C"));
60+
Assert.assertEquals(Long.MAX_VALUE, record.getLong("D"));
61+
Assert.assertEquals(Long.MIN_VALUE, record.getLong("E"));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)