|
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 | | - |
18 | | -package org.apache.commons.csv; |
19 | | - |
20 | | -import java.io.Serializable; |
21 | | -import java.util.Arrays; |
22 | | -import java.util.Iterator; |
23 | | -import java.util.Map; |
24 | | - |
25 | | -/** |
26 | | - * A CSV record |
27 | | - * |
28 | | - * @author Emmanuel Bourg |
29 | | - */ |
30 | | -public class CSVRecord implements Serializable, Iterable<String> { |
31 | | - |
32 | | - private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
33 | | - |
34 | | - /** The values of the record */ |
35 | | - private final String[] values; |
36 | | - |
37 | | - /** The column name to index mapping. */ |
38 | | - private final Map<String, Integer> mapping; |
39 | | - |
40 | | - CSVRecord(String[] values, Map<String, Integer> mapping) { |
41 | | - this.values = values != null ? values : EMPTY_STRING_ARRAY; |
42 | | - this.mapping = mapping; |
43 | | - } |
44 | | - |
45 | | - /** |
46 | | - * Returns a value by index. |
47 | | - * |
48 | | - * @param i the index of the column retrieved |
49 | | - */ |
50 | | - public String get(int i) { |
51 | | - return values[i]; |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * Returns a value by name. |
56 | | - * |
57 | | - * @param name the name of the column retrieved |
58 | | - */ |
59 | | - public String get(String name) { |
60 | | - if (mapping == null) { |
61 | | - throw new IllegalStateException("No header was specified, the record values can't be accessed by name"); |
62 | | - } |
63 | | - |
64 | | - Integer index = mapping.get(name); |
65 | | - |
66 | | - return index != null ? values[index.intValue()] : null; |
67 | | - } |
68 | | - |
69 | | - public Iterator<String> iterator() { |
70 | | - return Arrays.asList(values).iterator(); |
71 | | - } |
72 | | - |
73 | | - String[] values() { |
74 | | - return values; |
75 | | - } |
76 | | - |
77 | | - /** |
78 | | - * Returns the number of values in this record. |
79 | | - */ |
80 | | - public int size() { |
81 | | - return values.length; |
82 | | - } |
83 | | - |
84 | | - @Override |
85 | | - public String toString() { |
86 | | - return Arrays.toString(values); |
87 | | - } |
88 | | -} |
| 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 | + |
| 18 | +package org.apache.commons.csv; |
| 19 | + |
| 20 | +import java.io.Serializable; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +/** |
| 26 | + * A CSV record |
| 27 | + * |
| 28 | + * @author Emmanuel Bourg |
| 29 | + */ |
| 30 | +public class CSVRecord implements Serializable, Iterable<String> { |
| 31 | + |
| 32 | + private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 33 | + |
| 34 | + /** The values of the record */ |
| 35 | + private final String[] values; |
| 36 | + |
| 37 | + /** The column name to index mapping. */ |
| 38 | + private final Map<String, Integer> mapping; |
| 39 | + |
| 40 | + CSVRecord(String[] values, Map<String, Integer> mapping) { |
| 41 | + this.values = values != null ? values : EMPTY_STRING_ARRAY; |
| 42 | + this.mapping = mapping; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns a value by index. |
| 47 | + * |
| 48 | + * @param i the index of the column retrieved |
| 49 | + */ |
| 50 | + public String get(int i) { |
| 51 | + return values[i]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns a value by name. |
| 56 | + * |
| 57 | + * @param name the name of the column retrieved |
| 58 | + */ |
| 59 | + public String get(String name) { |
| 60 | + if (mapping == null) { |
| 61 | + throw new IllegalStateException("No header was specified, the record values can't be accessed by name"); |
| 62 | + } |
| 63 | + |
| 64 | + Integer index = mapping.get(name); |
| 65 | + |
| 66 | + return index != null ? values[index.intValue()] : null; |
| 67 | + } |
| 68 | + |
| 69 | + public Iterator<String> iterator() { |
| 70 | + return Arrays.asList(values).iterator(); |
| 71 | + } |
| 72 | + |
| 73 | + String[] values() { |
| 74 | + return values; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the number of values in this record. |
| 79 | + */ |
| 80 | + public int size() { |
| 81 | + return values.length; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String toString() { |
| 86 | + return Arrays.toString(values); |
| 87 | + } |
| 88 | +} |
0 commit comments