You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/03/09 18:24:35 UTC

(commons-csv) branch master updated (7b4e1059 -> cf393fc6)

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

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


    from 7b4e1059 The next release will be 1.14.0
     new 7bfb0cea CSVParser.getRecords() knows how to use CSVFormat's maxRows
     new 9b75f05c Javadoc
     new cf393fc6 CSVParser.iterator() knows how to use CSVFormat's maxRows

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |  3 ++
 .../java/org/apache/commons/csv/CSVFormat.java     | 19 +++++++
 .../java/org/apache/commons/csv/CSVParser.java     | 14 +++--
 .../java/org/apache/commons/csv/CSVPrinter.java    |  5 +-
 .../java/org/apache/commons/csv/CSVRecord.java     |  2 +-
 .../java/org/apache/commons/csv/CSVParserTest.java | 60 ++++++++++++++++++++++
 .../org/apache/commons/csv/CSVPrinterTest.java     | 14 ++---
 7 files changed, 101 insertions(+), 16 deletions(-)


(commons-csv) 03/03: CSVParser.iterator() knows how to use CSVFormat's maxRows

Posted by gg...@apache.org.
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 cf393fc6a2a46953fa02fe7453db4aa6c9c9fae0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Mar 9 14:24:30 2025 -0400

    CSVParser.iterator() knows how to use CSVFormat's maxRows
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/csv/CSVFormat.java     | 12 ++++++++--
 .../java/org/apache/commons/csv/CSVParser.java     | 10 +++++---
 .../java/org/apache/commons/csv/CSVPrinter.java    |  3 +--
 .../java/org/apache/commons/csv/CSVParserTest.java | 28 ++++++++++++++++++++++
 5 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 80a6edd6..a9382c3f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -53,6 +53,7 @@
       <action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(Stream) knows how to use CSVFormat's maxRows.</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">CSVParser.stream() knows how to use CSVFormat's maxRows.</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">CSVParser.getRecords() knows how to use CSVFormat's maxRows.</action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">CSVParser.iterator() knows how to use CSVFormat's maxRows.</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump com.opencsv:opencsv from 5.9 to 5.10.</action>
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.2 to 1.18.0 #522.</action>
diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java
index 5f8c76ea..b98893a9 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -2091,11 +2091,11 @@ public final class CSVFormat implements Serializable {
     }
 
     <T> IOStream<T> limit(final IOStream<T> stream) {
-        return getMaxRows() > 0 ? stream.limit(getMaxRows()) : stream;
+        return useMaxRows() ? stream.limit(getMaxRows()) : stream;
     }
 
     <T> Stream<T> limit(final Stream<T> stream) {
-        return getMaxRows() > 0 ? stream.limit(getMaxRows()) : stream;
+        return useMaxRows() ? stream.limit(getMaxRows()) : stream;
     }
 
     /**
@@ -2582,6 +2582,14 @@ public final class CSVFormat implements Serializable {
         return getTrim() ? value.trim() : value;
     }
 
+    boolean useMaxRows() {
+        return getMaxRows() > 0;
+    }
+
+    boolean useRow(final long rowNum) {
+        return !useMaxRows() || rowNum <= getMaxRows();
+    }
+
     /**
      * Verifies the validity and consistency of the attributes, and throws an {@link IllegalArgumentException} if necessary.
      * <p>
diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java
index 2e4c8133..409e40bf 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -221,14 +221,18 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
         private CSVRecord current;
 
         /**
-         * Gets the next record.
+         * Gets the next record or null at the end of stream or max rows read.
          *
          * @throws IOException  on parse error or input read-failure
          * @throws CSVException on invalid input.
          * @return the next record, or {@code null} if the end of the stream has been reached.
          */
         private CSVRecord getNextRecord() {
-            return Uncheck.get(CSVParser.this::nextRecord);
+            CSVRecord record = null;
+            if (format.useRow(recordNumber + 1)) {
+                record = Uncheck.get(CSVParser.this::nextRecord);
+            }
+            return record;
         }
 
         @Override
@@ -938,7 +942,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
      * @since 1.9.0
      */
     public Stream<CSVRecord> stream() {
-        return format.limit(StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED), false));
+        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED), false);
     }
 
 }
diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java
index ec0b8f1d..24849fb3 100644
--- a/src/main/java/org/apache/commons/csv/CSVPrinter.java
+++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java
@@ -445,8 +445,7 @@ public final class CSVPrinter implements Flushable, Closeable {
      */
     public void printRecords(final ResultSet resultSet) throws SQLException, IOException {
         final int columnCount = resultSet.getMetaData().getColumnCount();
-        final long maxRows = format.getMaxRows();
-        while (resultSet.next() && (maxRows < 1 || resultSet.getRow() <= maxRows)) {
+        while (resultSet.next() && format.useRow(resultSet.getRow())) {
             for (int i = 1; i <= columnCount; i++) {
                 final Object object = resultSet.getObject(i);
                 if (object instanceof Clob) {
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index 50bf052b..e4fdb5d4 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -1164,6 +1164,34 @@ public class CSVParserTest {
         }
     }
 
+    @ParameterizedTest
+    @ValueSource(longs = { -1, 0, 1, 2, 3, 4, 5, Long.MAX_VALUE })
+    public void testIteratorMaxRows(final long maxRows) throws Exception {
+        final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+        try (CSVParser parser = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get().parse(in)) {
+            final Iterator<CSVRecord> iterator = parser.iterator();
+            assertTrue(iterator.hasNext());
+            assertThrows(UnsupportedOperationException.class, iterator::remove);
+            assertArrayEquals(new String[] { "a", "b", "c" }, iterator.next().values());
+            final boolean noLimit = maxRows <= 0;
+            final int fixtureLen = 3;
+            final long expectedLen = noLimit ? fixtureLen : Math.min(fixtureLen, maxRows);
+            if (expectedLen > 1) {
+                assertTrue(iterator.hasNext());
+                assertArrayEquals(new String[] { "1", "2", "3" }, iterator.next().values());
+            }
+            assertEquals(expectedLen > 2, iterator.hasNext());
+            // again
+            assertEquals(expectedLen > 2, iterator.hasNext());
+            if (expectedLen == fixtureLen) {
+                assertTrue(iterator.hasNext());
+                assertArrayEquals(new String[] { "x", "y", "z" }, iterator.next().values());
+            }
+            assertFalse(iterator.hasNext());
+            assertThrows(NoSuchElementException.class, iterator::next);
+        }
+    }
+
     @Test
     public void testIteratorSequenceBreaking() throws IOException {
         final String fiveRows = "1\n2\n3\n4\n5\n";


(commons-csv) 02/03: Javadoc

Posted by gg...@apache.org.
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 9b75f05c9934cad8cc0886a7215e5c936e92662d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Mar 8 11:51:45 2025 -0500

    Javadoc
---
 src/main/java/org/apache/commons/csv/CSVParser.java | 6 +++---
 src/main/java/org/apache/commons/csv/CSVRecord.java | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java
index 0cd37c9a..2e4c8133 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -225,7 +225,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
          *
          * @throws IOException  on parse error or input read-failure
          * @throws CSVException on invalid input.
-         * @return the next record.
+         * @return the next record, or {@code null} if the end of the stream has been reached.
          */
         private CSVRecord getNextRecord() {
             return Uncheck.get(CSVParser.this::nextRecord);
@@ -876,8 +876,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
     /**
      * Parses the next record from the current point in the stream.
      *
-     * @return the record as an array of values, or {@code null} if the end of the stream has been reached
-     * @throws IOException  on parse error or input read-failure
+     * @return the record as an array of values, or {@code null} if the end of the stream has been reached.
+     * @throws IOException  on parse error or input read-failure.
      * @throws CSVException on invalid input.
      */
     CSVRecord nextRecord() throws IOException {
diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java
index b120f945..33642530 100644
--- a/src/main/java/org/apache/commons/csv/CSVRecord.java
+++ b/src/main/java/org/apache/commons/csv/CSVRecord.java
@@ -197,7 +197,7 @@ public final class CSVRecord implements Serializable, Iterable<String> {
      * Returns the number of this record in the parsed CSV file.
      *
      * <p>
-     * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to
+     * <strong>NOTE:</strong>If your CSV input has multi-line values, the returned number does not correspond to
      * the current line number of the parser that created this record.
      * </p>
      *


(commons-csv) 01/03: CSVParser.getRecords() knows how to use CSVFormat's maxRows

Posted by gg...@apache.org.
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 7bfb0ceaf708a649becb319a06a2d7f8e5022e9b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Mar 8 11:49:24 2025 -0500

    CSVParser.getRecords() knows how to use CSVFormat's maxRows
    
    CSVParser.stream() knows how to use CSVFormat's maxRows
---
 src/changes/changes.xml                            |  2 ++
 .../java/org/apache/commons/csv/CSVFormat.java     | 11 ++++++++
 .../java/org/apache/commons/csv/CSVParser.java     |  2 +-
 .../java/org/apache/commons/csv/CSVPrinter.java    |  2 +-
 .../java/org/apache/commons/csv/CSVParserTest.java | 32 ++++++++++++++++++++++
 .../org/apache/commons/csv/CSVPrinterTest.java     | 14 +++++-----
 6 files changed, 54 insertions(+), 9 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4898ae8e..80a6edd6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,8 @@
       <action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(ResultSet) knows how to use CSVFormat's maxRows.</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(Iterable) knows how to use CSVFormat's maxRows.</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(Stream) knows how to use CSVFormat's maxRows.</action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">CSVParser.stream() knows how to use CSVFormat's maxRows.</action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">CSVParser.getRecords() knows how to use CSVFormat's maxRows.</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump com.opencsv:opencsv from 5.9 to 5.10.</action>
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.2 to 1.18.0 #522.</action>
diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java
index 1db9b595..5f8c76ea 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -40,9 +40,11 @@ import java.util.HashSet;
 import java.util.Objects;
 import java.util.Set;
 import java.util.function.Supplier;
+import java.util.stream.Stream;
 
 import org.apache.commons.codec.binary.Base64OutputStream;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.function.IOStream;
 import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.io.output.AppendableOutputStream;
 
@@ -2088,6 +2090,14 @@ public final class CSVFormat implements Serializable {
         return quoteCharacter != null;
     }
 
+    <T> IOStream<T> limit(final IOStream<T> stream) {
+        return getMaxRows() > 0 ? stream.limit(getMaxRows()) : stream;
+    }
+
+    <T> Stream<T> limit(final Stream<T> stream) {
+        return getMaxRows() > 0 ? stream.limit(getMaxRows()) : stream;
+    }
+
     /**
      * Parses the specified content.
      *
@@ -3178,4 +3188,5 @@ public final class CSVFormat implements Serializable {
     public CSVFormat withTrim(final boolean trim) {
         return builder().setTrim(trim).get();
     }
+
 }
diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java
index 1c88d9c7..0cd37c9a 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -938,7 +938,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
      * @since 1.9.0
      */
     public Stream<CSVRecord> stream() {
-        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED), false);
+        return format.limit(StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED), false));
     }
 
 }
diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java
index 7c17d1e7..ec0b8f1d 100644
--- a/src/main/java/org/apache/commons/csv/CSVPrinter.java
+++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java
@@ -345,7 +345,7 @@ public final class CSVPrinter implements Flushable, Closeable {
 
     @SuppressWarnings("resource")
     private void printRecords(final IOStream<?> stream) throws IOException {
-        (format.getMaxRows() > 0 ? stream.limit(format.getMaxRows()) : stream).forEachOrdered(this::printRecordObject);
+        format.limit(stream).forEachOrdered(this::printRecordObject);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index 59a1dd6e..50bf052b 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -62,6 +62,7 @@ import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.EnumSource;
+import org.junit.jupiter.params.provider.ValueSource;
 
 /**
  * CSVParserTest
@@ -887,6 +888,20 @@ public class CSVParserTest {
 
     }
 
+    @ParameterizedTest
+    @ValueSource(longs = { -1, 0, 1, 2, 3, 4, Long.MAX_VALUE })
+    public void testGetRecordsMaxRows(final long maxRows) throws IOException {
+        try (CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.builder().setIgnoreSurroundingSpaces(true).setMaxRows(maxRows).get())) {
+            final List<CSVRecord> records = parser.getRecords();
+            final long expectedLength = maxRows <= 0 || maxRows > RESULT.length ? RESULT.length : maxRows;
+            assertEquals(expectedLength, records.size());
+            assertFalse(records.isEmpty());
+            for (int i = 0; i < expectedLength; i++) {
+                assertArrayEquals(RESULT[i], records.get(i).values());
+            }
+        }
+    }
+
     @Test
     public void testGetRecordThreeBytesRead() throws Exception {
         final String code = "id,date,val5,val4\n" +
@@ -1656,6 +1671,23 @@ public class CSVParserTest {
         }
     }
 
+    @ParameterizedTest
+    @ValueSource(longs = { -1, 0, 1, 2, 3, 4, Long.MAX_VALUE })
+    public void testStreamMaxRows(final long maxRows) throws Exception {
+        final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+        try (CSVParser parser = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get().parse(in)) {
+            final List<CSVRecord> list = parser.stream().collect(Collectors.toList());
+            assertFalse(list.isEmpty());
+            assertArrayEquals(new String[] { "a", "b", "c" }, list.get(0).values());
+            if (maxRows <= 0 || maxRows > 1) {
+                assertArrayEquals(new String[] { "1", "2", "3" }, list.get(1).values());
+            }
+            if (maxRows <= 0 || maxRows > 2) {
+                assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
+            }
+        }
+    }
+
     @Test
     public void testThrowExceptionWithLineAndPosition() throws IOException {
         final String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\nrec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10";
diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
index 36542b99..a1a59cf4 100644
--- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
@@ -710,7 +710,7 @@ public class CSVPrinterTest {
     }
 
     @ParameterizedTest
-    @ValueSource(longs = { -1, 0, 1, 2, Integer.MAX_VALUE })
+    @ValueSource(longs = { -1, 0, 1, 2, Long.MAX_VALUE })
     public void testExcelPrintAllStreamOfArrays(final long maxRows) throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVFormat format = CSVFormat.EXCEL.builder().setMaxRows(maxRows).get();
@@ -840,7 +840,7 @@ public class CSVPrinterTest {
     }
 
     @ParameterizedTest
-    @ValueSource(longs = { -1, 0, 1, 2, 3, 4, Integer.MAX_VALUE })
+    @ValueSource(longs = { -1, 0, 1, 2, 3, 4, Long.MAX_VALUE })
     public void testJdbcPrinterWithResultSet(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
         final StringWriter sw = new StringWriter();
         final CSVFormat format = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get();
@@ -864,11 +864,11 @@ public class CSVPrinterTest {
             assertEquals(allRows, resultString);
             expectedRowsWithHeader = TABLE_AND_HEADER_RECORD_COUNT;
         }
-        assertRowCount(format, resultString, expectedRowsWithHeader);
+        assertRowCount(CSVFormat.DEFAULT, resultString, expectedRowsWithHeader);
     }
 
     @ParameterizedTest
-    @ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
+    @ValueSource(longs = { -1, 0, 3, 4, Long.MAX_VALUE })
     public void testJdbcPrinterWithResultSetHeader(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
         final StringWriter sw = new StringWriter();
         try (Connection connection = getH2Connection()) {
@@ -887,13 +887,13 @@ public class CSVPrinterTest {
                     assertEquals(TABLE_RECORD_COUNT * 2, printer.getRecordCount());
                     assertNotEquals("ID,NAME" + RECORD_SEPARATOR + "1,r1" + RECORD_SEPARATOR + "2,r2" + RECORD_SEPARATOR, sw.toString());
                 }
-                assertRowCount(format, sw.toString(), TABLE_AND_HEADER_RECORD_COUNT + TABLE_RECORD_COUNT);
+                assertRowCount(CSVFormat.DEFAULT, sw.toString(), TABLE_AND_HEADER_RECORD_COUNT + TABLE_RECORD_COUNT);
             }
         }
     }
 
     @ParameterizedTest
-    @ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
+    @ValueSource(longs = { -1, 0, 3, 4, Long.MAX_VALUE })
     public void testJdbcPrinterWithResultSetMetaData(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
         final StringWriter sw = new StringWriter();
         try (Connection connection = getH2Connection()) {
@@ -1509,7 +1509,7 @@ public class CSVPrinterTest {
     }
 
     @ParameterizedTest
-    @ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
+    @ValueSource(longs = { -1, 0, 3, 4, Long.MAX_VALUE })
     public void testPrintCSVRecords(final long maxRows) throws IOException {
         // @formatter:off
         final String code = "a1,b1\n" + // 1)