kris-liheng commented on code in PR #5407:
URL: https://github.com/apache/iceberg/pull/5407#discussion_r936837532
##########
api/src/main/java/org/apache/iceberg/io/CloseableIterable.java:
##########
@@ -73,6 +74,36 @@ public CloseableIterator<E> iterator() {
};
}
+ /**
+ * Will run the given runnable when {@link CloseableIterable#close()} has
been called.
+ *
+ * @param iterable The underlying {@link CloseableIterable} to iterate over
+ * @param onCompletionRunnable The runnable to run after the underlying
iterable was closed
+ * @param <E> The type of the underlying iterable
+ * @return A new {@link CloseableIterable} where the runnable will be
executed as the final step
+ * after {@link CloseableIterable#close()} has been called
+ */
+ static <E> CloseableIterable<E> whenComplete(
+ CloseableIterable<E> iterable, Runnable onCompletionRunnable) {
+ Preconditions.checkNotNull(
+ onCompletionRunnable, "Cannot execute a null Runnable after
completion");
+ return new CloseableIterable<E>() {
+ @Override
+ public void close() throws IOException {
+ try {
+ iterable.close();
+ } finally {
+ onCompletionRunnable.run();
+ }
+ }
+
+ @Override
+ public CloseableIterator<E> iterator() {
+ return CloseableIterator.withClose(iterable.iterator());
Review Comment:
Nit: Why not just return `iterable.iterator()` here (instead , it was
wrapped with a `CloseableIterator.withClose`) ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]