Add IOSupplier.asSupplier().
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -418,6 +418,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add IOUtils.consume(Reader).
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add IOSupplier.asSupplier().
+ </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/IOSupplier.java
src/main/java/org/apache/commons/io/function/IOSupplier.java
package org.apache.commons.io.function;
import java.io.IOException;
+import java.io.UncheckedIOException;
import java.util.function.Supplier;
/**
@FunctionalInterface
public interface IOSupplier<T> {
+ /**
+ * Converts this instance to a Supplier that throws {@link UncheckedIOException} instead of {@link IOException}.
+ *
+ * @return an unchecked Predicate.
+ * @since 2.12.0
+ */
+ default Supplier<T> asSupplier() {
+ return () -> Uncheck.get(this);
+ }
+
/**
* Gets a result.
*

ADD
src/test/java/org/apache/commons/io/function/IOSupplierTest.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.function;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link IOSupplier}.
+ */
+public class IOSupplierTest {
+
+ private AtomicReference<String> ref1;
+
+ private String getThrows(IOSupplier<String> supplier) throws IOException {
+ return supplier.get();
+ }
+
+ private String getThrowsNone(IOSupplier<String> supplier) {
+ return supplier.asSupplier().get();
+ }
+
+ @BeforeEach
+ public void initEach() {
+ ref1 = new AtomicReference<>();
+ }
+
+ @Test
+ public void testAsSupplier() {
+ assertThrows(UncheckedIOException.class, () -> TestConstants.THROWING_IO_SUPPLIER.asSupplier().get());
+ assertEquals("new1", getThrowsNone(() -> TestUtils.compareAndSetThrows(ref1, "new1")));
+ assertEquals("new1", ref1.get());
+ }
+
+ @Test
+ public void testGet() throws IOException {
+ assertThrows(IOException.class, () -> TestConstants.THROWING_IO_SUPPLIER.get());
+ assertThrows(IOException.class, () -> {
+ throw new IOException();
+ });
+ assertEquals("new1", getThrows(() -> TestUtils.compareAndSetThrows(ref1, "new1")));
+ assertEquals("new1", ref1.get());
+ }
+
+}

ADD
src/test/java/org/apache/commons/io/function/TestUtils.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.function;
+
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicReference;
+
+class TestUtils {
+
+ static <T> T compareAndSetThrows(final AtomicReference<T> ref, final T expected, final T update) throws IOException {
+ if (!ref.compareAndSet(expected, update)) {
+ throw new IOException("Unexpected");
+ }
+ return ref.get(); // same as update
+ }
+
+ static <T> T compareAndSetThrows(final AtomicReference<T> ref, final T update) throws IOException {
+ return compareAndSetThrows(ref, null, update);
+ }
+
+}

MODIFY
src/test/java/org/apache/commons/io/function/UncheckTest.java
src/test/java/org/apache/commons/io/function/UncheckTest.java
private AtomicReference<String> ref1;
private AtomicReference<String> ref2;
private AtomicReference<String> ref3;
+
private AtomicReference<String> ref4;
@BeforeEach
@@ -53,8 +54,8 @@ public void testAcceptIOBiConsumerOfTUTU() {
}, null, null));
assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestConstants.THROWING_IO_BI_CONSUMER, null, null));
Uncheck.accept((t, u) -> {
- compareAndSet(ref1, t);
- compareAndSet(ref2, u);
+ TestUtils.compareAndSetThrows(ref1, t);
+ TestUtils.compareAndSetThrows(ref2, u);
}, "new1", "new2");
assertEquals("new1", ref1.get());
assertEquals("new2", ref2.get());
@@ -66,18 +67,10 @@ public void testAcceptIOConsumerOfTT() {
throw new IOException();
}, null));
assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestConstants.THROWING_IO_CONSUMER, null));
- Uncheck.accept(t -> compareAndSet(ref1, t), "new1");
+ Uncheck.accept(t -> TestUtils.compareAndSetThrows(ref1, t), "new1");
assertEquals("new1", ref1.get());
}
- private <T> T compareAndSet(final AtomicReference<T> ref, final T t) throws IOException {
- if (!ref.compareAndSet(null, t)) {
- // Tell the compiler this method throws a checked exception.
- throw new IOException("Unexpected1");
- }
- return ref.get(); // same as t
- }
-
@Test
public void testAcceptIOTriConsumerOfTUVTUV() {
assertThrows(UncheckedIOException.class, () -> Uncheck.accept((t, u, v) -> {
@@ -85,9 +78,9 @@ public void testAcceptIOTriConsumerOfTUVTUV() {
}, null, null, null));
assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestConstants.THROWING_IO_TRI_CONSUMER, null, null, null));
Uncheck.accept((t, u, v) -> {
- compareAndSet(ref1, t);
- compareAndSet(ref2, u);
- compareAndSet(ref3, v);
+ TestUtils.compareAndSetThrows(ref1, t);
+ TestUtils.compareAndSetThrows(ref2, u);
+ TestUtils.compareAndSetThrows(ref3, v);
}, "new1", "new2", "new3");
assertEquals("new1", ref1.get());
assertEquals("new2", ref2.get());
@@ -101,8 +94,8 @@ public void testApplyIOBiFunctionOfTURTU() {
}, null, null));
assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_BI_FUNCTION, null, null));
assertEquals("new0", Uncheck.apply((t, u) -> {
- compareAndSet(ref1, t);
- compareAndSet(ref2, u);
+ TestUtils.compareAndSetThrows(ref1, t);
+ TestUtils.compareAndSetThrows(ref2, u);
return "new0";
}, "new1", "new2"));
assertEquals("new1", ref1.get());
@@ -115,7 +108,7 @@ public void testApplyIOFunctionOfTRT() {
throw new IOException();
}, null));
assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_FUNCTION, null));
- Uncheck.apply(t -> compareAndSet(ref1, t), "new1");
+ Uncheck.apply(t -> TestUtils.compareAndSetThrows(ref1, t), "new1");
assertEquals("new1", ref1.get());
}
@@ -126,10 +119,10 @@ public void testApplyIOQuadFunctionOfTUVWRTUVW() {
}, null, null, null, null));
assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_QUAD_FUNCTION, null, null, null, null));
assertEquals("new0", Uncheck.apply((t, u, v, w) -> {
- compareAndSet(ref1, t);
- compareAndSet(ref2, u);
- compareAndSet(ref3, v);
- compareAndSet(ref4, w);
+ TestUtils.compareAndSetThrows(ref1, t);
+ TestUtils.compareAndSetThrows(ref2, u);
+ TestUtils.compareAndSetThrows(ref3, v);
+ TestUtils.compareAndSetThrows(ref4, w);
return "new0";
}, "new1", "new2", "new3", "new4"));
assertEquals("new1", ref1.get());
@@ -145,9 +138,9 @@ public void testApplyIOTriFunctionOfTUVRTUV() {
}, null, null, null));
assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_TRI_FUNCTION, null, null, null));
assertEquals("new0", Uncheck.apply((t, u, v) -> {
- compareAndSet(ref1, t);
- compareAndSet(ref2, u);
- compareAndSet(ref3, v);
+ TestUtils.compareAndSetThrows(ref1, t);
+ TestUtils.compareAndSetThrows(ref2, u);
+ TestUtils.compareAndSetThrows(ref3, v);
return "new0";
}, "new1", "new2", "new3"));
assertEquals("new1", ref1.get());
@@ -161,7 +154,7 @@ public void testGet() {
throw new IOException();
}));
assertThrows(UncheckedIOException.class, () -> Uncheck.get(TestConstants.THROWING_IO_SUPPLIER));
- assertEquals("new1", Uncheck.get(() -> compareAndSet(ref1, "new1")));
+ assertEquals("new1", Uncheck.get(() -> TestUtils.compareAndSetThrows(ref1, "new1")));
assertEquals("new1", ref1.get());
}
@@ -171,7 +164,7 @@ public void testRun() {
throw new IOException();
}));
assertThrows(UncheckedIOException.class, () -> Uncheck.run(TestConstants.THROWING_IO_RUNNABLE));
- Uncheck.run(() -> compareAndSet(ref1, "new1"));
+ Uncheck.run(() -> TestUtils.compareAndSetThrows(ref1, "new1"));
assertEquals("new1", ref1.get());
}
@@ -181,7 +174,7 @@ public void testTest() {
throw new IOException();
}, null));
assertThrows(UncheckedIOException.class, () -> Uncheck.test(TestConstants.THROWING_IO_PREDICATE, null));
- assertTrue(Uncheck.test(t -> compareAndSet(ref1, t).equals(t), "new1"));
+ assertTrue(Uncheck.test(t -> TestUtils.compareAndSetThrows(ref1, t).equals(t), "new1"));
assertEquals("new1", ref1.get());
}

Add IOSupplier.asSupplier().