eol native
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1302383 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
101a040805
commit
10bf28ee2e
|
@ -1,88 +1,88 @@
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
* this work for additional information regarding copyright ownership.
|
* this work for additional information regarding copyright ownership.
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.apache.commons.csv;
|
package org.apache.commons.csv;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A CSV record
|
* A CSV record
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bourg
|
* @author Emmanuel Bourg
|
||||||
*/
|
*/
|
||||||
public class CSVRecord implements Serializable, Iterable<String> {
|
public class CSVRecord implements Serializable, Iterable<String> {
|
||||||
|
|
||||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||||
|
|
||||||
/** The values of the record */
|
/** The values of the record */
|
||||||
private final String[] values;
|
private final String[] values;
|
||||||
|
|
||||||
/** The column name to index mapping. */
|
/** The column name to index mapping. */
|
||||||
private final Map<String, Integer> mapping;
|
private final Map<String, Integer> mapping;
|
||||||
|
|
||||||
CSVRecord(String[] values, Map<String, Integer> mapping) {
|
CSVRecord(String[] values, Map<String, Integer> mapping) {
|
||||||
this.values = values != null ? values : EMPTY_STRING_ARRAY;
|
this.values = values != null ? values : EMPTY_STRING_ARRAY;
|
||||||
this.mapping = mapping;
|
this.mapping = mapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a value by index.
|
* Returns a value by index.
|
||||||
*
|
*
|
||||||
* @param i the index of the column retrieved
|
* @param i the index of the column retrieved
|
||||||
*/
|
*/
|
||||||
public String get(int i) {
|
public String get(int i) {
|
||||||
return values[i];
|
return values[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a value by name.
|
* Returns a value by name.
|
||||||
*
|
*
|
||||||
* @param name the name of the column retrieved
|
* @param name the name of the column retrieved
|
||||||
*/
|
*/
|
||||||
public String get(String name) {
|
public String get(String name) {
|
||||||
if (mapping == null) {
|
if (mapping == null) {
|
||||||
throw new IllegalStateException("No header was specified, the record values can't be accessed by name");
|
throw new IllegalStateException("No header was specified, the record values can't be accessed by name");
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer index = mapping.get(name);
|
Integer index = mapping.get(name);
|
||||||
|
|
||||||
return index != null ? values[index.intValue()] : null;
|
return index != null ? values[index.intValue()] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterator<String> iterator() {
|
public Iterator<String> iterator() {
|
||||||
return Arrays.asList(values).iterator();
|
return Arrays.asList(values).iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] values() {
|
String[] values() {
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of values in this record.
|
* Returns the number of values in this record.
|
||||||
*/
|
*/
|
||||||
public int size() {
|
public int size() {
|
||||||
return values.length;
|
return values.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return Arrays.toString(values);
|
return Arrays.toString(values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue