|
| 1 | +package com.baeldung.trifunction; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | +import java.util.function.Function; |
| 5 | + |
| 6 | +/** |
| 7 | + * Represents a function that accepts three arguments and produces a result. |
| 8 | + * This is the three-arity specialization of {@link Function}. |
| 9 | + * |
| 10 | + * <p>This is a functional interface |
| 11 | + * whose functional method is {@link #apply(Object, Object, Object)}. |
| 12 | + * |
| 13 | + * @param <T> the type of the first argument to the function |
| 14 | + * @param <U> the type of the second argument to the function |
| 15 | + * @param <V> the type of the third argument to the function |
| 16 | + * @param <R> the type of the result of the function |
| 17 | + * |
| 18 | + * @see Function |
| 19 | + */ |
| 20 | +@FunctionalInterface |
| 21 | +public interface TriFunction<T, U, V, R> { |
| 22 | + |
| 23 | + /** |
| 24 | + * Applies this function to the given arguments. |
| 25 | + * |
| 26 | + * @param t the first function argument |
| 27 | + * @param u the second function argument |
| 28 | + * @param v the third function argument |
| 29 | + * @return the function result |
| 30 | + */ |
| 31 | + R apply(T t, U u, V v); |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns a composed function that first applies this function to |
| 35 | + * its input, and then applies the {@code after} function to the result. |
| 36 | + * If evaluation of either function throws an exception, it is relayed to |
| 37 | + * the caller of the composed function. |
| 38 | + * |
| 39 | + * @param <K> the type of output of the {@code after} function, and of the |
| 40 | + * composed function |
| 41 | + * @param after the function to apply after this function is applied |
| 42 | + * @return a composed function that first applies this function and then |
| 43 | + * applies the {@code after} function |
| 44 | + * @throws NullPointerException if after is null |
| 45 | + */ |
| 46 | + default <K> TriFunction<T, U, V, K> andThen(Function<? super R, ? extends K> after) { |
| 47 | + Objects.requireNonNull(after); |
| 48 | + return (T t, U u, V v) -> after.apply(apply(t, u, v)); |
| 49 | + } |
| 50 | +} |
0 commit comments