Skip to content

Commit 70485e4

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

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 int getInt(String name) {
150+
String s = this.get(name);
151+
return s != null ? Integer.parseInt(s) : 0;
152+
}
153+
137154
/**
138155
* Returns a value by name.
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 CSVRecordIntTest {
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, " + Integer.MAX_VALUE + ", " + Integer.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 testGetIntegerByMissingString() {
47+
Assert.assertEquals(null, Integer.valueOf(record.getInt("ABSENT")));
48+
}
49+
50+
@Test(expected = IllegalArgumentException.class)
51+
public void testGetIntegerByNullString() {
52+
Assert.assertEquals(null, Integer.valueOf(record.getInt(null)));
53+
}
54+
55+
@Test
56+
public void testGetIntegerByString() {
57+
Assert.assertEquals(-1, record.getInt("A"));
58+
Assert.assertEquals(0, record.getInt("B"));
59+
Assert.assertEquals(1, record.getInt("C"));
60+
Assert.assertEquals(Integer.MAX_VALUE, record.getInt("D"));
61+
Assert.assertEquals(Integer.MIN_VALUE, record.getInt("E"));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)