Java tutorial
/* * Copyright (c) 2016 Gridtec. 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. * 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 at.gridtec.lambda4j.function.bi; import at.gridtec.lambda4j.Lambda; import at.gridtec.lambda4j.consumer.bi.BiConsumer2; import at.gridtec.lambda4j.function.Function2; import org.apache.commons.lang3.tuple.Pair; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; /** * Represents an operation that accepts two input arguments and produces a * result. * <p> * This is a {@link FunctionalInterface} whose functional method is {@link #apply(Object, Object)}. * * @param <T> The type of the first argument to the function * @param <U> The type of the second argument to the function * @param <R> The type of return value from the function * @apiNote This is a JDK lambda. * @see BiFunction2 */ @SuppressWarnings("unused") @FunctionalInterface public interface BiFunction2<T, U, R> extends Lambda, BiFunction<T, U, R> { /** * Constructs a {@link BiFunction2} based on a lambda expression or a method reference. Thereby the given lambda * expression or method reference is returned on an as-is basis to implicitly transform it to the desired type. With * this method, it is possible to ensure that correct type is used from lambda expression or method reference. * * @param <T> The type of the first argument to the function * @param <U> The type of the second argument to the function * @param <R> The type of return value from the function * @param expression A lambda expression or (typically) a method reference, e.g. {@code this::method} * @return A {@code BiFunction2} from given lambda expression or method reference. * @implNote This implementation allows the given argument to be {@code null}, but only if {@code null} given, * {@code null} will be returned. * @see <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax">Lambda * Expression</a> * @see <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">Method Reference</a> */ static <T, U, R> BiFunction2<T, U, R> of(@Nullable final BiFunction2<T, U, R> expression) { return expression; } /** * Lifts a partial {@link BiFunction} into a total {@link BiFunction2} that returns an {@link Optional} result. * * @param <T> The type of the first argument to the function * @param <U> The type of the second argument to the function * @param <R> The type of return value from the function * @param partial A function that is only defined for some values in its domain * @return A partial {@code BiFunction} lifted into a total {@code BiFunction2} that returns an {@code Optional} * result. * @throws NullPointerException If given argument is {@code null} */ @Nonnull static <T, U, R> BiFunction2<T, U, Optional<R>> lift( @Nonnull final BiFunction<? super T, ? super U, ? extends R> partial) { Objects.requireNonNull(partial); return (t, u) -> Optional.ofNullable(partial.apply(t, u)); } /** * Calls the given {@link BiFunction} with the given arguments and returns its result. * * @param <T> The type of the first argument to the function * @param <U> The type of the second argument to the function * @param <R> The type of return value from the function * @param function The function to be called * @param t The first argument to the function * @param u The second argument to the function * @return The result from the given {@code BiFunction2}. * @throws NullPointerException If given argument is {@code null} */ static <T, U, R> R call(@Nonnull final BiFunction<? super T, ? super U, ? extends R> function, T t, U u) { Objects.requireNonNull(function); return function.apply(t, u); } /** * Creates a {@link BiFunction2} which uses the {@code first} parameter of this one as argument for the given {@link * Function}. * * @param <T> The type of the first argument to the function * @param <U> The type of the second argument to the function * @param <R> The type of return value from the function * @param function The function which accepts the {@code first} parameter of this one * @return Creates a {@code BiFunction2} which uses the {@code first} parameter of this one as argument for the * given {@code Function}. * @throws NullPointerException If given argument is {@code null} */ @Nonnull static <T, U, R> BiFunction2<T, U, R> onlyFirst(@Nonnull final Function<? super T, ? extends R> function) { Objects.requireNonNull(function); return (t, u) -> function.apply(t); } /** * Creates a {@link BiFunction2} which uses the {@code second} parameter of this one as argument for the given * {@link Function}. * * @param <T> The type of the first argument to the function * @param <U> The type of the second argument to the function * @param <R> The type of return value from the function * @param function The function which accepts the {@code second} parameter of this one * @return Creates a {@code BiFunction2} which uses the {@code second} parameter of this one as argument for the * given {@code Function}. * @throws NullPointerException If given argument is {@code null} */ @Nonnull static <T, U, R> BiFunction2<T, U, R> onlySecond(@Nonnull final Function<? super U, ? extends R> function) { Objects.requireNonNull(function); return (t, u) -> function.apply(u); } /** * Creates a {@link BiFunction2} which always returns a given value. * * @param <T> The type of the first argument to the function * @param <U> The type of the second argument to the function * @param <R> The type of return value from the function * @param ret The return value for the constant * @return A {@code BiFunction2} which always returns a given value. */ @Nonnull static <T, U, R> BiFunction2<T, U, R> constant(R ret) { return (t, u) -> ret; } /** * Applies this function to the given arguments. * * @param t The first argument to the function * @param u The second argument to the function * @return The return value from the function, which is its result. */ R apply(T t, U u); /** * Applies this function to the given tuple. * * @param tuple The tuple to be applied to the function * @return The return value from the function, which is its result. * @throws NullPointerException If given argument is {@code null} * @see org.apache.commons.lang3.tuple.Pair */ default R apply(@Nonnull Pair<T, U> tuple) { Objects.requireNonNull(tuple); return apply(tuple.getLeft(), tuple.getRight()); } /** * Applies this function partially to some arguments of this one, producing a {@link Function2} as result. * * @param t The first argument to this function used to partially apply this function * @return A {@code Function2} that represents this function partially applied the some arguments. */ @Nonnull default Function2<U, R> papply(T t) { return (u) -> this.apply(t, u); } /** * Returns the number of arguments for this function. * * @return The number of arguments for this function. * @implSpec The default implementation always returns {@code 2}. */ @Nonnegative default int arity() { return 2; } /** * Returns a composed {@link BiFunction2} that first applies the {@code before} functions to its input, and * then applies this function to the result. * If evaluation of either operation throws an exception, it is relayed to the caller of the composed operation. * * @param <A> The type of the argument to the first given function, and of composed function * @param <B> The type of the argument to the second given function, and of composed function * @param before1 The first function to apply before this function is applied * @param before2 The second function to apply before this function is applied * @return A composed {@code BiFunction2} that first applies the {@code before} functions to its input, and then * applies this function to the result. * @throws NullPointerException If given argument is {@code null} * @implSpec The input argument of this method is able to handle every type. */ @Nonnull default <A, B> BiFunction2<A, B, R> compose(@Nonnull final Function<? super A, ? extends T> before1, @Nonnull final Function<? super B, ? extends U> before2) { Objects.requireNonNull(before1); Objects.requireNonNull(before2); return (a, b) -> apply(before1.apply(a), before2.apply(b)); } /** * Returns a composed {@link BiFunction2} that first applies this function to its input, and then applies the * {@code after} function to the result. * If evaluation of either operation throws an exception, it is relayed to the caller of the composed operation. * * @param <S> The type of return value from the {@code after} function, and of the composed function * @param after The function to apply after this function is applied * @return A composed {@code BiFunction2} that first applies this function to its input, and then applies the {@code * after} function to the result. * @throws NullPointerException If given argument is {@code null} * @implSpec The input argument of this method is able to return every type. */ @Nonnull default <S> BiFunction2<T, U, S> andThen(@Nonnull final Function<? super R, ? extends S> after) { Objects.requireNonNull(after); return (t, u) -> after.apply(apply(t, u)); } /** * Returns a composed {@link BiConsumer2} that fist applies this function to its input, and then consumes the result * using the given {@link Consumer}. * If evaluation of either operation throws an exception, it is relayed to the caller of the composed operation. * * @param consumer The operation which consumes the result from this operation * @return A composed {@code BiConsumer2} that first applies this function to its input, and then consumes the * result using the given {@code Consumer}. * @throws NullPointerException If given argument is {@code null} */ @Nonnull default BiConsumer2<T, U> consume(@Nonnull final Consumer<? super R> consumer) { Objects.requireNonNull(consumer); return (t, u) -> consumer.accept(apply(t, u)); } /** * Returns a curried version of this function. * * @return A curried version of this function. */ @Nonnull default Function2<T, Function2<U, R>> curried() { return t -> u -> apply(t, u); } /** * Returns a tupled version of this function. * * @return A tupled version of this function. */ @Nonnull default Function2<Pair<T, U>, R> tupled() { return this::apply; } /** * Returns a reversed version of this function. This may be useful in recursive context. * * @return A reversed version of this function. */ @Nonnull default BiFunction2<U, T, R> reversed() { return (u, t) -> apply(t, u); } /** * Returns a memoized (caching) version of this {@link BiFunction2}. Whenever it is called, the mapping between the * input parameters and the return value is preserved in a cache, making subsequent calls returning the memoized * value instead of computing the return value again. * <p> * Unless the function and therefore the used cache will be garbage-collected, it will keep all memoized values * forever. * * @return A memoized (caching) version of this {@code BiFunction2}. * @implSpec This implementation does not allow the input parameters or return value to be {@code null} for the * resulting memoized function, as the cache used internally does not permit {@code null} keys or values. * @implNote The returned memoized function can be safely used concurrently from multiple threads which makes it * thread-safe. */ @Nonnull default BiFunction2<T, U, R> memoized() { if (isMemoized()) { return this; } else { final Map<Pair<T, U>, R> cache = new ConcurrentHashMap<>(); final Object lock = new Object(); return (BiFunction2<T, U, R> & Memoized) (t, u) -> { final R returnValue; synchronized (lock) { returnValue = cache.computeIfAbsent(Pair.of(t, u), key -> apply(key.getLeft(), key.getRight())); } return returnValue; }; } } /** * Converts this function to an equal function, which ensures that its result is not * {@code null} using {@link Optional}. This method mainly exists to avoid unnecessary {@code NullPointerException}s * through referencing {@code null} from this function. * * @return An equal function, which ensures that its result is not {@code null}. * @deprecated Use {@code lift} method for lifting this function. */ @Deprecated @Nonnull default BiFunction2<T, U, Optional<R>> nonNull() { return (t, u) -> Optional.ofNullable(apply(t, u)); } }