Skip to content

Commit e24e3af

Browse files
committed
CSVPrinter.printRecords(Stream) knows how to use CSVFormat's maxRows
1 parent b25b250 commit e24e3af

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.getMaxRows().</action>
5151
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(ResultSet) knows how to use CSVFormat's maxRows.</action>
5252
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(Iterable) knows how to use CSVFormat's maxRows.</action>
53+
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(Stream) knows how to use CSVFormat's maxRows.</action>
5354
<!-- UPDATE -->
5455
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump com.opencsv:opencsv from 5.9 to 5.10.</action>
5556
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.2 to 1.18.0 #522.</action>

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ private void printRecordObject(final Object value) throws IOException {
343343
}
344344
}
345345

346+
@SuppressWarnings("resource")
347+
private void printRecords(final IOStream<?> stream) throws IOException {
348+
(format.getMaxRows() > 0 ? stream.limit(format.getMaxRows()) : stream).forEachOrdered(this::printRecordObject);
349+
}
350+
346351
/**
347352
* Prints all the objects in the given {@link Iterable} handling nested collections/arrays as records.
348353
*
@@ -383,11 +388,7 @@ private void printRecordObject(final Object value) throws IOException {
383388
*/
384389
@SuppressWarnings("resource")
385390
public void printRecords(final Iterable<?> values) throws IOException {
386-
IOStream<?> stream = IOStream.of(values);
387-
if (format.getMaxRows() > 0) {
388-
stream = stream.limit(format.getMaxRows());
389-
}
390-
stream.forEachOrdered(this::printRecordObject);
391+
printRecords(IOStream.of(values));
391392
}
392393

393394
/**
@@ -526,6 +527,6 @@ public void printRecords(final ResultSet resultSet, final boolean printHeader) t
526527
*/
527528
@SuppressWarnings({ "resource" }) // Caller closes.
528529
public void printRecords(final Stream<?> values) throws IOException {
529-
IOStream.adapt(values).forEachOrdered(this::printRecordObject);
530+
printRecords(IOStream.adapt(values));
530531
}
531532
}

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -709,13 +709,19 @@ public void testExcelPrintAllIterableOfLists() throws IOException {
709709
}
710710
}
711711

712-
@Test
713-
public void testExcelPrintAllStreamOfArrays() throws IOException {
712+
@ParameterizedTest
713+
@ValueSource(longs = { -1, 0, 1, 2, Integer.MAX_VALUE })
714+
public void testExcelPrintAllStreamOfArrays(final long maxRows) throws IOException {
714715
final StringWriter sw = new StringWriter();
715-
try (CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) {
716+
final CSVFormat format = CSVFormat.EXCEL.builder().setMaxRows(maxRows).get();
717+
try (CSVPrinter printer = new CSVPrinter(sw, format)) {
716718
assertInitialState(printer);
717719
printer.printRecords(Stream.of(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
718-
assertEquals("r1c1,r1c2" + RECORD_SEPARATOR + "r2c1,r2c2" + RECORD_SEPARATOR, sw.toString());
720+
String expected = "r1c1,r1c2" + RECORD_SEPARATOR;
721+
if (maxRows != 1) {
722+
expected += "r2c1,r2c2" + RECORD_SEPARATOR;
723+
}
724+
assertEquals(expected, sw.toString());
719725
}
720726
}
721727

@@ -834,8 +840,8 @@ public void testJdbcPrinterWithFirstEmptyValue2() throws IOException, ClassNotFo
834840
}
835841

836842
@ParameterizedTest
837-
@ValueSource(ints = { -1, 0, 1, 2, 3, 4, Integer.MAX_VALUE })
838-
public void testJdbcPrinterWithResultSet(final int maxRows) throws IOException, ClassNotFoundException, SQLException {
843+
@ValueSource(longs = { -1, 0, 1, 2, 3, 4, Integer.MAX_VALUE })
844+
public void testJdbcPrinterWithResultSet(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
839845
final StringWriter sw = new StringWriter();
840846
final CSVFormat format = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get();
841847
try (Connection connection = getH2Connection()) {
@@ -862,8 +868,8 @@ public void testJdbcPrinterWithResultSet(final int maxRows) throws IOException,
862868
}
863869

864870
@ParameterizedTest
865-
@ValueSource(ints = { -1, 0, 3, 4, Integer.MAX_VALUE })
866-
public void testJdbcPrinterWithResultSetHeader(final int maxRows) throws IOException, ClassNotFoundException, SQLException {
871+
@ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
872+
public void testJdbcPrinterWithResultSetHeader(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
867873
final StringWriter sw = new StringWriter();
868874
try (Connection connection = getH2Connection()) {
869875
setUpTable(connection);
@@ -887,8 +893,8 @@ public void testJdbcPrinterWithResultSetHeader(final int maxRows) throws IOExcep
887893
}
888894

889895
@ParameterizedTest
890-
@ValueSource(ints = { -1, 0, 3, 4, Integer.MAX_VALUE })
891-
public void testJdbcPrinterWithResultSetMetaData(final int maxRows) throws IOException, ClassNotFoundException, SQLException {
896+
@ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
897+
public void testJdbcPrinterWithResultSetMetaData(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
892898
final StringWriter sw = new StringWriter();
893899
try (Connection connection = getH2Connection()) {
894900
setUpTable(connection);
@@ -1503,8 +1509,8 @@ public void testPrintCSVRecord() throws IOException {
15031509
}
15041510

15051511
@ParameterizedTest
1506-
@ValueSource(ints = { -1, 0, 3, 4, Integer.MAX_VALUE })
1507-
public void testPrintCSVRecords(final int maxRows) throws IOException {
1512+
@ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
1513+
public void testPrintCSVRecords(final long maxRows) throws IOException {
15081514
// @formatter:off
15091515
final String code = "a1,b1\n" + // 1)
15101516
"a2,b2\n" + // 2)

src/test/java/org/apache/commons/csv/Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ final class Utils {
3939
* @param actual the List of {@link CSVRecord} entries, each containing an array of values
4040
* @param maxRows the maximum number of rows expected, less than or equal to zero means no limit.
4141
*/
42-
public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual, final int maxRows) {
43-
final int expectedLength = maxRows > 0 ? Math.min(maxRows, expected.length) : expected.length;
42+
public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual, final long maxRows) {
43+
final long expectedLength = maxRows > 0 ? Math.min(maxRows, expected.length) : expected.length;
4444
assertEquals(expectedLength, actual.size(), message + " - outer array size");
4545
for (int i = 0; i < expectedLength; i++) {
4646
assertArrayEquals(expected[i], actual.get(i).values(), message + " (entry " + i + ")");

0 commit comments

Comments
 (0)