From 64bae1731766dfba45eec76b26e4af837b24effa Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Sat, 12 Dec 2020 20:04:59 +0100 Subject: [PATCH] CSV-267 - Minor improvement (#126) --- src/main/java/org/apache/commons/csv/CSVFormat.java | 5 ++--- src/test/java/org/apache/commons/csv/CSVPrinterTest.java | 4 ++-- src/test/java/org/apache/commons/csv/CSVRecordTest.java | 3 +-- src/test/java/org/apache/commons/csv/PerformanceTest.java | 2 +- src/test/java/org/apache/commons/csv/Utils.java | 5 +++-- .../java/org/apache/commons/csv/issues/JiraCsv198Test.java | 3 ++- .../java/org/apache/commons/csv/issues/JiraCsv248Test.java | 2 +- .../java/org/apache/commons/csv/issues/JiraCsv249Test.java | 2 +- src/test/resources/org/apache/commons/csv/CSVPrinter.java | 5 +++-- 9 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index de0fd031..652b4b9e 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -1051,7 +1051,7 @@ public final class CSVFormat implements Serializable { /** * Returns whether to trim leading and trailing blanks. * This is used by {@link #print(Object, Appendable, boolean)} - * Also by {@link CSVParser#addRecordValue(boolean)} + * Also by {CSVParser#addRecordValue(boolean)} * * @return whether to trim leading and trailing blanks. */ @@ -1322,8 +1322,7 @@ public final class CSVFormat implements Serializable { private void printWithEscapes(final CharSequence value, final Appendable out) throws IOException { int start = 0; int pos = 0; - final int len = value.length(); - final int end = len; + final int end = value.length(); final char delim = getDelimiter(); final char escape = getEscapeCharacter().charValue(); diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 8c8f19a3..66033b45 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -179,7 +179,7 @@ public class CSVPrinterTest { final char[] buf = new char[sz]; for (int i = 0; i < sz; i++) { // stick in special chars with greater frequency - char ch; + final char ch; final int what = r.nextInt(20); switch (what) { case 0: @@ -1342,7 +1342,7 @@ public class CSVPrinterTest { final String rowData = StringUtils.join(values, ','); final CharArrayWriter charArrayWriter = new CharArrayWriter(0); try (final CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(rowData)); - CSVPrinter csvPrinter = CSVFormat.INFORMIX_UNLOAD.print(charArrayWriter)) { + final CSVPrinter csvPrinter = CSVFormat.INFORMIX_UNLOAD.print(charArrayWriter)) { for (final CSVRecord record : parser) { csvPrinter.printRecord(record); } diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java index 11401a07..1f93c247 100644 --- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java +++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java @@ -31,7 +31,6 @@ import java.io.ObjectOutputStream; import java.io.StringReader; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; @@ -213,7 +212,7 @@ public class CSVRecordTest { @Test public void testSerialization() throws IOException, ClassNotFoundException { - CSVRecord shortRec; + final CSVRecord shortRec; try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) { shortRec = parser.iterator().next(); } diff --git a/src/test/java/org/apache/commons/csv/PerformanceTest.java b/src/test/java/org/apache/commons/csv/PerformanceTest.java index 9d2106a2..023f2f51 100644 --- a/src/test/java/org/apache/commons/csv/PerformanceTest.java +++ b/src/test/java/org/apache/commons/csv/PerformanceTest.java @@ -88,7 +88,7 @@ public class PerformanceTest { max = Integer.parseInt(args[0]); } - String tests[]; + final String[] tests; if (argc > 1) { tests = new String[argc - 1]; for (int i = 1; i < argc; i++) { diff --git a/src/test/java/org/apache/commons/csv/Utils.java b/src/test/java/org/apache/commons/csv/Utils.java index 2b4d310a..289083e7 100644 --- a/src/test/java/org/apache/commons/csv/Utils.java +++ b/src/test/java/org/apache/commons/csv/Utils.java @@ -39,8 +39,9 @@ final class Utils { * @param actual the List of {@link CSVRecord} entries, each containing an array of values */ public static void compare(final String message, final String[][] expected, final List actual) { - assertEquals(expected.length, actual.size(), message + " - outer array size"); - for (int i = 0; i < expected.length; i++) { + final int expectedLength = expected.length; + assertEquals(expectedLength, actual.size(), message + " - outer array size"); + for (int i = 0; i < expectedLength; i++) { assertArrayEquals(expected[i], actual.get(i).values(), message + " (entry " + i + ")"); } } diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv198Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv198Test.java index 307610a2..8e73447b 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv198Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv198Test.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; @@ -37,7 +38,7 @@ public class JiraCsv198Test { final InputStream pointsOfReference = getClass().getResourceAsStream("/org/apache/commons/csv/CSV-198/optd_por_public.csv"); assertNotNull(pointsOfReference); try (@SuppressWarnings("resource") - CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, "UTF-8"))) { + CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, StandardCharsets.UTF_8))) { for (final CSVRecord record : parser) { final String locationType = record.get("location_type"); assertNotNull(locationType); diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java index 1005e835..7b53b758 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java @@ -46,7 +46,7 @@ public class JiraCsv248Test { // CSVRecord rec = parser.iterator().next(); //} try (InputStream in = getTestInput(); - ObjectInputStream ois = new ObjectInputStream(in)) { + final ObjectInputStream ois = new ObjectInputStream(in)) { final Object object = ois.readObject(); assertTrue(object instanceof CSVRecord); final CSVRecord rec = (CSVRecord) object; diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv249Test.java b/src/test/java/org/apache/commons/csv/issues/JiraCsv249Test.java index 55d23aaa..5ba838ca 100644 --- a/src/test/java/org/apache/commons/csv/issues/JiraCsv249Test.java +++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv249Test.java @@ -40,7 +40,7 @@ public class JiraCsv249Test { printer.printRecord("foo \\", "bar"); } final StringReader stringReader = new StringReader(stringWriter.toString()); - List records; + final List records; try (CSVParser parser = new CSVParser(stringReader, csvFormat)) { records = parser.getRecords(); } diff --git a/src/test/resources/org/apache/commons/csv/CSVPrinter.java b/src/test/resources/org/apache/commons/csv/CSVPrinter.java index a0cc6126..c7377101 100644 --- a/src/test/resources/org/apache/commons/csv/CSVPrinter.java +++ b/src/test/resources/org/apache/commons/csv/CSVPrinter.java @@ -203,11 +203,12 @@ public final class CSVPrinter implements Flushable, Closeable { } out.append(format.getCommentMarker().charValue()); out.append(SP); - for (int i = 0; i < comment.length(); i++) { + final int commentLength = comment.length(); + for (int i = 0; i < commentLength; i++) { final char c = comment.charAt(i); switch (c) { case CR: - if (i + 1 < comment.length() && comment.charAt(i + 1) == LF) { + if (i + 1 < commentLength && comment.charAt(i + 1) == LF) { i++; } //$FALL-THROUGH$ break intentionally excluded.