Skip to content

Commit 88d6886

Browse files
committed
fix: sonarcloud code smell
1 parent 157b35a commit 88d6886

1 file changed

Lines changed: 37 additions & 32 deletions

File tree

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

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -504,38 +504,7 @@ private Headers createHeaders() throws IOException {
504504
}
505505

506506
// build the name to index mappings
507-
if (headerRecord != null) {
508-
// Track an occurrence of a null, empty or blank header.
509-
boolean observedMissing = false;
510-
for (int i = 0; i < headerRecord.length; i++) {
511-
final String header = headerRecord[i];
512-
final boolean blankHeader = CSVFormat.isBlank(header);
513-
if (blankHeader && !this.format.getAllowMissingColumnNames()) {
514-
throw new IllegalArgumentException(
515-
"A header name is missing in " + Arrays.toString(headerRecord));
516-
}
517-
518-
final boolean containsHeader = blankHeader ? observedMissing : hdrMap.containsKey(header);
519-
final DuplicateHeaderMode headerMode = this.format.getDuplicateHeaderMode();
520-
final boolean duplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_ALL;
521-
final boolean emptyDuplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_EMPTY;
522-
523-
if (containsHeader && !duplicatesAllowed && !(blankHeader && emptyDuplicatesAllowed)) {
524-
throw new IllegalArgumentException(
525-
String.format(
526-
"The header contains a duplicate name: \"%s\" in %s. If this is valid then use CSVFormat.Builder.setDuplicateHeaderMode().",
527-
header, Arrays.toString(headerRecord)));
528-
}
529-
observedMissing |= blankHeader;
530-
if (header != null) {
531-
hdrMap.put(header, Integer.valueOf(i));
532-
if (headerNames == null) {
533-
headerNames = new ArrayList<>(headerRecord.length);
534-
}
535-
headerNames.add(header);
536-
}
537-
}
538-
}
507+
headerNames = getHeaderNames(headerRecord, hdrMap, headerNames);
539508
}
540509
if (headerNames == null) {
541510
headerNames = Collections.emptyList(); // immutable
@@ -545,6 +514,42 @@ private Headers createHeaders() throws IOException {
545514
return new Headers(hdrMap, headerNames);
546515
}
547516

517+
private List<String> getHeaderNames(String[] headerRecord, Map<String, Integer> hdrMap, List<String> headerNames) {
518+
if (headerRecord != null) {
519+
// Track an occurrence of a null, empty or blank header.
520+
boolean observedMissing = false;
521+
for (int i = 0; i < headerRecord.length; i++) {
522+
final String header = headerRecord[i];
523+
final boolean blankHeader = CSVFormat.isBlank(header);
524+
if (blankHeader && !this.format.getAllowMissingColumnNames()) {
525+
throw new IllegalArgumentException(
526+
"A header name is missing in " + Arrays.toString(headerRecord));
527+
}
528+
529+
final boolean containsHeader = blankHeader ? observedMissing : hdrMap.containsKey(header);
530+
final DuplicateHeaderMode headerMode = this.format.getDuplicateHeaderMode();
531+
final boolean duplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_ALL;
532+
final boolean emptyDuplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_EMPTY;
533+
534+
if (containsHeader && !duplicatesAllowed && !(blankHeader && emptyDuplicatesAllowed)) {
535+
throw new IllegalArgumentException(
536+
String.format(
537+
"The header contains a duplicate name: \"%s\" in %s. If this is valid then use CSVFormat.Builder.setDuplicateHeaderMode().",
538+
header, Arrays.toString(headerRecord)));
539+
}
540+
observedMissing |= blankHeader;
541+
if (header != null) {
542+
hdrMap.put(header, Integer.valueOf(i));
543+
if (headerNames == null) {
544+
headerNames = new ArrayList<>(headerRecord.length);
545+
}
546+
headerNames.add(header);
547+
}
548+
}
549+
}
550+
return headerNames;
551+
}
552+
548553
/**
549554
* Gets the current line number in the input stream.
550555
*

0 commit comments

Comments
 (0)