Module com.machinezoo.noexception
Interface OptionalFunction<T,R>
-
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface OptionalFunction<T,R> extends Function<T,Optional<R>>
Variation ofFunction
that returnsOptional
instead of the raw value.OptionalFunction
is typically obtained fromExceptionHandler.function(Function)
, in which case its return value is empty when the underlyingFunction
throws an exception. See noexception tutorial.- See Also:
-
ExceptionHandler.function(Function)
,Function
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Optional<R>
apply(T t)
Variation ofFunction.apply(Object)
that returnsOptional
.default Function<T,R>
orElse(R result)
Converts thisOptionalFunction
to plainFunction
using default value.default Function<T,R>
orElseGet(Supplier<R> source)
-
-
-
Method Detail
-
apply
Optional<R> apply(T t)
Variation ofFunction.apply(Object)
that returnsOptional
. If thisOptionalFunction
is obtained fromExceptionHandler.function(Function)
, theOptional
will be empty only if the underlyingFunction
throws. Otherwise the returnedOptional
just wraps the return value of underlyingFunction
(possiblynull
).- Specified by:
-
apply
in interfaceFunction<T,R>
- Parameters:
-
t
- seeFunction.apply(Object)
- Returns:
-
Optional
typically wrapping return value ofFunction.apply(Object)
, or an emptyOptional
(typically signifying an exception) - See Also:
-
ExceptionHandler.function(Function)
,Function.apply(Object)
-
orElse
default Function<T,R> orElse(R result)
Converts thisOptionalFunction
to plainFunction
using default value. The returnedFunction
will unwrap present value from theOptional
if possible, or returnresult
if theOptional
is empty.- Parameters:
-
result
- default value to return instead of an emptyOptional
- Returns:
-
plain
Function
that either unwrapsOptional
or returns default value - See Also:
-
orElseGet(Supplier)
,Optional.orElse(Object)
-
orElseGet
default Function<T,R> orElseGet(Supplier<R> source)
Converts thisOptionalFunction
to plainFunction
using fallbackSupplier
. The returnedFunction
will unwrap present value from theOptional
if possible, or fall back to callingsource
if theOptional
is empty.- Parameters:
-
source
-Supplier
to query for fallback value whenOptional
is empty - Returns:
-
plain
Function
that either unwrapsOptional
or falls back tosource
- See Also:
-
orElse(Object)
,Optional.orElseGet(Supplier)
-
-