From 8b23cbb6a813f6e99843c766f7f0718122d6dd85 Mon Sep 17 00:00:00 2001 From: Sigee Date: Thu, 19 Sep 2024 15:46:49 +0200 Subject: [PATCH] Extract method to reduce cognitive complexity of createHeaders() --- .../org/apache/commons/csv/CSVParser.java | 72 +++++++++++-------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index 471a43c12..98b797ca6 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -504,42 +504,54 @@ private Headers createHeaders() throws IOException { headerRecord = formatHeader; } - // build the name to index mappings - if (headerRecord != null) { - // Track an occurrence of a null, empty or blank header. - boolean observedMissing = false; - for (int i = 0; i < headerRecord.length; i++) { - final String header = headerRecord[i]; - final boolean blankHeader = CSVFormat.isBlank(header); - if (blankHeader && !format.getAllowMissingColumnNames()) { - throw new IllegalArgumentException( - "A header name is missing in " + Arrays.toString(headerRecord)); - } + headerNames = getHeaderNames(headerRecord, hdrMap); + } + // Make header names Collection immutable + return new Headers(hdrMap, headerNames == null ? Collections.emptyList() : Collections.unmodifiableList(headerNames)); + } - final boolean containsHeader = blankHeader ? observedMissing : hdrMap.containsKey(header); - final DuplicateHeaderMode headerMode = format.getDuplicateHeaderMode(); - final boolean duplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_ALL; - final boolean emptyDuplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_EMPTY; + /** + * Build the name to index mappings + * + * @param headerRecord the record as an array of values, or {@code null} if the end of the stream has been reached + * @param headerMap the header column positions (0-based) + * @return header names in column order, or {@code null} if {@code headerRecord} is {@code null}. + */ + private List getHeaderNames(final String[] headerRecord, final Map headerMap) { + List headerNames = null; + if (headerRecord != null) { + // Track an occurrence of a null, empty or blank header. + boolean observedMissing = false; + for (int i = 0; i < headerRecord.length; i++) { + final String header = headerRecord[i]; + final boolean blankHeader = CSVFormat.isBlank(header); + if (blankHeader && !format.getAllowMissingColumnNames()) { + throw new IllegalArgumentException( + "A header name is missing in " + Arrays.toString(headerRecord)); + } - if (containsHeader && !duplicatesAllowed && !(blankHeader && emptyDuplicatesAllowed)) { - throw new IllegalArgumentException( - String.format( - "The header contains a duplicate name: \"%s\" in %s. If this is valid then use CSVFormat.Builder.setDuplicateHeaderMode().", - header, Arrays.toString(headerRecord))); - } - observedMissing |= blankHeader; - if (header != null) { - hdrMap.put(header, Integer.valueOf(i)); // N.B. Explicit (un)boxing is intentional - if (headerNames == null) { - headerNames = new ArrayList<>(headerRecord.length); - } - headerNames.add(header); + final boolean containsHeader = blankHeader ? observedMissing : headerMap.containsKey(header); + final DuplicateHeaderMode headerMode = format.getDuplicateHeaderMode(); + final boolean duplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_ALL; + final boolean emptyDuplicatesAllowed = headerMode == DuplicateHeaderMode.ALLOW_EMPTY; + + if (containsHeader && !duplicatesAllowed && !(blankHeader && emptyDuplicatesAllowed)) { + throw new IllegalArgumentException( + String.format( + "The header contains a duplicate name: \"%s\" in %s. If this is valid then use CSVFormat.Builder.setDuplicateHeaderMode().", + header, Arrays.toString(headerRecord))); + } + observedMissing |= blankHeader; + if (header != null) { + headerMap.put(header, Integer.valueOf(i)); // N.B. Explicit (un)boxing is intentional + if (headerNames == null) { + headerNames = new ArrayList<>(headerRecord.length); } + headerNames.add(header); } } } - // Make header names Collection immutable - return new Headers(hdrMap, headerNames == null ? Collections.emptyList() : Collections.unmodifiableList(headerNames)); + return headerNames; } /**