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
This commit is contained in:
Gary D. Gregory 2013-08-01 21:11:10 +00:00
parent 412d05d166
commit 50b7f79338
3 changed files with 110 additions and 9 deletions

View File

@ -80,12 +80,13 @@ public class CSVRecord implements Serializable, Iterable<String> {
* *
* @param 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 * @return the column value, maybe null depending on {@link CSVFormat#getNullString()}.
* @throws IllegalStateException * @throws IllegalStateException
* if no header mapping was provided * if no header mapping was provided
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the record is inconsistent * if {@code name} is not mapped or if the record is inconsistent
* @see #isConsistent() * @see #isConsistent()
* @see CSVFormat#withNullString(String)
*/ */
public String get(final String name) { public String get(final String name) {
if (mapping == null) { if (mapping == null) {
@ -93,16 +94,36 @@ public class CSVRecord implements Serializable, Iterable<String> {
"No header mapping was specified, the record values can't be accessed by name"); "No header mapping was specified, the record values can't be accessed by name");
} }
final Integer index = mapping.get(name); final Integer index = mapping.get(name);
if (index == null) {
throw new IllegalArgumentException(String.format("Mapping for %s not found, expected one of %s", name,
mapping.keySet()));
}
try { try {
return index != null ? values[index.intValue()] : null; return values[index.intValue()];
} catch (final ArrayIndexOutOfBoundsException e) { } catch (final ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException( throw new IllegalArgumentException(String.format(
String.format( "Index for header '%s' is %d but CSVRecord only has %d values!", name, index,
"Index for header '%s' is %d but CSVRecord only has %d values!", Integer.valueOf(values.length)));
name, index, Integer.valueOf(values.length)));
} }
} }
/**
* 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 boolean getBoolean(String name) {
String s = this.get(name);
return s != null ? Boolean.parseBoolean(s) : false;
}
/** /**
* Returns the comment for this record, if any. * Returns the comment for this record, if any.
* *

View File

@ -0,0 +1,63 @@
/*
* 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 CSVRecordBooleanTest {
private CSVRecord record;
@Before
public void setUp() throws IOException {
this.record = createTestRecord();
}
@Test
public void testGetBooleanByString() {
Assert.assertEquals(Boolean.TRUE, Boolean.valueOf(record.getBoolean("A")));
Assert.assertEquals(Boolean.TRUE, Boolean.valueOf(record.getBoolean("B")));
Assert.assertEquals(Boolean.FALSE, Boolean.valueOf(record.getBoolean("C")));
Assert.assertEquals(Boolean.FALSE, Boolean.valueOf(record.getBoolean("D")));
}
@Test(expected = IllegalArgumentException.class)
public void testGetBooleanByMissingString() {
Assert.assertEquals(null, Boolean.valueOf(record.getBoolean("ABSENT")));
}
@Test(expected = IllegalArgumentException.class)
public void testGetBooleanByNullString() {
Assert.assertEquals(null, Boolean.valueOf(record.getBoolean(null)));
}
/**
* @return
* @throws IOException
*/
private CSVRecord createTestRecord() throws IOException {
String csv = "A,B,C,D\ntrue, TRUE, false, foo";
CSVRecord record = CSVParser.parseString(csv, CSVFormat.DEFAULT.withHeader().withIgnoreSurroundingSpaces(true))
.iterator().next();
return record;
}
}

View File

@ -29,6 +29,8 @@ import org.junit.Test;
public class CSVRecordTest { public class CSVRecordTest {
private enum EnumFixture { UNKNOWN_COLUMN };
private String[] values; private String[] values;
private CSVRecord record, recordWithHeader; private CSVRecord record, recordWithHeader;
private Map<String, Integer> header; private Map<String, Integer> header;
@ -69,11 +71,26 @@ public class CSVRecordTest {
recordWithHeader.get("fourth"); recordWithHeader.get("fourth");
} }
@Test @Test(expected = IllegalArgumentException.class)
public void testGetUnmapped() { public void testGetUnmappedName() {
assertNull(recordWithHeader.get("fourth")); assertNull(recordWithHeader.get("fourth"));
} }
@Test(expected = IllegalArgumentException.class)
public void testGetUnmappedEnum() {
assertNull(recordWithHeader.get(EnumFixture.UNKNOWN_COLUMN));
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testGetUnmappedNegativeInt() {
assertNull(recordWithHeader.get(Integer.MIN_VALUE));
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testGetUnmappedPositiveInt() {
assertNull(recordWithHeader.get(Integer.MAX_VALUE));
}
@Test @Test
public void testIsConsistent() { public void testIsConsistent() {
assertTrue(record.isConsistent()); assertTrue(record.isConsistent());