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/07 21:11:34 UTC

(commons-csv) branch master updated (30f0e1d6 -> b25b2509)

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 30f0e1d6 CSVPrinter.printRecords(ResultSet) knows how to use CSVFormat's maxRows
     new a557344f Javadoc
     new b25b2509 CSVPrinter.printRecords(Iterable) knows how to use CSVFormat's maxRows

The 2 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 ++-
 src/main/java/org/apache/commons/csv/CSVFormat.java   |  6 ++++++
 src/main/java/org/apache/commons/csv/CSVPrinter.java  |  6 +++++-
 .../java/org/apache/commons/csv/CSVParserTest.java    |  8 ++++----
 .../java/org/apache/commons/csv/CSVPrinterTest.java   | 19 ++++++++++---------
 src/test/java/org/apache/commons/csv/Utils.java       |  5 +++--
 6 files changed, 30 insertions(+), 17 deletions(-)


(commons-csv) 02/02: CSVPrinter.printRecords(Iterable) 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 b25b2509c839f490546586d70f360448bb17e449
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 7 16:11:27 2025 -0500

    CSVPrinter.printRecords(Iterable) knows how to use CSVFormat's maxRows
---
 src/changes/changes.xml                               |  3 ++-
 src/main/java/org/apache/commons/csv/CSVPrinter.java  |  6 +++++-
 .../java/org/apache/commons/csv/CSVParserTest.java    |  8 ++++----
 .../java/org/apache/commons/csv/CSVPrinterTest.java   | 19 ++++++++++---------
 src/test/java/org/apache/commons/csv/Utils.java       |  5 +++--
 5 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8632cf78..91fb9b41 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -48,7 +48,8 @@
       <action type="add" dev="ggregory" due-to="Gary Gregory">Define and use Maven property commons.jmh.version.</action> 
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.Builder.setMaxRows(long).</action> 
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.getMaxRows().</action> 
-      <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(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>
       <!-- 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/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java
index a09e0b63..d2f08b57 100644
--- a/src/main/java/org/apache/commons/csv/CSVPrinter.java
+++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java
@@ -383,7 +383,11 @@ public final class CSVPrinter implements Flushable, Closeable {
      */
     @SuppressWarnings("resource")
     public void printRecords(final Iterable<?> values) throws IOException {
-        IOStream.of(values).forEachOrdered(this::printRecordObject);
+        IOStream<?> stream = IOStream.of(values);
+        if (format.getMaxRows() > 0) {
+            stream = stream.limit(format.getMaxRows());
+        }
+        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 38d442e5..59a1dd6e 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -169,7 +169,7 @@ public class CSVParserTest {
         try (CSVParser parser = CSVParser.parse(code, format)) {
             final List<CSVRecord> records = parser.getRecords();
             assertFalse(records.isEmpty());
-            Utils.compare("Records do not match expected result", res, records);
+            Utils.compare("Records do not match expected result", res, records, -1);
         }
     }
 
@@ -192,7 +192,7 @@ public class CSVParserTest {
         try (CSVParser parser = CSVParser.parse(code, format)) {
             final List<CSVRecord> records = parser.getRecords();
             assertFalse(records.isEmpty());
-            Utils.compare("", res, records);
+            Utils.compare("", res, records, -1);
         }
     }
 
@@ -428,12 +428,12 @@ public class CSVParserTest {
         try (CSVParser parser = CSVParser.parse(code, format)) {
             final List<CSVRecord> records = parser.getRecords();
             assertFalse(records.isEmpty());
-            Utils.compare("Failed to parse without comments", res, records);
+            Utils.compare("Failed to parse without comments", res, records, -1);
             format = CSVFormat.DEFAULT.withCommentMarker('#');
         }
         try (CSVParser parser = CSVParser.parse(code, format)) {
             final List<CSVRecord> records = parser.getRecords();
-            Utils.compare("Failed to parse with comments", resComments, records);
+            Utils.compare("Failed to parse with comments", resComments, records, -1);
         }
     }
 
diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
index 9e865a1a..b1ac8053 100644
--- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
@@ -146,7 +146,7 @@ public class CSVPrinterTest {
             for (int i = 0; i < expected.length; i++) {
                 expected[i] = expectNulls(expected[i], format);
             }
-            Utils.compare("Printer output :" + printable(result), expected, parseResult);
+            Utils.compare("Printer output :" + printable(result), expected, parseResult, -1);
         }
     }
 
@@ -1470,7 +1470,7 @@ public class CSVPrinterTest {
         try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
             final List<CSVRecord> records = parser.getRecords();
             assertFalse(records.isEmpty());
-            Utils.compare("Fail", res, records);
+            Utils.compare("Fail", res, records, -1);
         }
     }
 
@@ -1498,20 +1498,21 @@ public class CSVPrinterTest {
         try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
             final List<CSVRecord> records = parser.getRecords();
             assertFalse(records.isEmpty());
-            Utils.compare("Fail", res, records);
+            Utils.compare("Fail", res, records, -1);
         }
     }
 
-    @Test
-    public void testPrintCSVRecords() throws IOException {
+    @ParameterizedTest
+    @ValueSource(ints = { -1, 0, 3, 4, Integer.MAX_VALUE })
+    public void testPrintCSVRecords(final int maxRows) throws IOException {
         // @formatter:off
         final String code = "a1,b1\n" + // 1)
                 "a2,b2\n" + // 2)
                 "a3,b3\n" + // 3)
                 "a4,b4\n";  // 4)
         // @formatter:on
-        final String[][] res = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
-        final CSVFormat format = CSVFormat.DEFAULT;
+        final String[][] expected = { { "a1", "b1" }, { "a2", "b2" }, { "a3", "b3" }, { "a4", "b4" } };
+        final CSVFormat format = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get();
         final StringWriter sw = new StringWriter();
         try (CSVPrinter printer = format.print(sw);
                 CSVParser parser = CSVParser.parse(code, format)) {
@@ -1521,7 +1522,7 @@ public class CSVPrinterTest {
         try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
             final List<CSVRecord> records = parser.getRecords();
             assertFalse(records.isEmpty());
-            Utils.compare("Fail", res, records);
+            Utils.compare("Fail", expected, records, maxRows);
         }
     }
 
@@ -1690,7 +1691,7 @@ public class CSVPrinterTest {
         try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
             final List<CSVRecord> records = parser.getRecords();
             assertFalse(records.isEmpty());
-            Utils.compare("Fail", res, records);
+            Utils.compare("Fail", res, records, -1);
         }
     }
 
diff --git a/src/test/java/org/apache/commons/csv/Utils.java b/src/test/java/org/apache/commons/csv/Utils.java
index c99b77ac..32a460b6 100644
--- a/src/test/java/org/apache/commons/csv/Utils.java
+++ b/src/test/java/org/apache/commons/csv/Utils.java
@@ -37,9 +37,10 @@ final class Utils {
      * @param message  the message to be displayed
      * @param expected the 2d array of expected results
      * @param actual   the List of {@link CSVRecord} entries, each containing an array of values
+     * @param maxRows  the maximum number of rows expected, less than or equal to zero means no limit.
      */
-    public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual) {
-        final int expectedLength = expected.length;
+    public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual, final int maxRows) {
+        final int expectedLength = maxRows > 0 ? Math.min(maxRows, expected.length) : 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 + ")");


(commons-csv) 01/02: 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 a557344f3d2f5b9c2f58a8859533914772a2dbf1
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Mar 7 15:37:28 2025 -0500

    Javadoc
---
 src/main/java/org/apache/commons/csv/CSVFormat.java | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java
index ea1613ba..1db9b595 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -744,6 +744,9 @@ public final class CSVFormat implements Serializable {
 
         /**
          * Sets the maximum number of rows to process, excluding the header row.
+         * <p>
+         * Values less than or equal to 0 mean no limit.
+         * </p>
          *
          * @param maxRows the maximum number of rows to process, excluding the header row.
          * @return This instance.
@@ -1921,6 +1924,9 @@ public final class CSVFormat implements Serializable {
 
     /**
      * Gets the maximum number of rows to process, excluding the header row.
+     * <p>
+     * Values less than or equal to 0 mean no limit.
+     * </p>
      *
      * @return The maximum number of rows to process, excluding the header row.
      * @since 1.14.0