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-lang.git
The following commit(s) were added to refs/heads/master by this push:
new b4e95a4 Add TriConsumer; provenance Apache Log4j 2.7, and enhanced
with andThen().
b4e95a4 is described below
commit b4e95a473604df15df7e2a121fba45ad617ef40f
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Mar 24 19:44:47 2021 -0400
Add TriConsumer; provenance Apache Log4j 2.7, and enhanced with
andThen().
---
src/changes/changes.xml | 1 +
.../apache/commons/lang3/function/TriConsumer.java | 71 ++++++++++++++++++++++
.../commons/lang3/function/TriConsumerTest.java | 71 ++++++++++++++++++++++
3 files changed, 143 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 017a983..bc85850 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1646" type="fix" dev="aherbert" due-to="Alex
Herbert">NumberUtils.createNumber to return requested floating point type for
zero.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add EnumUtils.getEnumSystemProperty(...).</action>
+ <action type="add" dev="ggregory" due-to="Gary
Gregory">Add TriConsumer.</action>
</release>
<release version="3.12.0" date="2021-02-26" description="New features and
bug fixes (Java 8).">
diff --git a/src/main/java/org/apache/commons/lang3/function/TriConsumer.java
b/src/main/java/org/apache/commons/lang3/function/TriConsumer.java
new file mode 100644
index 0000000..6bd7c81
--- /dev/null
+++ b/src/main/java/org/apache/commons/lang3/function/TriConsumer.java
@@ -0,0 +1,71 @@
+/*
+ * 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.lang3.function;
+
+import java.util.Objects;
+import java.util.function.Consumer;
+
+/**
+ * Represents an operation that accepts three input arguments and returns no
result. This is the three-arity
+ * specialization of {@link Consumer}. Unlike most other functional
interfaces, {@code TriConsumer} is expected to
+ * operate via side-effects.
+ *
+ * <p>
+ * This is a {@link FunctionalInterface} whose functional method is {@link
#accept(Object, Object, Object)}.
+ * </p>
+ * <p>
+ * Provenance: Apache Log4j 2.7
+ * </p>
+ *
+ * @param <T> type of the first argument
+ * @param <U> type of the second argument
+ * @param <V> type of the third argument
+ * @since 3.13.0
+ */
+@FunctionalInterface
+public interface TriConsumer<T, U, V> {
+
+ /**
+ * Performs the operation given the specified arguments.
+ *
+ * @param k the first input argument
+ * @param v the second input argument
+ * @param s the third input argument
+ */
+ void accept(T k, U v, V s);
+
+ /**
+ * Returns a composed {@code TriConsumer} 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 TriConsumer} that performs in sequence this
operation followed by the {@code after}
+ * operation.
+ * @throws NullPointerException if {@code after} is null.
+ */
+ default TriConsumer<T, U, V> andThen(final TriConsumer<? 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/lang3/function/TriConsumerTest.java
b/src/test/java/org/apache/commons/lang3/function/TriConsumerTest.java
new file mode 100644
index 0000000..bb178ac
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/function/TriConsumerTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.lang3.function;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@linkTriConsumer}.
+ */
+public class TriConsumerTest {
+
+ @Test
+ public void testAccept() throws Throwable {
+ final AtomicReference<Character> ref1 = new AtomicReference<>();
+ final AtomicReference<Short> ref2 = new AtomicReference<>();
+ final AtomicReference<String> ref3 = new AtomicReference<>();
+ final TriConsumer<AtomicReference<Character>, AtomicReference<Short>,
AtomicReference<String>> tri = (t, u,
+ v) -> {
+ ref1.set(Character.valueOf('a'));
+ ref2.set(Short.valueOf((short) 1));
+ ref3.set("z");
+ };
+ tri.accept(ref1, ref2, ref3);
+ assertEquals(Character.valueOf('a'), ref1.get());
+ assertEquals(Short.valueOf((short) 1), ref2.get());
+ assertEquals("z", ref3.get());
+ }
+
+ @Test
+ public void testAndThen() throws Throwable {
+ final AtomicReference<Character> ref1 = new AtomicReference<>();
+ final AtomicReference<Short> ref2 = new AtomicReference<>();
+ final AtomicReference<String> ref3 = new AtomicReference<>();
+ final TriConsumer<AtomicReference<Character>, AtomicReference<Short>,
AtomicReference<String>> tri = (t, u,
+ v) -> {
+ ref1.set(Character.valueOf('a'));
+ ref2.set(Short.valueOf((short) 1));
+ ref3.set("z");
+ };
+ final TriConsumer<AtomicReference<Character>, AtomicReference<Short>,
AtomicReference<String>> triAfter = (t, u,
+ v) -> {
+ ref1.set(Character.valueOf('b'));
+ ref2.set(Short.valueOf((short) 2));
+ ref3.set("zz");
+ };
+ tri.andThen(triAfter).accept(ref1, ref2, ref3);
+ assertEquals(Character.valueOf('b'), ref1.get());
+ assertEquals(Short.valueOf((short) 2), ref2.get());
+ assertEquals("zz", ref3.get());
+ }
+
+}