Skip to content

Commit 7bfb0ce

Browse files
committed
CSVParser.getRecords() knows how to use CSVFormat's maxRows
CSVParser.stream() knows how to use CSVFormat's maxRows
1 parent 7b4e105 commit 7bfb0ce

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
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>
5353
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVPrinter.printRecords(Stream) knows how to use CSVFormat's maxRows.</action>
54+
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVParser.stream() knows how to use CSVFormat's maxRows.</action>
55+
<action type="add" dev="ggregory" due-to="Gary Gregory">CSVParser.getRecords() knows how to use CSVFormat's maxRows.</action>
5456
<!-- UPDATE -->
5557
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump com.opencsv:opencsv from 5.9 to 5.10.</action>
5658
<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/CSVFormat.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
import java.util.Objects;
4141
import java.util.Set;
4242
import java.util.function.Supplier;
43+
import java.util.stream.Stream;
4344

4445
import org.apache.commons.codec.binary.Base64OutputStream;
4546
import org.apache.commons.io.IOUtils;
47+
import org.apache.commons.io.function.IOStream;
4648
import org.apache.commons.io.function.Uncheck;
4749
import org.apache.commons.io.output.AppendableOutputStream;
4850

@@ -2088,6 +2090,14 @@ public boolean isQuoteCharacterSet() {
20882090
return quoteCharacter != null;
20892091
}
20902092

2093+
<T> IOStream<T> limit(final IOStream<T> stream) {
2094+
return getMaxRows() > 0 ? stream.limit(getMaxRows()) : stream;
2095+
}
2096+
2097+
<T> Stream<T> limit(final Stream<T> stream) {
2098+
return getMaxRows() > 0 ? stream.limit(getMaxRows()) : stream;
2099+
}
2100+
20912101
/**
20922102
* Parses the specified content.
20932103
*
@@ -3178,4 +3188,5 @@ public CSVFormat withTrim() {
31783188
public CSVFormat withTrim(final boolean trim) {
31793189
return builder().setTrim(trim).get();
31803190
}
3191+
31813192
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ CSVRecord nextRecord() throws IOException {
938938
* @since 1.9.0
939939
*/
940940
public Stream<CSVRecord> stream() {
941-
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED), false);
941+
return format.limit(StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED), false));
942942
}
943943

944944
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private void printRecordObject(final Object value) throws IOException {
345345

346346
@SuppressWarnings("resource")
347347
private void printRecords(final IOStream<?> stream) throws IOException {
348-
(format.getMaxRows() > 0 ? stream.limit(format.getMaxRows()) : stream).forEachOrdered(this::printRecordObject);
348+
format.limit(stream).forEachOrdered(this::printRecordObject);
349349
}
350350

351351
/**

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.junit.jupiter.api.Test;
6363
import org.junit.jupiter.params.ParameterizedTest;
6464
import org.junit.jupiter.params.provider.EnumSource;
65+
import org.junit.jupiter.params.provider.ValueSource;
6566

6667
/**
6768
* CSVParserTest
@@ -887,6 +888,20 @@ public void testGetRecordsFromBrokenInputStream() throws IOException {
887888

888889
}
889890

891+
@ParameterizedTest
892+
@ValueSource(longs = { -1, 0, 1, 2, 3, 4, Long.MAX_VALUE })
893+
public void testGetRecordsMaxRows(final long maxRows) throws IOException {
894+
try (CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.builder().setIgnoreSurroundingSpaces(true).setMaxRows(maxRows).get())) {
895+
final List<CSVRecord> records = parser.getRecords();
896+
final long expectedLength = maxRows <= 0 || maxRows > RESULT.length ? RESULT.length : maxRows;
897+
assertEquals(expectedLength, records.size());
898+
assertFalse(records.isEmpty());
899+
for (int i = 0; i < expectedLength; i++) {
900+
assertArrayEquals(RESULT[i], records.get(i).values());
901+
}
902+
}
903+
}
904+
890905
@Test
891906
public void testGetRecordThreeBytesRead() throws Exception {
892907
final String code = "id,date,val5,val4\n" +
@@ -1656,6 +1671,23 @@ public void testStream() throws Exception {
16561671
}
16571672
}
16581673

1674+
@ParameterizedTest
1675+
@ValueSource(longs = { -1, 0, 1, 2, 3, 4, Long.MAX_VALUE })
1676+
public void testStreamMaxRows(final long maxRows) throws Exception {
1677+
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
1678+
try (CSVParser parser = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get().parse(in)) {
1679+
final List<CSVRecord> list = parser.stream().collect(Collectors.toList());
1680+
assertFalse(list.isEmpty());
1681+
assertArrayEquals(new String[] { "a", "b", "c" }, list.get(0).values());
1682+
if (maxRows <= 0 || maxRows > 1) {
1683+
assertArrayEquals(new String[] { "1", "2", "3" }, list.get(1).values());
1684+
}
1685+
if (maxRows <= 0 || maxRows > 2) {
1686+
assertArrayEquals(new String[] { "x", "y", "z" }, list.get(2).values());
1687+
}
1688+
}
1689+
}
1690+
16591691
@Test
16601692
public void testThrowExceptionWithLineAndPosition() throws IOException {
16611693
final String csvContent = "col1,col2,col3,col4,col5,col6,col7,col8,col9,col10\nrec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8,\"\"rec9\"\",rec10";

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ public void testExcelPrintAllIterableOfLists() throws IOException {
710710
}
711711

712712
@ParameterizedTest
713-
@ValueSource(longs = { -1, 0, 1, 2, Integer.MAX_VALUE })
713+
@ValueSource(longs = { -1, 0, 1, 2, Long.MAX_VALUE })
714714
public void testExcelPrintAllStreamOfArrays(final long maxRows) throws IOException {
715715
final StringWriter sw = new StringWriter();
716716
final CSVFormat format = CSVFormat.EXCEL.builder().setMaxRows(maxRows).get();
@@ -840,7 +840,7 @@ public void testJdbcPrinterWithFirstEmptyValue2() throws IOException, ClassNotFo
840840
}
841841

842842
@ParameterizedTest
843-
@ValueSource(longs = { -1, 0, 1, 2, 3, 4, Integer.MAX_VALUE })
843+
@ValueSource(longs = { -1, 0, 1, 2, 3, 4, Long.MAX_VALUE })
844844
public void testJdbcPrinterWithResultSet(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
845845
final StringWriter sw = new StringWriter();
846846
final CSVFormat format = CSVFormat.DEFAULT.builder().setMaxRows(maxRows).get();
@@ -864,11 +864,11 @@ public void testJdbcPrinterWithResultSet(final long maxRows) throws IOException,
864864
assertEquals(allRows, resultString);
865865
expectedRowsWithHeader = TABLE_AND_HEADER_RECORD_COUNT;
866866
}
867-
assertRowCount(format, resultString, expectedRowsWithHeader);
867+
assertRowCount(CSVFormat.DEFAULT, resultString, expectedRowsWithHeader);
868868
}
869869

870870
@ParameterizedTest
871-
@ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
871+
@ValueSource(longs = { -1, 0, 3, 4, Long.MAX_VALUE })
872872
public void testJdbcPrinterWithResultSetHeader(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
873873
final StringWriter sw = new StringWriter();
874874
try (Connection connection = getH2Connection()) {
@@ -887,13 +887,13 @@ public void testJdbcPrinterWithResultSetHeader(final long maxRows) throws IOExce
887887
assertEquals(TABLE_RECORD_COUNT * 2, printer.getRecordCount());
888888
assertNotEquals("ID,NAME" + RECORD_SEPARATOR + "1,r1" + RECORD_SEPARATOR + "2,r2" + RECORD_SEPARATOR, sw.toString());
889889
}
890-
assertRowCount(format, sw.toString(), TABLE_AND_HEADER_RECORD_COUNT + TABLE_RECORD_COUNT);
890+
assertRowCount(CSVFormat.DEFAULT, sw.toString(), TABLE_AND_HEADER_RECORD_COUNT + TABLE_RECORD_COUNT);
891891
}
892892
}
893893
}
894894

895895
@ParameterizedTest
896-
@ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
896+
@ValueSource(longs = { -1, 0, 3, 4, Long.MAX_VALUE })
897897
public void testJdbcPrinterWithResultSetMetaData(final long maxRows) throws IOException, ClassNotFoundException, SQLException {
898898
final StringWriter sw = new StringWriter();
899899
try (Connection connection = getH2Connection()) {
@@ -1509,7 +1509,7 @@ public void testPrintCSVRecord() throws IOException {
15091509
}
15101510

15111511
@ParameterizedTest
1512-
@ValueSource(longs = { -1, 0, 3, 4, Integer.MAX_VALUE })
1512+
@ValueSource(longs = { -1, 0, 3, 4, Long.MAX_VALUE })
15131513
public void testPrintCSVRecords(final long maxRows) throws IOException {
15141514
// @formatter:off
15151515
final String code = "a1,b1\n" + // 1)

0 commit comments

Comments
 (0)