Add TriConsumer; provenance Apache Log4j 2.7, and enhanced with

andThen().
This commit is contained in:
Gary Gregory 2021-03-24 19:44:47 -04:00
parent 69c9593cc1
commit b4e95a4736
3 changed files with 143 additions and 0 deletions

View File

@ -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> <action issue="LANG-1646" type="fix" dev="aherbert" due-to="Alex Herbert">NumberUtils.createNumber to return requested floating point type for zero.</action>
<!-- ADD --> <!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action> <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>
<release version="3.12.0" date="2021-02-26" description="New features and bug fixes (Java 8)."> <release version="3.12.0" date="2021-02-26" description="New features and bug fixes (Java 8).">

View File

@ -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);
};
}
}

View File

@ -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());
}
}