Interface Function0<R>
- Type Parameters:
R- return type of the function
- All Superinterfaces:
Serializable, Supplier<R>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Represents a function with no arguments.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondefault <V> Function0<V> Returns a composed function that first applies this Function0 to the given argument and then applies Functionafterto the result.apply()Applies this function to no arguments and returns the result.default intarity()Returns the number of function arguments.static <R> Function0<R> constant(R value) Returns a function that always returns the constant value that you give in parameter.curried()Returns a curried version of this function.default Rget()Implementation of Supplier.get(), just calls apply().default booleanChecks if this function is memoizing (= caching) computed values.Lifts the givenpartialFunctioninto a total function that returns anOptionresult.Lifts the givenpartialFunctioninto a total function that returns anTryresult.memoized()Returns a memoizing version of this function, which computes the return value for given arguments only one time.static <R> Function0<R> Narrows the givenFunction0<? extends R>toFunction0<R>static <R> Function0<R> reversed()Returns a reversed version of this function.tupled()Returns a tupled version of this function.
-
Field Details
-
serialVersionUID
-
-
Method Details
-
constant
Returns a function that always returns the constant value that you give in parameter.- Type Parameters:
R- the result type- Parameters:
value- the value to be returned- Returns:
- a function always returning the given value
-
of
Creates aFunction0based 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- Type Parameters:
R- return type- Parameters:
methodReference- (typically) a method reference, e.g.Type::method- Returns:
- a
Function0
-
lift
Lifts the givenpartialFunctioninto a total function that returns anOptionresult.- Type Parameters:
R- return type- Parameters:
partialFunction- a function that is not defined for all values of the domain (e.g. by throwing)- Returns:
- a function that applies arguments to the given
partialFunctionand returnsSome(result)if the function is defined for the given arguments, andNoneotherwise.
-
liftTry
Lifts the givenpartialFunctioninto a total function that returns anTryresult.- Type Parameters:
R- return type- Parameters:
partialFunction- a function that is not defined for all values of the domain (e.g. by throwing)- Returns:
- a function that applies arguments to the given
partialFunctionand returnsSuccess(result)if the function is defined for the given arguments, andFailure(throwable)otherwise.
-
narrow
-
apply
R apply()Applies this function to no arguments and returns the result.- Returns:
- the result of function application
-
get
-
arity
default int arity()Returns the number of function arguments.- Returns:
- an int value >= 0
- See Also:
-
curried
-
tupled
-
reversed
-
memoized
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
nullas single argument or return value.- Returns:
- a memoizing function equivalent to this.
-
isMemoized
default boolean isMemoized()Checks if this function is memoizing (= caching) computed values.- Returns:
- true, if this function is memoizing, false otherwise
-
andThen
Returns a composed function that first applies this Function0 to the given argument and then applies Functionafterto the result.- Type Parameters:
V- return type of after- Parameters:
after- the function applied after this- Returns:
- a function composed of this and after
- Throws:
NullPointerException- if after is null
-