Skip to content

Commit a96ae74

Browse files
committed
Add IOIterable.asIterable()
1 parent 9bd6a06 commit a96ae74

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The <action> type attribute can be add,update,fix,remove.
6464
<action dev="pkarwasz" type="add" due-to="Piotr P. Karwasz">Add IOUtils.toByteArray(InputStream, int, int) for safer chunked reading with size validation.</action>
6565
<action dev="pkarwasz" type="add" due-to="Piotr P. Karwasz">Add org.apache.commons.io.file.PathUtils.getPath(String, String).</action>
6666
<action dev="ggregory" type="add" due-to="Gary Gregory">Add org.apache.commons.io.channels.ByteArraySeekableByteChannel.</action>
67+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterable.asIterable().</action>
6768
<!-- UPDATE -->
6869
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 85 to 88 #774, #783.</action>
6970
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] Bump commons-codec:commons-codec from 1.18.0 to 1.19.0.</action>

src/main/java/org/apache/commons/io/function/IOIterable.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 java.io.IOException;
21+
import java.io.UncheckedIOException;
2122
import java.util.Objects;
2223

2324
/**
@@ -28,6 +29,17 @@
2829
*/
2930
public interface IOIterable<T> {
3031

32+
/**
33+
* Creates an {@link Iterable} for this instance that throws {@link UncheckedIOException} instead of
34+
* {@link IOException}.
35+
*
36+
* @return an {@link UncheckedIOException} {@link Iterable}.
37+
* @since 2.21.0
38+
*/
39+
default Iterable<T> asIterable() {
40+
return new UncheckedIOIterable<>(this);
41+
}
42+
3143
/**
3244
* Like {@link Iterable#iterator()}.
3345
*

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ public void beforeEach() {
6161
iterable = fixture;
6262
}
6363

64+
@Test
65+
void testAsIterable() throws IOException {
66+
final AtomicInteger ref = new AtomicInteger();
67+
iterable.asIterable().iterator().forEachRemaining(e -> ref.incrementAndGet());
68+
assertEquals(2, ref.get());
69+
}
70+
6471
@Test
6572
void testForEach() throws IOException {
6673
final AtomicInteger ref = new AtomicInteger();

0 commit comments

Comments
 (0)