Skip to content

Commit 920c949

Browse files
committed
[CSV-313] Add CSVPrinter.getRecordCount()
1 parent 49c50d7 commit 920c949

3 files changed

Lines changed: 148 additions & 31 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@
4040
<title>Apache Commons CSV Release Notes</title>
4141
</properties>
4242
<body>
43-
<release version="1.12.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
43+
<release version="1.13.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4444
<!-- FIX -->
4545
<!-- ADD -->
46+
<action type="add" issue="CSV-313" dev="ggregory" due-to="Gary Gregory">Add CSVPrinter.getRecordCount().</action>
4647
<!-- UPDATE -->
4748
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 76 to 77 #486.</action>
4849
</release>

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public final class CSVPrinter implements Flushable, Closeable {
8383
/** True if we just began a new record. */
8484
private boolean newRecord = true;
8585

86+
private long recordCount;
87+
8688
/**
8789
* Creates a printer that will print values to the given stream following the CSVFormat.
8890
* <p>
@@ -140,6 +142,17 @@ public void close(final boolean flush) throws IOException {
140142
}
141143
}
142144

145+
/**
146+
* Outputs the record separator and increments the record count.
147+
*
148+
* @throws IOException
149+
* If an I/O error occurs
150+
*/
151+
private synchronized void endOfRecord() throws IOException {
152+
println();
153+
recordCount++;
154+
}
155+
143156
/**
144157
* Flushes the underlying stream.
145158
*
@@ -162,6 +175,16 @@ public Appendable getOut() {
162175
return this.appendable;
163176
}
164177

178+
/**
179+
* Gets the record count printed, this does not include comments or headers.
180+
*
181+
* @return the record count, this does not include comments or headers.
182+
* @since 1.13.0
183+
*/
184+
public long getRecordCount() {
185+
return recordCount;
186+
}
187+
165188
/**
166189
* Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
167190
*
@@ -235,7 +258,10 @@ public synchronized void printComment(final String comment) throws IOException {
235258
* @since 1.9.0
236259
*/
237260
public synchronized void printHeaders(final ResultSet resultSet) throws IOException, SQLException {
238-
printRecord((Object[]) format.builder().setHeader(resultSet).build().getHeader());
261+
try (IOStream<String> stream = IOStream.of(format.builder().setHeader(resultSet).build().getHeader())) {
262+
stream.forEachOrdered(this::print);
263+
}
264+
println();
239265
}
240266

241267
/**
@@ -265,7 +291,7 @@ public synchronized void println() throws IOException {
265291
@SuppressWarnings("resource")
266292
public synchronized void printRecord(final Iterable<?> values) throws IOException {
267293
IOStream.of(values).forEachOrdered(this::print);
268-
println();
294+
endOfRecord();
269295
}
270296

271297
/**
@@ -302,7 +328,7 @@ public void printRecord(final Object... values) throws IOException {
302328
@SuppressWarnings("resource") // caller closes.
303329
public synchronized void printRecord(final Stream<?> values) throws IOException {
304330
IOStream.adapt(values).forEachOrdered(this::print);
305-
println();
331+
endOfRecord();
306332
}
307333

308334
private void printRecordObject(final Object value) throws IOException {
@@ -426,7 +452,7 @@ public void printRecords(final ResultSet resultSet) throws SQLException, IOExcep
426452
print(object);
427453
}
428454
}
429-
println();
455+
endOfRecord();
430456
}
431457
}
432458

0 commit comments

Comments
 (0)