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/24 16:38:37 UTC
[commons-io] 14/21: Add IOTriConsumer
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
commit 0cdb6b3aef0b90df5c0d7a35da09a1e32b4a5e71
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 24 12:03:58 2022 -0400
Add IOTriConsumer
---
src/changes/changes.xml | 2 +-
.../java/org/apache/commons/io/UncheckedIO.java | 46 ++++++++++++-
.../org/apache/commons/io/function/Constants.java | 8 ++-
.../apache/commons/io/function/IOTriConsumer.java | 76 ++++++++++++++++++++++
.../commons/io/function/IOTriConsumerTest.java | 54 +++++++++++++++
5 files changed, 181 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4892f35b..cb0b734f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -357,7 +357,7 @@ The <action> type attribute can be add,update,fix,remove.
Add IOExceptionList.checkEmpty(List, Object).
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
- Add IOBiConsumer.
+ Add IOBiConsumer, IOTriConsumer.
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add and reuse IOConsumer forAll(*), forEach(*), and forEachIndexed(*).
diff --git a/src/main/java/org/apache/commons/io/UncheckedIO.java b/src/main/java/org/apache/commons/io/UncheckedIO.java
index fd766484..4c3e5b74 100644
--- a/src/main/java/org/apache/commons/io/UncheckedIO.java
+++ b/src/main/java/org/apache/commons/io/UncheckedIO.java
@@ -20,6 +20,7 @@ package org.apache.commons.io;
import java.io.IOException;
import java.io.UncheckedIOException;
+import org.apache.commons.io.function.IOBiConsumer;
import org.apache.commons.io.function.IOBiFunction;
import org.apache.commons.io.function.IOConsumer;
import org.apache.commons.io.function.IOFunction;
@@ -27,6 +28,7 @@ import org.apache.commons.io.function.IOPredicate;
import org.apache.commons.io.function.IOQuadFunction;
import org.apache.commons.io.function.IORunnable;
import org.apache.commons.io.function.IOSupplier;
+import org.apache.commons.io.function.IOTriConsumer;
import org.apache.commons.io.function.IOTriFunction;
/**
@@ -36,11 +38,29 @@ import org.apache.commons.io.function.IOTriFunction;
*/
public class UncheckedIO {
+ /**
+ * Accepts an IO consumer with the given arguments.
+ *
+ * @param <T> the first input type.
+ * @param <U> the second input type.
+ * @param t the first input argument.
+ * @param u the second input argument.
+ * @param consumer Consumes the value.
+ * @throws UncheckedIOException if an I/O error occurs.
+ */
+ public static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
+ try {
+ consumer.accept(t, u);
+ } catch (final IOException e) {
+ throw wrap(e);
+ }
+ }
+
/**
* Accepts an IO consumer with the given argument.
*
- * @param <T> the return type of the operations.
- * @param t the consumer argument.
+ * @param <T> the input type.
+ * @param t the input argument.
* @param consumer Consumes the value.
* @throws UncheckedIOException if an I/O error occurs.
*/
@@ -53,7 +73,27 @@ public class UncheckedIO {
}
/**
- * Applies an IO bi-function with the given arguments.
+ * Accepts an IO consumer with the given arguments.
+ *
+ * @param <T> the first input type.
+ * @param <U> the second input type.
+ * @param <V> the third input type.
+ * @param t the first input argument.
+ * @param u the second input argument.
+ * @param v the third input argument.
+ * @param consumer Consumes the value.
+ * @throws UncheckedIOException if an I/O error occurs.
+ */
+ public static <T, U, V> void accept(final IOTriConsumer<T, U, V> consumer, final T t, final U u, final V v) {
+ try {
+ consumer.accept(t, u, v);
+ } catch (final IOException e) {
+ throw wrap(e);
+ }
+ }
+
+ /**
+ * Applies an IO function with the given arguments.
*
* @param function the function.
* @param <T> the first function argument type.
diff --git a/src/main/java/org/apache/commons/io/function/Constants.java b/src/main/java/org/apache/commons/io/function/Constants.java
index 55386e58..a094710e 100644
--- a/src/main/java/org/apache/commons/io/function/Constants.java
+++ b/src/main/java/org/apache/commons/io/function/Constants.java
@@ -26,7 +26,7 @@ class Constants {
* No-op singleton.
*/
@SuppressWarnings("rawtypes")
- static final IOBiConsumer IO_BI_CONSUMER = (t, u) -> {/* NOOP */};
+ static final IOBiConsumer IO_BI_CONSUMER = (t, u) -> {/* No-op */};
/**
* No-op singleton.
@@ -50,6 +50,12 @@ class Constants {
*/
static final IOPredicate<Object> IO_PREDICATE_TRUE = t -> true;
+ /**
+ * No-op singleton.
+ */
+ @SuppressWarnings("rawtypes")
+ static final IOTriConsumer IO_TRI_CONSUMER = (t, u, v) -> {/* No-op */};
+
private Constants() {
// We don't want instances
}
diff --git a/src/main/java/org/apache/commons/io/function/IOTriConsumer.java b/src/main/java/org/apache/commons/io/function/IOTriConsumer.java
new file mode 100644
index 00000000..a2d6b502
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/function/IOTriConsumer.java
@@ -0,0 +1,76 @@
+/*
+ * 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.Objects;
+import java.util.function.BiConsumer;
+
+/**
+ * Like {@link BiConsumer} but throws {@link IOException}.
+ *
+ * @param <T> the type of the first argument to the operation
+ * @param <U> the type of the second argument to the operation
+ * @param <V> the type of the third argument to the operation
+ *
+ * @see BiConsumer
+ * @since 2.12.0
+ */
+@FunctionalInterface
+public interface IOTriConsumer<T, U, V> {
+
+ /**
+ * Returns The no-op singleton.
+ *
+ * @param <T> the type of the first argument to the operation
+ * @param <U> the type of the second argument to the operation
+ * @param <V> the type of the third argument to the operation
+ * @return The NOOP singleton.
+ */
+ static <T, U, V> IOTriConsumer<T, U, V> noop() {
+ return Constants.IO_TRI_CONSUMER;
+ }
+
+ /**
+ * Performs this operation on the given arguments.
+ *
+ * @param t the first input argument
+ * @param u the second input argument
+ * @param v the second third argument
+ * @throws IOException if an I/O error occurs.
+ */
+ void accept(T t, U u, V v) throws IOException;
+
+ /**
+ * Returns a composed {@link IOTriConsumer} that performs, in sequence, this operation followed by the {@code after}
+ * operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation.
+ * If performing this operation throws an exception, the {@code after} operation will not be performed.
+ *
+ * @param after the operation to perform after this operation
+ * @return a composed {@link IOTriConsumer} that performs in sequence this operation followed by the {@code after}
+ * operation
+ * @throws NullPointerException if {@code after} is null
+ */
+ default IOTriConsumer<T, U, V> andThen(final IOTriConsumer<? super T, ? super U, ? super V> after) {
+ Objects.requireNonNull(after);
+ return (t, u, v) -> {
+ accept(t, u, v);
+ after.accept(t, u, v);
+ };
+ }
+}
diff --git a/src/test/java/org/apache/commons/io/function/IOTriConsumerTest.java b/src/test/java/org/apache/commons/io/function/IOTriConsumerTest.java
new file mode 100644
index 00000000..48db770b
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/function/IOTriConsumerTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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 java.io.IOException;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link IOTriConsumer}.
+ */
+public class IOTriConsumerTest {
+
+ @Test
+ public void testAccept() throws IOException {
+ final AtomicReference<String> ref = new AtomicReference<>();
+ final IOTriConsumer<String, Integer, Character> consumer = (s, i, b) -> ref.set(s + i + b);
+ consumer.accept("A", 1, 'b');
+ assertEquals("A1b", ref.get());
+ }
+
+ @Test
+ public void testAndThen() throws IOException {
+ final AtomicReference<String> ref = new AtomicReference<>();
+ final IOTriConsumer<String, Integer, Character> consumer1 = (s, i, b) -> ref.set(s + i + b);
+ final IOTriConsumer<String, Integer, Character> consumer2 = (s, i, b) -> ref.set(ref.get() + b + i + s);
+ consumer1.andThen(consumer2).accept("B", 2, 'b');
+ assertEquals("B2bb2B", ref.get());
+ }
+
+ @Test
+ public void testNoop() throws IOException {
+ IOTriConsumer.noop().accept(null, null, null);
+ }
+
+}