Add IOFunction.asFunction()
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -421,6 +421,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add IOSupplier.asSupplier().
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add IOFunction.asFunction().
+ </action>
<!-- UPDATE -->
<action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
Bump actions/cache from 2.1.6 to 3.0.5 #307, #337.

MODIFY
src/main/java/org/apache/commons/io/function/IOFunction.java
src/main/java/org/apache/commons/io/function/IOFunction.java
package org.apache.commons.io.function;
import java.io.IOException;
+import java.io.UncheckedIOException;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
*/
R apply(final T t) throws IOException;
+ /**
+ * Converts this instance to a {@link Function} that throws {@link UncheckedIOException} instead of
+ * {@link IOException}.
+ *
+ * @return an unchecked Function.
+ * @since 2.12.0
+ */
+ default Function<T, R> asFunction() {
+ return t -> Uncheck.apply(this, t);
+ }
+
/**
* Returns a composed {@link IOFunction} that first applies the {@code before}
* function to its input, and then applies this function to the result.

MODIFY
src/test/java/org/apache/commons/io/function/IOFunctionTest.java
src/test/java/org/apache/commons/io/function/IOFunctionTest.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -111,6 +113,12 @@ public void testApplyThrowsException() {
assertThrows(IOException.class, () -> throwException.apply(new ByteArrayInputStream(ArrayUtils.EMPTY_BYTE_ARRAY)));
}
+ @Test
+ public void testAsFunction() throws IOException {
+ assertThrows(UncheckedIOException.class, () -> Optional.of("a").map(TestConstants.THROWING_IO_FUNCTION.asFunction()).get());
+ assertEquals("a", Optional.of("a").map(IOFunction.identity().asFunction()).get());
+ }
+
@Test
public void testComposeFunction() throws IOException {
final Function<InputStream, Integer> alwaysSeven = is -> 7;

Add IOFunction.asFunction()