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

javaslang.CheckedFunction0 Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC4
Show newest version
/*     / \____  _    _  ____   ______  / \ ____  __    _ _____
 *    /  /    \/ \  / \/    \ /  /\__\/  //    \/  \  / /  _  \   Javaslang
 *  _/  /  /\  \  \/  /  /\  \\__\\  \  //  /\  \ /\\/  \__/  /   Copyright 2014-now Daniel Dietrich
 * /___/\_/  \_/\____/\_/  \_/\__\/__/___\_/  \_//  \__/_____/    Licensed under the Apache License, Version 2.0
 */
package javaslang;

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

import java.util.Objects;
import javaslang.control.Option;
import javaslang.control.Try;

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

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

    /**
     * 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. */ static Function0> lift(CheckedFunction0 partialFunction) { return () -> Try.of(() -> partialFunction.apply()).getOption(); } /** * 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; @Override default int arity() { return 0; } @Override default CheckedFunction0 curried() { return this; } @Override default CheckedFunction1 tupled() { return t -> apply(); } @Override default CheckedFunction0 reversed() { return this; } @Override default CheckedFunction0 memoized() { if (isMemoized()) { return this; } else { return (CheckedFunction0 & Memoized) Lazy.of(() -> Try.of(this::apply).get())::get; } } /** * 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()); } @Override default Type getType() { return new Type<>(this); } /** * Represents the type of a {@code CheckedFunction0} which consists of no parameter types * and a return type. * * * @param the return type of the function * @author Daniel Dietrich * @since 2.0.0 */ final class Type extends λ.Type { private static final long serialVersionUID = 1L; private Type(CheckedFunction0 λ) { super(λ); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy