diff --git a/smithy-utils/src/main/java/software/amazon/smithy/utils/TriConsumer.java b/smithy-utils/src/main/java/software/amazon/smithy/utils/TriConsumer.java new file mode 100644 index 00000000000..02aa8e220b7 --- /dev/null +++ b/smithy-utils/src/main/java/software/amazon/smithy/utils/TriConsumer.java @@ -0,0 +1,55 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file 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 software.amazon.smithy.utils; + +import java.util.Objects; +import java.util.function.BiConsumer; + +/** + * Similar to {@link BiConsumer}, but accepts three arguments. + * + * @param The first argument type. + * @param The second argument type. + * @param The third argument type. + */ +@FunctionalInterface +public interface TriConsumer { + /** + * Performs the operation on the given inputs. + * + * @param t is the first argument + * @param u is the second argument + * @param v is the third argument + */ + void accept(T t, U u, V v); + + /** + * Returns a composed {@link TriConsumer} that performs, in sequence, this + * operation followed by the {@code after} operation. + * + * @param after the operation to perform after this operation + * @return a composed {@link TriConsumer} + * @throws NullPointerException if {@code after} is null + */ + default TriConsumer andThen(TriConsumer after) { + Objects.requireNonNull(after); + + return (x, y, z) -> { + accept(x, y, z); + after.accept(x, y, z); + }; + } +} diff --git a/smithy-utils/src/test/java/software/amazon/smithy/utils/TriConsumerTest.java b/smithy-utils/src/test/java/software/amazon/smithy/utils/TriConsumerTest.java new file mode 100644 index 00000000000..b5ae858ce08 --- /dev/null +++ b/smithy-utils/src/test/java/software/amazon/smithy/utils/TriConsumerTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file 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 software.amazon.smithy.utils; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import java.util.Locale; +import org.junit.jupiter.api.Test; + +public class TriConsumerTest { + @Test + public void composes() { + StringBuilder result = new StringBuilder(); + + TriConsumer first = (a, b, c) -> { + result.append(a).append(b).append(c); + }; + + TriConsumer second = (d, e, f) -> { + result.append(d.toUpperCase(Locale.ENGLISH)) + .append(e.toUpperCase(Locale.ENGLISH)) + .append(f.toUpperCase(Locale.ENGLISH)); + }; + + TriConsumer composed = first.andThen(second); + + composed.accept("a", "b", "c"); + + assertThat(result.toString(), equalTo("abcABC")); + } +}