File tree Expand file tree Collapse file tree
main/java/org/apache/commons/csv
test/java/org/apache/commons/csv Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,21 +60,32 @@ public String get(int i) {
6060 * Returns a value by name.
6161 *
6262 * @param name
63- * the name of the column to be retrieved
63+ * the name of the column to be retrieved.
6464 * @return the column value, or {@code null} if the column name is not found
6565 * @throws IllegalStateException
6666 * if no header mapping was provided
6767 */
6868 public String get (String name ) {
6969 if (mapping == null ) {
70- throw new IllegalStateException ("No header was specified, the record values can't be accessed by name" );
70+ throw new IllegalStateException ("No header mapping was specified, the record values can't be accessed by name" );
7171 }
7272
7373 Integer index = mapping .get (name );
7474
7575 return index != null ? values [index .intValue ()] : null ;
7676 }
7777
78+ /**
79+ * Checks whether a given columns is mapped.
80+ *
81+ * @param name
82+ * the name of the column to be retrieved.
83+ * @return whether a given columns is mapped.
84+ */
85+ public boolean isMapped (String name ) {
86+ return mapping != null ? mapping .containsKey (name ) : false ;
87+ }
88+
7889 public Iterator <String > iterator () {
7990 return Arrays .asList (values ).iterator ();
8091 }
Original file line number Diff line number Diff line change @@ -503,6 +503,10 @@ public void testProvidedHeader() throws Exception {
503503 for (int i = 0 ; i < 3 ; i ++) {
504504 assertTrue (records .hasNext ());
505505 CSVRecord record = records .next ();
506+ assertTrue (record .isMapped ("A" ));
507+ assertTrue (record .isMapped ("B" ));
508+ assertTrue (record .isMapped ("C" ));
509+ assertFalse (record .isMapped ("NOT MAPPED" ));
506510 assertEquals (record .get (0 ), record .get ("A" ));
507511 assertEquals (record .get (1 ), record .get ("B" ));
508512 assertEquals (record .get (2 ), record .get ("C" ));
You can’t perform that action at this time.
0 commit comments