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

io.vavr.CheckedFunction0 Maven / Gradle / Ivy

/*  __    __  __  __    __  ___
 * \  \  /  /    \  \  /  /  __/
 *  \  \/  /  /\  \  \/  /  /
 *   \____/__/  \__\____/__/
 *
 * Copyright 2014-2020 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 static io.vavr.CheckedFunction0Module.sneakyThrow;

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

/**
 * Represents a function with no arguments.
 *
 * @param  return type of the function
 * @author Daniel Dietrich
 */
@FunctionalInterface
public interface CheckedFunction0 extends Serializable {

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

    /**
     * Returns a function that always returns the constant
     * value that you give in parameter.
     *

     * @param  the result type
     * @param value the value to be returned
     * @return a function always returning the given value
     */
    static  CheckedFunction0 constant(R value) {
        return () -> value;
    }

    /**
     * Creates a {@code CheckedFunction0} 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 * @return a {@code CheckedFunction0} */ static CheckedFunction0 of(CheckedFunction0 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 * @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 Function0> lift(CheckedFunction0 partialFunction) { return () -> Try.of(partialFunction::apply).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 * @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 Function0> liftTry(CheckedFunction0 partialFunction) { return () -> Try.of(partialFunction::apply); } /** * Narrows the given {@code CheckedFunction0} to {@code CheckedFunction0} * * @param f A {@code CheckedFunction0} * @param return type * @return the given {@code f} instance as narrowed type {@code CheckedFunction0} */ @SuppressWarnings("unchecked") static CheckedFunction0 narrow(CheckedFunction0 f) { return (CheckedFunction0) f; } /** * Applies this function to no arguments and returns the result. * * @return the result of function application * @throws Throwable if something goes wrong applying this function to the given arguments */ R apply() throws Throwable; /** * Returns the number of function arguments. * @return an int value >= 0 * @see Arity */ default int arity() { return 0; } /** * Returns a curried version of this function. * * @return a curried function equivalent to this. */ default CheckedFunction0 curried() { return this; } /** * Returns a tupled version of this function. * * @return a tupled function equivalent to this. */ default CheckedFunction1 tupled() { return t -> apply(); } /** * Returns a reversed version of this function. This may be useful in a recursive context. * * @return a reversed function equivalent to this. */ default CheckedFunction0 reversed() { return this; } /** * 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 CheckedFunction0 memoized() { if (isMemoized()) { return this; } else { final Lazy lazy = Lazy.of(() -> { try { return apply(); } catch (Throwable x) { throw new RuntimeException(x); } }); return (CheckedFunction0 & Memoized) () -> { try { return lazy.get(); } catch(RuntimeException x) { throw x.getCause(); } }; } } /** * 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; } /** * Return a composed function that first applies this CheckedFunction0 to the given arguments and in case of throwable * try to get value from {@code recover} function with same arguments and throwable information. * * @param recover the function applied in case of throwable * @return a function composed of this and recover * @throws NullPointerException if recover is null */ default Function0 recover(Function> recover) { Objects.requireNonNull(recover, "recover is null"); return () -> { try { return this.apply(); } catch (Throwable throwable) { final Supplier func = recover.apply(throwable); Objects.requireNonNull(func, () -> "recover return null for " + throwable.getClass() + ": " + throwable.getMessage()); return func.get(); } }; } /** * Returns an unchecked function that will sneaky throw if an exceptions occurs when applying the function. * * @return a new Function0 that throws a {@code Throwable}. */ default Function0 unchecked() { return () -> { try { return apply(); } catch(Throwable t) { return sneakyThrow(t); } }; } /** * Returns a composed function that first applies this CheckedFunction0 to the given argument and then applies * {@linkplain CheckedFunction1} {@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 CheckedFunction0 andThen(CheckedFunction1 after) { Objects.requireNonNull(after, "after is null"); return () -> after.apply(apply()); } } interface CheckedFunction0Module { // DEV-NOTE: we do not plan to expose this as public API @SuppressWarnings("unchecked") static R sneakyThrow(Throwable t) throws T { throw (T) t; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy