You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/02/17 13:03:19 UTC

(commons-io) branch master updated: Add IOIterable.unwrap()

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new b0c4fc11f Add IOIterable.unwrap()
b0c4fc11f is described below

commit b0c4fc11f44c1b5d1e6c480c0a924aa4f01faa73
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Mon Feb 17 07:34:53 2025 -0500

    Add IOIterable.unwrap()
---
 src/main/java/org/apache/commons/io/function/IOIterable.java | 12 ++++++++++--
 .../org/apache/commons/io/input/ReversedLinesFileReader.java |  5 +++++
 .../java/org/apache/commons/io/function/IOIterableTest.java  | 12 ++++++++++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/function/IOIterable.java b/src/main/java/org/apache/commons/io/function/IOIterable.java
index 8a5f1f3c0..234616aa5 100644
--- a/src/main/java/org/apache/commons/io/function/IOIterable.java
+++ b/src/main/java/org/apache/commons/io/function/IOIterable.java
@@ -37,8 +37,7 @@ public interface IOIterable<T> {
      * @see Iterable#iterator()
      */
     default void forEach(final IOConsumer<? super T> action) throws IOException {
-        Objects.requireNonNull(action);
-        iterator().forEachRemaining(action);
+        iterator().forEachRemaining(Objects.requireNonNull(action));
     }
 
     /**
@@ -59,4 +58,13 @@ default IOSpliterator<T> spliterator() {
         return IOSpliteratorAdapter.adapt(new UncheckedIOIterable<>(this).spliterator());
     }
 
+    /**
+     * Unwraps this instance and returns the underlying {@link Iterable}.
+     * <p>
+     * Implementations may not have anything to unwrap and that behavior is undefined for now.
+     * </p>
+     * @return the underlying Iterable.
+     */
+    Iterable<T> unwrap();
+
 }
diff --git a/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java b/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
index f49f7349e..0ed22b264 100644
--- a/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
+++ b/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
@@ -584,4 +584,9 @@ public String toString(final int lineCount) throws IOException {
         return lines.isEmpty() ? EMPTY_STRING : String.join(System.lineSeparator(), lines) + System.lineSeparator();
     }
 
+    @Override
+    public Iterable<String> unwrap() {
+        return null;
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/io/function/IOIterableTest.java b/src/test/java/org/apache/commons/io/function/IOIterableTest.java
index f8f376170..e7915c123 100644
--- a/src/test/java/org/apache/commons/io/function/IOIterableTest.java
+++ b/src/test/java/org/apache/commons/io/function/IOIterableTest.java
@@ -18,6 +18,7 @@
 package org.apache.commons.io.function;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
@@ -44,6 +45,11 @@ public IOIterator<Path> iterator() {
             return IOIterator.adapt(list);
         }
 
+        @Override
+        public Iterable<Path> unwrap() {
+            return list;
+        }
+
     }
 
     private IOIterable<Path> iterable;
@@ -76,4 +82,10 @@ public void testSpliterator() throws IOException {
         iterable.spliterator().forEachRemaining(e -> ref.incrementAndGet());
         assertEquals(2, ref.get());
     }
+
+    @Test
+    public void testUnrwap() throws IOException {
+        assertSame(fixture.list, iterable.unwrap());
+        assertSame(fixture.unwrap(), iterable.unwrap());
+    }
 }