Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TriConsumer #581

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <T> The first argument type.
* @param <U> The second argument type.
* @param <V> The third argument type.
*/
@FunctionalInterface
public interface TriConsumer<T, U, V> {
/**
* 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<T, U, V> andThen(TriConsumer<? super T, ? super U, ? super V> after) {
Objects.requireNonNull(after);

return (x, y, z) -> {
accept(x, y, z);
after.accept(x, y, z);
};
}
}
Original file line number Diff line number Diff line change
@@ -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;
mtdowling marked this conversation as resolved.
Show resolved Hide resolved

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<String, String, String> first = (a, b, c) -> {
result.append(a).append(b).append(c);
};

TriConsumer<String, String, String> second = (d, e, f) -> {
result.append(d.toUpperCase(Locale.ENGLISH))
.append(e.toUpperCase(Locale.ENGLISH))
.append(f.toUpperCase(Locale.ENGLISH));
};

TriConsumer<String, String, String> composed = first.andThen(second);

composed.accept("a", "b", "c");

assertThat(result.toString(), equalTo("abcABC"));
}
}