This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git

commit e991e6d5c2a523fffe9fb103265e044396cee31a
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri Nov 1 10:51:29 2024 -0400

    Add and use CSVParser.Builder and builder()
---
 src/changes/changes.xml                            |   1 +
 .../java/org/apache/commons/csv/CSVFormat.java     |   9 ++
 .../java/org/apache/commons/csv/CSVParser.java     |  73 +++++++++++++-
 .../org/apache/commons/csv/CSVFileParserTest.java  |   8 +-
 .../java/org/apache/commons/csv/CSVParserTest.java | 111 +++++++++++++++++----
 .../org/apache/commons/csv/PerformanceTest.java    |   2 +-
 .../apache/commons/csv/issues/JiraCsv149Test.java  |   4 +-
 .../apache/commons/csv/issues/JiraCsv150Test.java  |   4 +-
 .../apache/commons/csv/issues/JiraCsv206Test.java  |   8 +-
 .../apache/commons/csv/issues/JiraCsv249Test.java  |   8 +-
 .../apache/commons/csv/issues/JiraCsv290Test.java  |   6 +-
 .../apache/commons/csv/issues/JiraCsv294Test.java  |   8 +-
 12 files changed, 195 insertions(+), 47 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 28be74c6..09b60e51 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,6 +44,7 @@
       <!-- FIX -->
       <!-- ADD -->
       <action type="add" issue="CSV-313" dev="ggregory" due-to="Gary 
Gregory">Add CSVPrinter.getRecordCount().</action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">Add and use 
CSVParser.Builder and builder().</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory, 
Dependabot">Bump org.apache.commons:commons-parent from 76 to 78 #486, 
#495.</action>
       <action type="update" dev="ggregory" due-to="Gary Gregory, 
Dependabot">Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 
#493.</action> 
diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java 
b/src/main/java/org/apache/commons/csv/CSVFormat.java
index dd5416e1..7376f902 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -1370,6 +1370,15 @@ public final class CSVFormat implements Serializable {
         return contains(source, Constants.CR) || contains(source, 
Constants.LF);
     }
 
+    /**
+     * Creates a null-safe copy of the given instance.
+     *
+     * @return a copy of the given instance or null if the input is null.
+     */
+    static CSVFormat copy(final CSVFormat format) {
+        return format != null ? format.copy() : null;
+    }
+
     static boolean isBlank(final String value) {
         return value == null || value.trim().isEmpty();
     }
diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java 
b/src/main/java/org/apache/commons/csv/CSVParser.java
index a2bc2307..14e2a778 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -47,6 +47,7 @@ import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 
+import org.apache.commons.io.build.AbstractStreamBuilder;
 import org.apache.commons.io.function.Uncheck;
 
 /**
@@ -142,6 +143,65 @@ import org.apache.commons.io.function.Uncheck;
  */
 public final class CSVParser implements Iterable<CSVRecord>, Closeable {
 
+    /**
+     * Builds a new {@link CSVParser}.
+     *
+     * @since 1.13.0
+     */
+    public static class Builder extends AbstractStreamBuilder<CSVParser, 
Builder> {
+
+        private CSVFormat format;
+        private long characterOffset;
+        private long recordNumber;
+
+        /**
+         * Constructs a new instance.
+         */
+        protected Builder() {
+            // empty
+        }
+
+        @SuppressWarnings("resource")
+        @Override
+        public CSVParser get() throws IOException {
+            return new CSVParser(getReader(), format != null ? format : 
CSVFormat.DEFAULT, characterOffset, recordNumber);
+        }
+
+        /**
+         * Sets the lexer offset when the parser does not start parsing at the 
beginning of the source.
+         *
+         * @param characterOffset the lexer offset.
+         * @return this instance.
+         */
+        public Builder setCharacterOffset(final long characterOffset) {
+            this.characterOffset = characterOffset;
+            return asThis();
+        }
+
+        /**
+         * Sets the CSV format. A copy of the given format is kept.
+         *
+         * @param format the CSV format, null is equivalent to {@link 
CSVFormat#DEFAULT}.
+         * @return this instance.
+         */
+        public Builder setFormat(final CSVFormat format) {
+            this.format = CSVFormat.copy(format);
+            return asThis();
+        }
+
+        /**
+         * Sets the next record number to assign.
+         *
+         * @param recordNumber the next record number to assign.
+         * @return this instance.
+         */
+        public Builder setRecordNumber(final long recordNumber) {
+            this.recordNumber = recordNumber;
+            return asThis();
+        }
+
+    }
+
     final class CSVRecordIterator implements Iterator<CSVRecord> {
         private CSVRecord current;
 
@@ -190,7 +250,6 @@ public final class CSVParser implements 
Iterable<CSVRecord>, Closeable {
             throw new UnsupportedOperationException();
         }
     }
-
     /**
      * Header information based on name and position.
      */
@@ -212,6 +271,16 @@ public final class CSVParser implements 
Iterable<CSVRecord>, Closeable {
         }
     }
 
+    /**
+     * Creates a new builder.
+     *
+     * @return a new builder.
+     * @since 1.13.0
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
     /**
      * Creates a parser for the given {@link File}.
      *
@@ -427,7 +496,7 @@ public final class CSVParser implements 
Iterable<CSVRecord>, Closeable {
      * @param characterOffset
      *            Lexer offset when the parser does not start parsing at the 
beginning of the source.
      * @param recordNumber
-     *            The next record number to assign
+     *            The next record number to assign.
      * @throws IllegalArgumentException
      *             If the parameters of the format are inconsistent or if 
either the reader or format is null.
      * @throws IOException
diff --git a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java 
b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
index 4d9b8711..728686eb 100644
--- a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
@@ -58,8 +58,8 @@ public class CSVFileParserTest {
     @ParameterizedTest
     @MethodSource("generateData")
     public void testCSVFile(final File testFile) throws Exception {
-        try (FileReader fr = new FileReader(testFile); BufferedReader testData 
= new BufferedReader(fr)) {
-            String line = readTestData(testData);
+        try (FileReader fr = new FileReader(testFile); BufferedReader 
testDataReader = new BufferedReader(fr)) {
+            String line = readTestData(testDataReader);
             assertNotNull("file must contain config line", line);
             final String[] split = line.split(" ");
             assertTrue(split.length >= 1, testFile.getName() + " require 1 
param");
@@ -81,7 +81,7 @@ public class CSVFileParserTest {
                     fail(testFile.getName() + " unexpected option: " + option);
                 }
             }
-            line = readTestData(testData); // get string version of format
+            line = readTestData(testDataReader); // get string version of 
format
             assertEquals(line, format.toString(), testFile.getName() + " 
Expected format ");
 
             // Now parse the file and compare against the expected results
@@ -94,7 +94,7 @@ public class CSVFileParserTest {
                         parsed += "#" + comment.replace("\n", "\\n");
                     }
                     final int count = record.size();
-                    assertEquals(readTestData(testData), count + ":" + parsed, 
testFile.getName());
+                    assertEquals(readTestData(testDataReader), count + ":" + 
parsed, testFile.getName());
                 }
             }
         }
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java 
b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index 6a063730..aed3a205 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -69,13 +69,15 @@ import org.junit.jupiter.params.provider.EnumSource;
  */
 public class CSVParserTest {
 
+    private static final CSVFormat EXCEL_WITH_HEADER = 
CSVFormat.EXCEL.withHeader();
+
     private static final Charset UTF_8 = StandardCharsets.UTF_8;
 
     private static final String UTF_8_NAME = UTF_8.name();
 
     private static final String CSV_INPUT = "a,b,c,d\n" + " a , b , 1 2 \n" + 
"\"foo baar\", b,\n" +
-    // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
-        "   \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV 
escaping
+            // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
+            "   \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard 
CSV escaping
 
     private static final String CSV_INPUT_1 = "a,b,c,d";
 
@@ -220,7 +222,7 @@ public class CSVParserTest {
     @Disabled("CSV-107")
     public void testBOM() throws IOException {
         final URL url = 
ClassLoader.getSystemClassLoader().getResource("org/apache/commons/csv/CSVFileParser/bom.csv");
-        try (final CSVParser parser = CSVParser.parse(url, 
StandardCharsets.UTF_8, CSVFormat.EXCEL.withHeader())) {
+        try (final CSVParser parser = CSVParser.parse(url, 
StandardCharsets.UTF_8, EXCEL_WITH_HEADER)) {
             parser.forEach(record -> assertNotNull(record.get("Date")));
         }
     }
@@ -228,7 +230,7 @@ public class CSVParserTest {
     @Test
     public void testBOMInputStreamParserWithInputStream() throws IOException {
         try (final BOMInputStream inputStream = 
createBOMInputStream("org/apache/commons/csv/CSVFileParser/bom.csv");
-                final CSVParser parser = CSVParser.parse(inputStream, UTF_8, 
CSVFormat.EXCEL.withHeader())) {
+                final CSVParser parser = CSVParser.parse(inputStream, UTF_8, 
EXCEL_WITH_HEADER)) {
             parser.forEach(record -> assertNotNull(record.get("Date")));
         }
     }
@@ -236,7 +238,10 @@ public class CSVParserTest {
     @Test
     public void testBOMInputStreamParserWithReader() throws IOException {
         try (final Reader reader = new 
InputStreamReader(createBOMInputStream("org/apache/commons/csv/CSVFileParser/bom.csv"),
 UTF_8_NAME);
-                final CSVParser parser = new CSVParser(reader, 
CSVFormat.EXCEL.withHeader())) {
+                final CSVParser parser = CSVParser.builder()
+                        .setReader(reader)
+                        .setFormat(EXCEL_WITH_HEADER)
+                        .get()) {
             parser.forEach(record -> assertNotNull(record.get("Date")));
         }
     }
@@ -244,15 +249,18 @@ public class CSVParserTest {
     @Test
     public void testBOMInputStreamParseWithReader() throws IOException {
         try (final Reader reader = new 
InputStreamReader(createBOMInputStream("org/apache/commons/csv/CSVFileParser/bom.csv"),
 UTF_8_NAME);
-                final CSVParser parser = CSVParser.parse(reader, 
CSVFormat.EXCEL.withHeader())) {
+                final CSVParser parser = CSVParser.builder()
+                        .setReader(reader)
+                        .setFormat(EXCEL_WITH_HEADER)
+                        .get()) {
             parser.forEach(record -> assertNotNull(record.get("Date")));
         }
     }
 
     @Test
     public void testCarriageReturnEndings() throws IOException {
-        final String code = "foo\rbaar,\rhello,world\r,kanu";
-        try (final CSVParser parser = CSVParser.parse(code, 
CSVFormat.DEFAULT)) {
+        final String string = "foo\rbaar,\rhello,world\r,kanu";
+        try (final CSVParser parser = 
CSVParser.builder().setCharSequence(string).get()) {
             final List<CSVRecord> records = parser.getRecords();
             assertEquals(4, records.size());
         }
@@ -260,8 +268,8 @@ public class CSVParserTest {
 
     @Test
     public void testCarriageReturnLineFeedEndings() throws IOException {
-        final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
-        try (final CSVParser parser = CSVParser.parse(code, 
CSVFormat.DEFAULT)) {
+        final String string = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
+        try (final CSVParser parser = 
CSVParser.builder().setCharSequence(string).get()) {
             final List<CSVRecord> records = parser.getRecords();
             assertEquals(4, records.size());
         }
@@ -569,7 +577,7 @@ public class CSVParserTest {
     @Test
     public void testExcelHeaderCountLessThanData() throws Exception {
         final String code = "A,B,C,,\r\na,b,c,d,e\r\n";
-        try (final CSVParser parser = CSVParser.parse(code, 
CSVFormat.EXCEL.withHeader())) {
+        try (final CSVParser parser = CSVParser.parse(code, 
EXCEL_WITH_HEADER)) {
             parser.getRecords().forEach(record -> {
                 assertEquals("a", record.get("A"));
                 assertEquals("b", record.get("B"));
@@ -783,7 +791,10 @@ public class CSVParserTest {
     public void testGetOneLineOneParser() throws IOException {
         final CSVFormat format = CSVFormat.DEFAULT;
         try (final PipedWriter writer = new PipedWriter();
-                final CSVParser parser = new CSVParser(new 
PipedReader(writer), format)) {
+                final CSVParser parser = CSVParser.builder()
+                        .setReader(new PipedReader(writer))
+                        .setFormat(format)
+                        .get()) {
             writer.append(CSV_INPUT_1);
             writer.append(format.getRecordSeparator());
             final CSVRecord record1 = parser.nextRecord();
@@ -1232,35 +1243,68 @@ public class CSVParserTest {
     public void testParse() throws Exception {
         final ClassLoader loader = ClassLoader.getSystemClassLoader();
         final URL url = 
loader.getResource("org/apache/commons/csv/CSVFileParser/test.csv");
-        final CSVFormat format = CSVFormat.DEFAULT.withHeader("A", "B", "C", 
"D");
+        final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader("A", 
"B", "C", "D").build();
         final Charset charset = StandardCharsets.UTF_8;
-
-        try (@SuppressWarnings("resource") // CSVParser closes the input 
resource
-        final CSVParser parser = CSVParser.parse(new 
InputStreamReader(url.openStream(), charset), format)) {
+        // Reader
+        try (final CSVParser parser = CSVParser.parse(new 
InputStreamReader(url.openStream(), charset), format)) {
             parseFully(parser);
         }
-        try (final CSVParser parser = CSVParser.parse(new 
String(Files.readAllBytes(Paths.get(url.toURI())), charset), format)) {
+        try (final CSVParser parser = CSVParser.builder().setReader(new 
InputStreamReader(url.openStream(), charset)).setFormat(format).get()) {
             parseFully(parser);
         }
-        try (final CSVParser parser = CSVParser.parse(new File(url.toURI()), 
charset, format)) {
+        // String
+        final Path path = Paths.get(url.toURI());
+        final String string = new String(Files.readAllBytes(path), charset);
+        try (final CSVParser parser = CSVParser.parse(string, format)) {
             parseFully(parser);
         }
-        try (@SuppressWarnings("resource") // CSVParser closes the input 
resource
-        final CSVParser parser = CSVParser.parse(url.openStream(), charset, 
format)) {
+        try (final CSVParser parser = 
CSVParser.builder().setCharSequence(string).setFormat(format).get()) {
             parseFully(parser);
         }
-        try (final CSVParser parser = CSVParser.parse(Paths.get(url.toURI()), 
charset, format)) {
+        // File
+        final File file = new File(url.toURI());
+        try (final CSVParser parser = CSVParser.parse(file, charset, format)) {
             parseFully(parser);
         }
+        try (final CSVParser parser = 
CSVParser.builder().setFile(file).setCharset(charset).setFormat(format).get()) {
+            parseFully(parser);
+        }
+        // InputStream
+        try (final CSVParser parser = CSVParser.parse(url.openStream(), 
charset, format)) {
+            parseFully(parser);
+        }
+        try (final CSVParser parser = 
CSVParser.builder().setInputStream(url.openStream()).setCharset(charset).setFormat(format).get())
 {
+            parseFully(parser);
+        }
+        // Path
+        try (final CSVParser parser = CSVParser.parse(path, charset, format)) {
+            parseFully(parser);
+        }
+        try (final CSVParser parser = 
CSVParser.builder().setPath(path).setCharset(charset).setFormat(format).get()) {
+            parseFully(parser);
+        }
+        // URL
         try (final CSVParser parser = CSVParser.parse(url, charset, format)) {
             parseFully(parser);
         }
+        try (final CSVParser parser = 
CSVParser.builder().setURI(url.toURI()).setCharset(charset).setFormat(format).get())
 {
+            parseFully(parser);
+        }
+        // InputStreamReader
         try (final CSVParser parser = new CSVParser(new 
InputStreamReader(url.openStream(), charset), format)) {
             parseFully(parser);
         }
+        try (final CSVParser parser = CSVParser.builder().setReader(new 
InputStreamReader(url.openStream(), charset)).setFormat(format).get()) {
+            parseFully(parser);
+        }
+        // InputStreamReader with longs
         try (final CSVParser parser = new CSVParser(new 
InputStreamReader(url.openStream(), charset), format, /* characterOffset= */0, 
/* recordNumber= */1)) {
             parseFully(parser);
         }
+        try (final CSVParser parser = CSVParser.builder().setReader(new 
InputStreamReader(url.openStream(), 
charset)).setFormat(format).setCharacterOffset(0)
+                .setRecordNumber(0).get()) {
+            parseFully(parser);
+        }
     }
 
     @Test
@@ -1380,7 +1424,10 @@ public class CSVParserTest {
         try (CSVPrinter printer = new CSVPrinter(buf, format.getFormat())) {
             printer.printRecords(Stream.of(lines));
         }
-        try (CSVParser csvRecords = new CSVParser(new 
StringReader(buf.toString()), format.getFormat())) {
+        try (CSVParser csvRecords = CSVParser.builder()
+                .setReader(new StringReader(buf.toString()))
+                .setFormat(format.getFormat())
+                .get()) {
             for (final String[] line : lines) {
                 assertArrayEquals(line, csvRecords.nextRecord().values());
             }
@@ -1654,6 +1701,26 @@ public class CSVParserTest {
             assertEquals(code.indexOf("EOF"), record.getCharacterPosition());
         }
         // now try to read starting at record 3
+        try (CSVParser parser = CSVParser.builder()
+                .setReader(new StringReader(code.substring((int) 
positionRecord3)))
+                .setFormat(format)
+                .setCharacterOffset(positionRecord3)
+                .setRecordNumber(3)
+                .get()) {
+            CSVRecord record;
+            // nextRecord
+            assertNotNull(record = parser.nextRecord());
+            assertEquals(3, record.getRecordNumber());
+            assertEquals(code.indexOf("'A"), record.getCharacterPosition());
+            assertEquals("A" + lineSeparator + "A", record.get(0));
+            assertEquals("B" + lineSeparator + "B", record.get(1));
+            assertEquals("CC", record.get(2));
+            // nextRecord
+            assertNotNull(record = parser.nextRecord());
+            assertEquals(4, record.getRecordNumber());
+            assertEquals(code.indexOf('\u00c4'), 
record.getCharacterPosition());
+            assertEquals("\u00c4", record.get(0));
+        } // again with ctor
         try (CSVParser parser = new CSVParser(new 
StringReader(code.substring((int) positionRecord3)), format, positionRecord3, 
3)) {
             CSVRecord record;
             // nextRecord
diff --git a/src/test/java/org/apache/commons/csv/PerformanceTest.java 
b/src/test/java/org/apache/commons/csv/PerformanceTest.java
index 100ac84e..8523a9a6 100644
--- a/src/test/java/org/apache/commons/csv/PerformanceTest.java
+++ b/src/test/java/org/apache/commons/csv/PerformanceTest.java
@@ -299,7 +299,7 @@ public class PerformanceTest {
     }
 
     private static void testParseCommonsCSV() throws Exception {
-        testParser("CSV", () -> new CSVParser(createReader(), format));
+        testParser("CSV", () -> 
CSVParser.builder().setReader(createReader()).setFormat(format).get());
     }
 
     private static void testParsePath() throws Exception {
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java 
b/src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java
index 70f8c5ea..0a08958e 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv149Test.java
@@ -41,7 +41,7 @@ public class JiraCsv149Test {
         if (eolAtEof) {
             source += CR_LF;
         }
-        final StringReader records = new StringReader(source);
+        final StringReader reader = new StringReader(source);
         // @formatter:off
         final CSVFormat format = CSVFormat.RFC4180.builder()
             .setHeader()
@@ -50,7 +50,7 @@ public class JiraCsv149Test {
             .build();
         // @formatter:on
         int lineCounter = 2;
-        try (final CSVParser parser = new CSVParser(records, format)) {
+        try (final CSVParser parser = 
CSVParser.builder().setReader(reader).setFormat(format).get()) {
             for (final CSVRecord record : parser) {
                 assertNotNull(record);
                 assertEquals(lineCounter++, parser.getCurrentLineNumber());
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv150Test.java 
b/src/test/java/org/apache/commons/csv/issues/JiraCsv150Test.java
index 1ede9f23..7d8ba89f 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv150Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv150Test.java
@@ -27,8 +27,8 @@ import org.junit.jupiter.api.Test;
 
 public class JiraCsv150Test {
 
-    private void testDisable(final CSVFormat csvFormat, final StringReader 
stringReader) throws IOException {
-        try (CSVParser csvParser = new CSVParser(stringReader, csvFormat)) {
+    private void testDisable(final CSVFormat format, final StringReader 
reader) throws IOException {
+        try (CSVParser csvParser = 
CSVParser.builder().setReader(reader).setFormat(format).get()) {
             assertEquals(1, csvParser.getRecords().size());
         }
     }
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv206Test.java 
b/src/test/java/org/apache/commons/csv/issues/JiraCsv206Test.java
index 8693c36f..26645a16 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv206Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv206Test.java
@@ -35,9 +35,9 @@ public class JiraCsv206Test {
         // Read with multiple character delimiter
         final String source = 
"FirstName[|]LastName[|]Address\r\nJohn[|]Smith[|]123 Main St.";
         final StringReader reader = new StringReader(source);
-        final CSVFormat csvFormat = 
CSVFormat.DEFAULT.builder().setDelimiter("[|]").build();
+        final CSVFormat format = 
CSVFormat.DEFAULT.builder().setDelimiter("[|]").build();
         CSVRecord record = null;
-        try (final CSVParser csvParser = new CSVParser(reader, csvFormat)) {
+        try (final CSVParser csvParser = 
CSVParser.builder().setReader(reader).setFormat(format).get()) {
             final Iterator<CSVRecord> iterator = csvParser.iterator();
             record = iterator.next();
             assertEquals("FirstName", record.get(0));
@@ -57,13 +57,13 @@ public class JiraCsv206Test {
         // @formatter:on
         final String comment = "Change delimiter to [I]";
         // @formatter:off
-        final CSVFormat format = CSVFormat.EXCEL.builder()
+        final CSVFormat formatExcel = CSVFormat.EXCEL.builder()
                 .setDelimiter("[I]").setHeader("first name", "last name", 
"address")
                 .setCommentMarker('#')
                 .setHeaderComments(comment).build();
         // @formatter:on
         final StringBuilder out = new StringBuilder();
-        try (final CSVPrinter printer = format.print(out)) {
+        try (final CSVPrinter printer = formatExcel.print(out)) {
             printer.print(record.get(0));
             printer.print(record.get(1));
             printer.print(record.get(2));
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 7989a464..58caced4 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv249Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv249Test.java
@@ -34,14 +34,14 @@ public class JiraCsv249Test {
 
     @Test
     public void testJiraCsv249() throws IOException {
-        final CSVFormat csvFormat = 
CSVFormat.DEFAULT.builder().setEscape('\\').build();
+        final CSVFormat format = 
CSVFormat.DEFAULT.builder().setEscape('\\').build();
         final StringWriter stringWriter = new StringWriter();
-        try (CSVPrinter printer = new CSVPrinter(stringWriter, csvFormat)) {
+        try (CSVPrinter printer = new CSVPrinter(stringWriter, format)) {
             printer.printRecord("foo \\", "bar");
         }
-        final StringReader stringReader = new 
StringReader(stringWriter.toString());
+        final StringReader reader = new StringReader(stringWriter.toString());
         final List<CSVRecord> records;
-        try (CSVParser parser = new CSVParser(stringReader, csvFormat)) {
+        try (CSVParser parser = 
CSVParser.builder().setReader(reader).setFormat(format).get()) {
             records = parser.getRecords();
         }
         records.forEach(record -> {
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv290Test.java 
b/src/test/java/org/apache/commons/csv/issues/JiraCsv290Test.java
index 9c6badbb..e0ead70b 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv290Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv290Test.java
@@ -94,13 +94,13 @@ public class JiraCsv290Test {
     @Test
     public void testWriteThenRead() throws Exception {
         final StringWriter sw = new StringWriter();
-        try (CSVPrinter printer = new CSVPrinter(sw, 
CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build()))
 {
+        final CSVFormat format = 
CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build();
+        try (CSVPrinter printer = new CSVPrinter(sw, format)) {
             printer.printRecord("column1", "column2");
             printer.printRecord("v11", "v12");
             printer.printRecord("v21", "v22");
             printer.close();
-            try (CSVParser parser = new CSVParser(new 
StringReader(sw.toString()),
-                    
CSVFormat.POSTGRESQL_CSV.builder().setHeader().setSkipHeaderRecord(true).build()))
 {
+            try (CSVParser parser = CSVParser.builder().setReader(new 
StringReader(sw.toString())).setFormat(format).get()) {
                 assertArrayEquals(new Object[] { "column1", "column2" }, 
parser.getHeaderNames().toArray());
                 final Iterator<CSVRecord> i = parser.iterator();
                 assertArrayEquals(new String[] { "v11", "v12" }, 
i.next().toList().toArray());
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv294Test.java 
b/src/test/java/org/apache/commons/csv/issues/JiraCsv294Test.java
index f01948fa..3d13e479 100644
--- a/src/test/java/org/apache/commons/csv/issues/JiraCsv294Test.java
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv294Test.java
@@ -25,6 +25,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
+import java.io.StringReader;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
 
@@ -36,15 +37,16 @@ import org.junit.jupiter.api.Test;
 
 public class JiraCsv294Test {
 
-    private static void testInternal(final CSVFormat csvFormat, final String 
expectedSubstring) throws IOException {
+    private static void testInternal(final CSVFormat format, final String 
expectedSubstring) throws IOException {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        try (CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(bos, 
StandardCharsets.UTF_8), csvFormat)) {
+        try (CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(bos, 
StandardCharsets.UTF_8), format)) {
             printer.printRecord("a", "b \"\"", "c");
         }
         final byte[] written = bos.toByteArray();
         final String writtenString = new String(written, 
StandardCharsets.UTF_8);
         assertTrue(writtenString.contains(expectedSubstring));
-        try (CSVParser parser = new CSVParser(new InputStreamReader(new 
ByteArrayInputStream(written), StandardCharsets.UTF_8), csvFormat)) {
+        try (CSVParser parser = CSVParser.builder().setReader(new 
InputStreamReader(new ByteArrayInputStream(written), StandardCharsets.UTF_8))
+                .setFormat(format).get()) {
             final List<CSVRecord> records = parser.getRecords();
             assertEquals(1, records.size());
             final CSVRecord record = records.get(0);

Reply via email to