From 16827955cc02d2d2adf6e12689af2c865c5ce895 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Fri, 18 Dec 2020 07:53:06 +0100 Subject: [PATCH] Minor Improvement: * Add final * Unnecessary semicolon '' * Use StandardCharsets * Fix javadoc --- src/main/java/org/apache/commons/csv/CSVFormat.java | 12 ++++++------ .../java/org/apache/commons/csv/CSVBenchmark.java | 3 ++- .../org/apache/commons/csv/CSVFileParserTest.java | 3 ++- .../java/org/apache/commons/csv/CSVParserTest.java | 2 +- .../java/org/apache/commons/csv/CSVPrinterTest.java | 2 +- .../java/org/apache/commons/csv/PerformanceTest.java | 6 +++--- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index cd621bb925..9cfd6b8cf5 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -1402,7 +1402,6 @@ private void printWithQuotes(final Object object, final CharSequence value, fina int start = 0; int pos = 0; final int len = value.length(); - final int end = len; final char delimChar = getDelimiter(); final char quoteChar = getQuoteCharacter().charValue(); @@ -1445,7 +1444,7 @@ private void printWithQuotes(final Object object, final CharSequence value, fina // by including the default comment char too. quote = true; } else { - while (pos < end) { + while (pos < len) { c = value.charAt(pos); if (c == LF || c == CR || c == quoteChar || c == delimChar || c == escapeChar) { quote = true; @@ -1455,7 +1454,7 @@ private void printWithQuotes(final Object object, final CharSequence value, fina } if (!quote) { - pos = end - 1; + pos = len - 1; c = value.charAt(pos); // Some other chars at the end caused the parser to fail, so for now // encapsulate if we end in anything less than ' ' @@ -1468,7 +1467,7 @@ private void printWithQuotes(final Object object, final CharSequence value, fina if (!quote) { // no encapsulation needed - write out the original value - out.append(value, start, end); + out.append(value, start, len); return; } break; @@ -1478,7 +1477,7 @@ private void printWithQuotes(final Object object, final CharSequence value, fina if (!quote) { // no encapsulation needed - write out the original value - out.append(value, start, end); + out.append(value, start, len); return; } @@ -1487,7 +1486,7 @@ private void printWithQuotes(final Object object, final CharSequence value, fina // Pick up where we left off: pos should be positioned on the first character that caused // the need for encapsulation. - while (pos < end) { + while (pos < len) { final char c = value.charAt(pos); if (c == quoteChar || c == escapeChar) { // write out the chunk up until this point @@ -1507,6 +1506,7 @@ private void printWithQuotes(final Object object, final CharSequence value, fina * Always use quotes unless QuoteMode is NONE, so we not have to look ahead. * * @throws IOException + * If an I/O error occurs */ private void printWithQuotes(final Reader reader, final Appendable out) throws IOException { diff --git a/src/test/java/org/apache/commons/csv/CSVBenchmark.java b/src/test/java/org/apache/commons/csv/CSVBenchmark.java index 6d951baf45..e061dd2d5a 100644 --- a/src/test/java/org/apache/commons/csv/CSVBenchmark.java +++ b/src/test/java/org/apache/commons/csv/CSVBenchmark.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.zip.GZIPInputStream; @@ -63,7 +64,7 @@ public class CSVBenchmark { public void init() throws IOException { final File file = new File("src/test/resources/perf/worldcitiespop.txt.gz"); final InputStream in = new GZIPInputStream(new FileInputStream(file)); - this.data = IOUtils.toString(in, "ISO-8859-1"); + this.data = IOUtils.toString(in, StandardCharsets.ISO_8859_1); in.close(); } diff --git a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java index d71007bb40..61a6ebdb97 100644 --- a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.net.URL; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.stream.Stream; @@ -135,7 +136,7 @@ public void testCSVUrl(final File testFile) throws Exception { // Now parse the file and compare against the expected results final URL resource = ClassLoader.getSystemResource("org/apache/commons/csv/CSVFileParser/" + split[0]); - try (final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format)) { + try (final CSVParser parser = CSVParser.parse(resource, StandardCharsets.UTF_8, format)) { for (final CSVRecord record : parser) { String parsed = Arrays.toString(record.values()); final String comment = record.getComment(); diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 0df4f0e192..14e62a3b82 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -572,7 +572,7 @@ public void testGetOneLine() throws IOException { /** * Tests reusing a parser to process new string records one at a time as they are being discovered. See [CSV-110]. * - * @throws IOException + * @throws IOException when an I/O error occurs. */ @Test public void testGetOneLineOneParser() throws IOException { diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 66033b454d..39e83b0195 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -635,7 +635,7 @@ public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLExc public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); Class.forName("org.h2.Driver"); - try (final Connection connection = geH2Connection();) { + try (final Connection connection = geH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT from TEST"); diff --git a/src/test/java/org/apache/commons/csv/PerformanceTest.java b/src/test/java/org/apache/commons/csv/PerformanceTest.java index 023f2f5168..10381a6ac8 100644 --- a/src/test/java/org/apache/commons/csv/PerformanceTest.java +++ b/src/test/java/org/apache/commons/csv/PerformanceTest.java @@ -64,7 +64,7 @@ public class PerformanceTest { private static int max = 11; // skip first test private static int num = 0; // number of elapsed times recorded - private static long[] elapsedTimes = new long[max]; + private static final long[] ELAPSED_TIMES = new long[max]; private static final CSVFormat format = CSVFormat.EXCEL; @@ -149,7 +149,7 @@ private static class Stats { private static void show(final String msg, final Stats s, final long start) { final long elapsed = System.currentTimeMillis() - start; System.out.printf("%-20s: %5dms %d lines %d fields%n", msg, elapsed, s.count, s.fields); - elapsedTimes[num] = elapsed; + ELAPSED_TIMES[num] = elapsed; num++; } @@ -158,7 +158,7 @@ private static void show(){ if (num > 1) { long tot = 0; for (int i = 1; i < num; i++) { // skip first test - tot += elapsedTimes[i]; + tot += ELAPSED_TIMES[i]; } System.out.printf("%-20s: %5dms%n%n", "Average(not first)", tot / (num - 1)); }