All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vavr.Function5 Maven / Gradle / Ivy

There is a newer version: 1.0.0-alpha-4
Show newest version
/*  __    __  __  __    __  ___
 * \  \  /  /    \  \  /  /  __/
 *  \  \/  /  /\  \  \/  /  /
 *   \____/__/  \__\____/__/
 *
 * Copyright 2014-2019 Vavr, http://vavr.io
 *
 * 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 io.vavr;

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*\
   G E N E R A T O R   C R A F T E D
\*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

import io.vavr.control.Option;
import io.vavr.control.Try;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
 * Represents a function with 5 arguments.
 *
 * @param  argument 1 of the function
 * @param  argument 2 of the function
 * @param  argument 3 of the function
 * @param  argument 4 of the function
 * @param  argument 5 of the function
 * @param  return type of the function
 * @author Daniel Dietrich
 */
@FunctionalInterface
public interface Function5 extends Serializable {

    /**
     * The serial version uid.
     */
    long serialVersionUID = 1L;

    /**
     * Returns a function that always returns the constant
     * value that you give in parameter.
     *
     * @param  generic parameter type 1 of the resulting function
     * @param  generic parameter type 2 of the resulting function
     * @param  generic parameter type 3 of the resulting function
     * @param  generic parameter type 4 of the resulting function
     * @param  generic parameter type 5 of the resulting function
     * @param  the result type
     * @param value the value to be returned
     * @return a function always returning the given value
     */
    static  Function5 constant(R value) {
        return (t1, t2, t3, t4, t5) -> value;
    }

    /**
     * Creates a {@code Function5} based on
     * 
     *
     * Examples (w.l.o.g. referring to Function1):
     * 
// using a lambda expression
     * Function1<Integer, Integer> add1 = Function1.of(i -> i + 1);
     *
     * // using a method reference (, e.g. Integer method(Integer i) { return i + 1; })
     * Function1<Integer, Integer> add2 = Function1.of(this::method);
     *
     * // using a lambda reference
     * Function1<Integer, Integer> add3 = Function1.of(add1::apply);
     * 
*

* Caution: Reflection loses type information of lambda references. *

// type of a lambda expression
     * Type<?, ?> type1 = add1.getType(); // (Integer) -> Integer
     *
     * // type of a method reference
     * Type<?, ?> type2 = add2.getType(); // (Integer) -> Integer
     *
     * // type of a lambda reference
     * Type<?, ?> type3 = add3.getType(); // (Object) -> Object
     * 
* * @param methodReference (typically) a method reference, e.g. {@code Type::method} * @param return type * @param 1st argument * @param 2nd argument * @param 3rd argument * @param 4th argument * @param 5th argument * @return a {@code Function5} */ static Function5 of(Function5 methodReference) { return methodReference; } /** * Lifts the given {@code partialFunction} into a total function that returns an {@code Option} result. * * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing) * @param return type * @param 1st argument * @param 2nd argument * @param 3rd argument * @param 4th argument * @param 5th argument * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Some(result)} * if the function is defined for the given arguments, and {@code None} otherwise. */ @SuppressWarnings("RedundantTypeArguments") static Function5> lift(Function5 partialFunction) { return (t1, t2, t3, t4, t5) -> Try.of(() -> partialFunction.apply(t1, t2, t3, t4, t5)).toOption(); } /** * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result. * * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing) * @param return type * @param 1st argument * @param 2nd argument * @param 3rd argument * @param 4th argument * @param 5th argument * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)} * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise. */ static Function5> liftTry(Function5 partialFunction) { return (t1, t2, t3, t4, t5) -> Try.of(() -> partialFunction.apply(t1, t2, t3, t4, t5)); } /** * Narrows the given {@code Function5} to {@code Function5} * * @param f A {@code Function5} * @param return type * @param 1st argument * @param 2nd argument * @param 3rd argument * @param 4th argument * @param 5th argument * @return the given {@code f} instance as narrowed type {@code Function5} */ @SuppressWarnings("unchecked") static Function5 narrow(Function5 f) { return (Function5) f; } /** * Applies this function to 5 arguments and returns the result. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @param t4 argument 4 * @param t5 argument 5 * @return the result of function application * */ R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5); /** * Applies this function partially to one argument. * * @param t1 argument 1 * @return a partial application of this function */ default Function4 apply(T1 t1) { return (T2 t2, T3 t3, T4 t4, T5 t5) -> apply(t1, t2, t3, t4, t5); } /** * Applies this function partially to two arguments. * * @param t1 argument 1 * @param t2 argument 2 * @return a partial application of this function */ default Function3 apply(T1 t1, T2 t2) { return (T3 t3, T4 t4, T5 t5) -> apply(t1, t2, t3, t4, t5); } /** * Applies this function partially to three arguments. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @return a partial application of this function */ default Function2 apply(T1 t1, T2 t2, T3 t3) { return (T4 t4, T5 t5) -> apply(t1, t2, t3, t4, t5); } /** * Applies this function partially to 4 arguments. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @param t4 argument 4 * @return a partial application of this function */ default Function1 apply(T1 t1, T2 t2, T3 t3, T4 t4) { return (T5 t5) -> apply(t1, t2, t3, t4, t5); } /** * Returns the number of function arguments. * @return an int value >= 0 * @see Arity */ default int arity() { return 5; } /** * Returns a curried version of this function. * * @return a curried function equivalent to this. */ default Function1>>>> curried() { return t1 -> t2 -> t3 -> t4 -> t5 -> apply(t1, t2, t3, t4, t5); } /** * Returns a tupled version of this function. * * @return a tupled function equivalent to this. */ default Function1, R> tupled() { return t -> apply(t._1, t._2, t._3, t._4, t._5); } /** * Returns a reversed version of this function. This may be useful in a recursive context. * * @return a reversed function equivalent to this. */ default Function5 reversed() { return (t5, t4, t3, t2, t1) -> apply(t1, t2, t3, t4, t5); } /** * Returns a memoizing version of this function, which computes the return value for given arguments only one time. * On subsequent calls given the same arguments the memoized value is returned. *

* Please note that memoizing functions do not permit {@code null} as single argument or return value. * * @return a memoizing function equivalent to this. */ default Function5 memoized() { if (isMemoized()) { return this; } else { final Map, R> cache = new HashMap<>(); return (Function5 & Memoized) (t1, t2, t3, t4, t5) -> { final Tuple5 key = Tuple.of(t1, t2, t3, t4, t5); synchronized (cache) { if (cache.containsKey(key)) { return cache.get(key); } else { final R value = tupled().apply(key); cache.put(key, value); return value; } } }; } } /** * Checks if this function is memoizing (= caching) computed values. * * @return true, if this function is memoizing, false otherwise */ default boolean isMemoized() { return this instanceof Memoized; } /** * Returns a composed function that first applies this Function5 to the given argument and then applies * {@linkplain Function} {@code after} to the result. * * @param return type of after * @param after the function applied after this * @return a function composed of this and after * @throws NullPointerException if after is null */ default Function5 andThen(Function after) { Objects.requireNonNull(after, "after is null"); return (t1, t2, t3, t4, t5) -> after.apply(apply(t1, t2, t3, t4, t5)); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy