Skip to content

Commit

Permalink
[CSV-313] Add CSVPrinter.getRecordCount()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Oct 10, 2024
1 parent 49c50d7 commit 920c949
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
<title>Apache Commons CSV Release Notes</title>
</properties>
<body>
<release version="1.12.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
<release version="1.13.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
<!-- FIX -->
<!-- ADD -->
<action type="add" issue="CSV-313" dev="ggregory" due-to="Gary Gregory">Add CSVPrinter.getRecordCount().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 76 to 77 #486.</action>
</release>
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/org/apache/commons/csv/CSVPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public final class CSVPrinter implements Flushable, Closeable {
/** True if we just began a new record. */
private boolean newRecord = true;

private long recordCount;

/**
* Creates a printer that will print values to the given stream following the CSVFormat.
* <p>
Expand Down Expand Up @@ -140,6 +142,17 @@ public void close(final boolean flush) throws IOException {
}
}

/**
* Outputs the record separator and increments the record count.
*
* @throws IOException
* If an I/O error occurs
*/
private synchronized void endOfRecord() throws IOException {
println();
recordCount++;
}

/**
* Flushes the underlying stream.
*
Expand All @@ -162,6 +175,16 @@ public Appendable getOut() {
return this.appendable;
}

/**
* Gets the record count printed, this does not include comments or headers.
*
* @return the record count, this does not include comments or headers.
* @since 1.13.0
*/
public long getRecordCount() {
return recordCount;
}

/**
* Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
*
Expand Down Expand Up @@ -235,7 +258,10 @@ public synchronized void printComment(final String comment) throws IOException {
* @since 1.9.0
*/
public synchronized void printHeaders(final ResultSet resultSet) throws IOException, SQLException {
printRecord((Object[]) format.builder().setHeader(resultSet).build().getHeader());
try (IOStream<String> stream = IOStream.of(format.builder().setHeader(resultSet).build().getHeader())) {
stream.forEachOrdered(this::print);
}
println();
}

/**
Expand Down Expand Up @@ -265,7 +291,7 @@ public synchronized void println() throws IOException {
@SuppressWarnings("resource")
public synchronized void printRecord(final Iterable<?> values) throws IOException {
IOStream.of(values).forEachOrdered(this::print);
println();
endOfRecord();
}

/**
Expand Down Expand Up @@ -302,7 +328,7 @@ public void printRecord(final Object... values) throws IOException {
@SuppressWarnings("resource") // caller closes.
public synchronized void printRecord(final Stream<?> values) throws IOException {
IOStream.adapt(values).forEachOrdered(this::print);
println();
endOfRecord();
}

private void printRecordObject(final Object value) throws IOException {
Expand Down Expand Up @@ -426,7 +452,7 @@ public void printRecords(final ResultSet resultSet) throws SQLException, IOExcep
print(object);
}
}
println();
endOfRecord();
}
}

Expand Down
Loading

0 comments on commit 920c949

Please sign in to comment.