001package Torello.Java.Function;
002
003import java.util.function.Function;
004
005/**
006 * Function-Pointer
007 * <SPAN CLASS=TJF>Input:</SPAN> {@code byte, T}
008 * <SPAN CLASS=TJF>Output:</SPAN> {@code R}.
009 * 
010 * <BR /><BR />
011 * <EMBED CLASS="external-html" DATA-FILE-ID="PRIMTFUNC">
012 * <EMBED CLASS="globalDefs" DATA-Primitive=byte>
013 * @param <T> The type of the second input-parameter.
014 * @param <R> The type of the function-output.
015 */
016@FunctionalInterface
017public interface ByteTFunction<T, R>
018{
019    /**
020     * Applies this function to the given arguments.
021     * @param b The byte (first) argument to the function.
022     * @param t The (second) argument to the function.
023     * @return The function result.  Result shall be of type {@code 'R'}
024     */
025    public R apply(byte b, T t);
026
027    /**
028     * Returns a composed function that first applies {@code 'this'} function to its input, and
029     * then applies the {@code 'after'} function to the result. If evaluation of either function
030     * throws an exception, it is relayed to the caller of the composed function.
031     * 
032     * @param <V> the output-type of the {@code 'after'} function, and also of the (returned)
033     * {@code 'composed'} function.
034     * 
035     * @param after The function to apply, after this function is applied.
036     * 
037     * @return a composed function that first applies {@code 'this'} function and then applies the
038     * {@code 'after'} function
039     * 
040     * @throws NullPointerException if null is passed to parameter {@code 'after'}.
041     */
042    public default <V> ByteTFunction<T, V> andThen(Function<? super R, ? extends V> after)
043    {
044        if (after == null) throw new NullPointerException
045            ("null has been passed to parameter 'after'");
046
047        return (byte b, T t) -> after.apply(this.apply(b, t));
048    }
049}