Skip to content

Commit 50b7f79

Browse files
committed
CSVRecord.get(String) throws IAE if the column is not mapped (does not exist). This is similar to what JDBC does in ResultSet. Add getBoolean(String) API and tests.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1509431 13f79535-47bb-0310-9956-ffa450edef68
1 parent 412d05d commit 50b7f79

3 files changed

Lines changed: 110 additions & 9 deletions

File tree

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,50 @@ public String get(final int i) {
8080
*
8181
* @param name
8282
* the name of the column to be retrieved.
83-
* @return the column value, or {@code null} if the column name is not found
83+
* @return the column value, maybe null depending on {@link CSVFormat#getNullString()}.
8484
* @throws IllegalStateException
8585
* if no header mapping was provided
8686
* @throws IllegalArgumentException
87-
* if the record is inconsistent
87+
* if {@code name} is not mapped or if the record is inconsistent
8888
* @see #isConsistent()
89+
* @see CSVFormat#withNullString(String)
8990
*/
9091
public String get(final String name) {
9192
if (mapping == null) {
9293
throw new IllegalStateException(
9394
"No header mapping was specified, the record values can't be accessed by name");
9495
}
9596
final Integer index = mapping.get(name);
97+
if (index == null) {
98+
throw new IllegalArgumentException(String.format("Mapping for %s not found, expected one of %s", name,
99+
mapping.keySet()));
100+
}
96101
try {
97-
return index != null ? values[index.intValue()] : null;
102+
return values[index.intValue()];
98103
} catch (final ArrayIndexOutOfBoundsException e) {
99-
throw new IllegalArgumentException(
100-
String.format(
101-
"Index for header '%s' is %d but CSVRecord only has %d values!",
102-
name, index, Integer.valueOf(values.length)));
104+
throw new IllegalArgumentException(String.format(
105+
"Index for header '%s' is %d but CSVRecord only has %d values!", name, index,
106+
Integer.valueOf(values.length)));
103107
}
104108
}
105109

110+
/**
111+
* Returns a value by name.
112+
*
113+
* @param name
114+
* the name of the column to be retrieved.
115+
* @return the column value
116+
* @throws IllegalStateException
117+
* if no header mapping was provided
118+
* @throws IllegalArgumentException
119+
* if the record is inconsistent
120+
* @see #isConsistent()
121+
*/
122+
public boolean getBoolean(String name) {
123+
String s = this.get(name);
124+
return s != null ? Boolean.parseBoolean(s) : false;
125+
}
126+
106127
/**
107128
* Returns the comment for this record, if any.
108129
*
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 CSVRecordBooleanTest {
26+
27+
private CSVRecord record;
28+
29+
@Before
30+
public void setUp() throws IOException {
31+
this.record = createTestRecord();
32+
}
33+
34+
@Test
35+
public void testGetBooleanByString() {
36+
Assert.assertEquals(Boolean.TRUE, Boolean.valueOf(record.getBoolean("A")));
37+
Assert.assertEquals(Boolean.TRUE, Boolean.valueOf(record.getBoolean("B")));
38+
Assert.assertEquals(Boolean.FALSE, Boolean.valueOf(record.getBoolean("C")));
39+
Assert.assertEquals(Boolean.FALSE, Boolean.valueOf(record.getBoolean("D")));
40+
}
41+
42+
@Test(expected = IllegalArgumentException.class)
43+
public void testGetBooleanByMissingString() {
44+
Assert.assertEquals(null, Boolean.valueOf(record.getBoolean("ABSENT")));
45+
}
46+
47+
@Test(expected = IllegalArgumentException.class)
48+
public void testGetBooleanByNullString() {
49+
Assert.assertEquals(null, Boolean.valueOf(record.getBoolean(null)));
50+
}
51+
52+
/**
53+
* @return
54+
* @throws IOException
55+
*/
56+
private CSVRecord createTestRecord() throws IOException {
57+
String csv = "A,B,C,D\ntrue, TRUE, false, foo";
58+
CSVRecord record = CSVParser.parseString(csv, CSVFormat.DEFAULT.withHeader().withIgnoreSurroundingSpaces(true))
59+
.iterator().next();
60+
return record;
61+
}
62+
63+
}

src/test/java/org/apache/commons/csv/CSVRecordTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
public class CSVRecordTest {
3131

32+
private enum EnumFixture { UNKNOWN_COLUMN };
33+
3234
private String[] values;
3335
private CSVRecord record, recordWithHeader;
3436
private Map<String, Integer> header;
@@ -69,11 +71,26 @@ public void testGetStringInconsistentRecord() {
6971
recordWithHeader.get("fourth");
7072
}
7173

72-
@Test
73-
public void testGetUnmapped() {
74+
@Test(expected = IllegalArgumentException.class)
75+
public void testGetUnmappedName() {
7476
assertNull(recordWithHeader.get("fourth"));
7577
}
7678

79+
@Test(expected = IllegalArgumentException.class)
80+
public void testGetUnmappedEnum() {
81+
assertNull(recordWithHeader.get(EnumFixture.UNKNOWN_COLUMN));
82+
}
83+
84+
@Test(expected = ArrayIndexOutOfBoundsException.class)
85+
public void testGetUnmappedNegativeInt() {
86+
assertNull(recordWithHeader.get(Integer.MIN_VALUE));
87+
}
88+
89+
@Test(expected = ArrayIndexOutOfBoundsException.class)
90+
public void testGetUnmappedPositiveInt() {
91+
assertNull(recordWithHeader.get(Integer.MAX_VALUE));
92+
}
93+
7794
@Test
7895
public void testIsConsistent() {
7996
assertTrue(record.isConsistent());

0 commit comments

Comments
 (0)