Skip to content

Commit 2a5ddb7

Browse files
committed
[CSV-239] Cannot get headers in column order from CSVRecord.
* fix indentation * add javadoc to Headers class * rename method to createHeaders * use String.format to build error message * initialize header names List with appropriate size
1 parent f7ce795 commit 2a5ddb7

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,7 @@ public CSVFormat withAllowDuplicateHeaderNames() {
22692269
return withAllowDuplicateHeaderNames(true);
22702270
}
22712271

2272-
public boolean getAllowDuplicateHeaderNames() {
2272+
public boolean getAllowDuplicateHeaderNames() {
22732273
return allowDuplicateHeaderNames;
22742274
}
22752275
}

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public CSVParser(final Reader reader, final CSVFormat format, final long charact
410410
this.format = format;
411411
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
412412
this.csvRecordIterator = new CSVRecordIterator();
413-
Headers headers = createHeaderMapAndHeaderNames();
413+
Headers headers = createHeaders();
414414
this.headerMap = headers.headerMap;
415415
this.headerNames = headers.headerNames;
416416
this.characterOffset = characterOffset;
@@ -446,8 +446,18 @@ private Map<String, Integer> createEmptyHeaderMap() {
446446
new LinkedHashMap<>();
447447
}
448448

449+
/**
450+
* Header information based on name and position.
451+
*/
449452
private static final class Headers {
453+
/**
454+
* Header column positions (0-based)
455+
*/
450456
final Map<String, Integer> headerMap;
457+
458+
/**
459+
* Header names in column order
460+
*/
451461
final List<String> headerNames;
452462

453463
Headers(Map<String, Integer> headerMap, List<String> headerNames) {
@@ -462,7 +472,7 @@ private static final class Headers {
462472
* @return null if the format has no header.
463473
* @throws IOException if there is a problem reading the header or skipping the first record
464474
*/
465-
private Headers createHeaderMapAndHeaderNames() throws IOException {
475+
private Headers createHeaders() throws IOException {
466476
Map<String, Integer> hdrMap = null;
467477
List<String> headerNames = null;
468478
final String[] formatHeader = this.format.getHeader();
@@ -490,9 +500,10 @@ private Headers createHeaderMapAndHeaderNames() throws IOException {
490500
final boolean emptyHeader = header == null || header.trim().isEmpty();
491501
if (containsHeader) {
492502
if (!emptyHeader && !this.format.getAllowDuplicateHeaderNames()) {
493-
throw new IllegalArgumentException("The header contains a duplicate name: \"" + header
494-
+ "\" in " + Arrays.toString(headerRecord)
495-
+ ". If this is valid then use CSVFormat.withAllowDuplicateHeaderNames().");
503+
throw new IllegalArgumentException(
504+
String.format("The header contains a duplicate name: \"%s\" in %s."
505+
+ " If this is valid then use CSVFormat.withAllowDuplicateHeaderNames().",
506+
header, Arrays.toString(headerRecord)));
496507
}
497508
if (emptyHeader && !this.format.getAllowMissingColumnNames()) {
498509
throw new IllegalArgumentException(
@@ -502,7 +513,7 @@ private Headers createHeaderMapAndHeaderNames() throws IOException {
502513
if (header != null) {
503514
hdrMap.put(header, Integer.valueOf(i));
504515
if (headerNames == null) {
505-
headerNames = new ArrayList<>();
516+
headerNames = new ArrayList<>(headerRecord.length);
506517
}
507518
headerNames.add(header);
508519
}

0 commit comments

Comments
 (0)