Skip to content

Commit b0c4fc1

Browse files
committed
Add IOIterable.unwrap()
1 parent ce20c6a commit b0c4fc1

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/main/java/org/apache/commons/io/function/IOIterable.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public interface IOIterable<T> {
3737
* @see Iterable#iterator()
3838
*/
3939
default void forEach(final IOConsumer<? super T> action) throws IOException {
40-
Objects.requireNonNull(action);
41-
iterator().forEachRemaining(action);
40+
iterator().forEachRemaining(Objects.requireNonNull(action));
4241
}
4342

4443
/**
@@ -59,4 +58,13 @@ default IOSpliterator<T> spliterator() {
5958
return IOSpliteratorAdapter.adapt(new UncheckedIOIterable<>(this).spliterator());
6059
}
6160

61+
/**
62+
* Unwraps this instance and returns the underlying {@link Iterable}.
63+
* <p>
64+
* Implementations may not have anything to unwrap and that behavior is undefined for now.
65+
* </p>
66+
* @return the underlying Iterable.
67+
*/
68+
Iterable<T> unwrap();
69+
6270
}

src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,9 @@ public String toString(final int lineCount) throws IOException {
584584
return lines.isEmpty() ? EMPTY_STRING : String.join(System.lineSeparator(), lines) + System.lineSeparator();
585585
}
586586

587+
@Override
588+
public Iterable<String> unwrap() {
589+
return null;
590+
}
591+
587592
}

src/test/java/org/apache/commons/io/function/IOIterableTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.commons.io.function;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertSame;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
2223

2324
import java.io.IOException;
@@ -44,6 +45,11 @@ public IOIterator<Path> iterator() {
4445
return IOIterator.adapt(list);
4546
}
4647

48+
@Override
49+
public Iterable<Path> unwrap() {
50+
return list;
51+
}
52+
4753
}
4854

4955
private IOIterable<Path> iterable;
@@ -76,4 +82,10 @@ public void testSpliterator() throws IOException {
7682
iterable.spliterator().forEachRemaining(e -> ref.incrementAndGet());
7783
assertEquals(2, ref.get());
7884
}
85+
86+
@Test
87+
public void testUnrwap() throws IOException {
88+
assertSame(fixture.list, iterable.unwrap());
89+
assertSame(fixture.unwrap(), iterable.unwrap());
90+
}
7991
}

0 commit comments

Comments
 (0)