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 2021/11/06 18:34:26 UTC
[commons-io] branch master updated: Add IOBiConsumer.
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 2c9a10d Add IOBiConsumer.
2c9a10d is described below
commit 2c9a10d864d2208b8bd23e5fb5a1c520c1520be9
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Nov 6 14:34:24 2021 -0400
Add IOBiConsumer.
---
src/changes/changes.xml | 3 ++
.../apache/commons/io/function/IOBiConsumer.java | 63 ++++++++++++++++++++++
.../commons/io/function/IOBiConsumerTest.java | 48 +++++++++++++++++
3 files changed, 114 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9e6ad1d..5c989d6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -284,6 +284,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add IOExceptionList.checkEmpty(List, Object).
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add IOBiConsumer.
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Update FileEntry to use FileTime instead of long for file time stamps.
diff --git a/src/main/java/org/apache/commons/io/function/IOBiConsumer.java b/src/main/java/org/apache/commons/io/function/IOBiConsumer.java
new file mode 100644
index 0000000..eb0cc07
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/function/IOBiConsumer.java
@@ -0,0 +1,63 @@
+/*
+ * 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
+ *
+ * @see BiConsumer
+ * @since 1.8
+ */
+@FunctionalInterface
+public interface IOBiConsumer<T, U> {
+
+ /**
+ * Performs this operation on the given arguments.
+ *
+ * @param t the first input argument
+ * @param u the second input argument
+ * @throws IOException if an I/O error occurs.
+ */
+ void accept(T t, U u) throws IOException;
+
+ /**
+ * Returns a composed {@code BiConsumer} 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 {@code BiConsumer} that performs in sequence this operation followed by the {@code after}
+ * operation
+ * @throws NullPointerException if {@code after} is null
+ */
+ default IOBiConsumer<T, U> andThen(IOBiConsumer<? super T, ? super U> after) {
+ Objects.requireNonNull(after);
+
+ return (l, r) -> {
+ accept(l, r);
+ after.accept(l, r);
+ };
+ }
+}
diff --git a/src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java b/src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java
new file mode 100644
index 0000000..92e62e1
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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 IOBiConsumer}.
+ */
+public class IOBiConsumerTest {
+
+ @Test
+ public void testAccept() throws IOException {
+ final AtomicReference<String> ref = new AtomicReference<>();
+ final IOBiConsumer<String, Integer> biConsumer = (s, i) -> ref.set(s + i);
+ biConsumer.accept("A", 1);
+ assertEquals("A1", ref.get());
+ }
+
+ @Test
+ public void testAndThen() throws IOException {
+ final AtomicReference<String> ref = new AtomicReference<>();
+ final IOBiConsumer<String, Integer> biConsumer1 = (s, i) -> ref.set(s + i);
+ final IOBiConsumer<String, Integer> biConsumer2 = (s, i) -> ref.set(ref.get() + i + s);
+ biConsumer1.andThen(biConsumer2).accept("B", 2);
+ assertEquals("B22B", ref.get());
+ }
+}