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 2022/07/27 17:59:28 UTC
[commons-io] branch master updated: Add IOSupplier.asSupplier().
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 17b7581a Add IOSupplier.asSupplier().
17b7581a is described below
commit 17b7581a70036bb955f98f8eb06c48ec20fba6d6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jul 27 09:53:07 2022 -0400
Add IOSupplier.asSupplier().
---
src/changes/changes.xml | 3 +
.../org/apache/commons/io/function/IOSupplier.java | 11 ++++
.../apache/commons/io/function/IOSupplierTest.java | 67 ++++++++++++++++++++++
.../org/apache/commons/io/function/TestUtils.java} | 29 +++++-----
.../apache/commons/io/function/UncheckTest.java | 47 +++++++--------
5 files changed, 114 insertions(+), 43 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 87445201..09fdebbb 100644
--- a/src/changes/changes.xml
+++ b/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.
diff --git a/src/main/java/org/apache/commons/io/function/IOSupplier.java b/src/main/java/org/apache/commons/io/function/IOSupplier.java
index e5cd5e0d..a9a656a2 100644
--- a/src/main/java/org/apache/commons/io/function/IOSupplier.java
+++ b/src/main/java/org/apache/commons/io/function/IOSupplier.java
@@ -18,6 +18,7 @@
package org.apache.commons.io.function;
import java.io.IOException;
+import java.io.UncheckedIOException;
import java.util.function.Supplier;
/**
@@ -29,6 +30,16 @@ 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.
*
diff --git a/src/test/java/org/apache/commons/io/function/IOSupplierTest.java b/src/test/java/org/apache/commons/io/function/IOSupplierTest.java
new file mode 100644
index 00000000..d3c91f24
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/function/IOSupplierTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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());
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/io/function/IOSupplier.java b/src/test/java/org/apache/commons/io/function/TestUtils.java
similarity index 62%
copy from src/main/java/org/apache/commons/io/function/IOSupplier.java
copy to src/test/java/org/apache/commons/io/function/TestUtils.java
index e5cd5e0d..47889bee 100644
--- a/src/main/java/org/apache/commons/io/function/IOSupplier.java
+++ b/src/test/java/org/apache/commons/io/function/TestUtils.java
@@ -18,22 +18,19 @@
package org.apache.commons.io.function;
import java.io.IOException;
-import java.util.function.Supplier;
+import java.util.concurrent.atomic.AtomicReference;
-/**
- * Like {@link Supplier} but throws {@link IOException}.
- *
- * @param <T> the return type of the operations.
- * @since 2.7
- */
-@FunctionalInterface
-public interface IOSupplier<T> {
+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);
+ }
- /**
- * Gets a result.
- *
- * @return a result
- * @throws IOException if an I/O error occurs.
- */
- T get() throws IOException;
}
diff --git a/src/test/java/org/apache/commons/io/function/UncheckTest.java b/src/test/java/org/apache/commons/io/function/UncheckTest.java
index a379208a..ab054416 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -36,6 +36,7 @@ public class UncheckTest {
private AtomicReference<String> ref1;
private AtomicReference<String> ref2;
private AtomicReference<String> ref3;
+
private AtomicReference<String> ref4;
@BeforeEach
@@ -53,8 +54,8 @@ public class UncheckTest {
}, 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 class UncheckTest {
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 class UncheckTest {
}, 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 class UncheckTest {
}, 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 class UncheckTest {
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 class UncheckTest {
}, 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 class UncheckTest {
}, 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 class UncheckTest {
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 class UncheckTest {
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 class UncheckTest {
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());
}