- Type Parameters:
T
- the type of value implementations of this interface supply
- All Superinterfaces:
Supplier<T>
- All Known Implementing Classes:
Absence
,CachingSupplier
,FixedValueSupplier
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Supplier
with additional contractual requirements.
An OptionalSupplier
does not behave like an
Optional
or a CompletableFuture
, despite the
deliberate similarities of some method names.
An implementation of this interface is not a value-based class.
- Author:
- Laird Nelson
- See Also:
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic enum
A token indicating transitory or permanent presence or absence. -
Method Summary
Modifier and TypeMethodDescriptiondefault OptionalSupplier.Determinism
Returns anOptionalSupplier.Determinism
denoting the presence of values returned by thisOptionalSupplier
'sget()
method.default T
exceptionally
(Function<? super RuntimeException, ? extends T> handler) Returns either the result of invoking theget()
method, if aRuntimeException
does not occur, or the result of invoking theapply(Object)
method on the suppliedhandler
with anyRuntimeException
that does occur, includingNoSuchElementException
andUnsupportedOperationException
instances that are used to indicate value absence.get()
Returns a value, which may benull
.default <U> U
handle
(BiFunction<? super T, ? super RuntimeException, ? extends U> handler) Returns the result of invoking theapply(Object, Object)
method on the suppliedhandler
, supplying it with the result of an invocation of theget()
method, which may benull
, or anyRuntimeException
that an invocation of theget()
method throws, includingNoSuchElementException
andUnsupportedOperationException
instances that are used to indicate value absence.default void
Invokes theaccept(Object)
method on the suppliedaction
with the return value of an invocation of theget()
method, which may benull
, unless theget()
method throws either aNoSuchElementException
or anUnsupportedOperationException
, indicating value absence, in which case no action is taken.default void
ifPresentOrElse
(Consumer<? super T> presentAction, Runnable absentAction) Invokes theaccept(Object)
method on the suppliedpresentAction
with the return value of an invocation of theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, invokes therun()
method of the suppliedabsentAction
instead.static <T> OptionalSupplier<T>
of()
InvokesAbsence.instance()
and returns the result.static <T> OptionalSupplier<T>
Returns anOptionalSupplier
representing the suppliedSupplier
.static <T> OptionalSupplier<T>
Returns anOptionalSupplier
that will, loosely speaking, try the suppliedsupplier
first, and, if it throws aNoSuchElementException
or anUnsupportedOperationException
from itsSupplier.get()
method, will then try the supplieddefaults
.static <T> OptionalSupplier<T>
of
(OptionalSupplier.Determinism determinism, Supplier<? extends T> supplier) Returns a newOptionalSupplier
whosedeterminism()
method will return the suppliedOptionalSupplier.Determinism
and whoseget()
method will return the result of invoking theSupplier.get()
method on the suppliedsupplier
.static <T> OptionalSupplier<T>
of
(T value) Returns a newOptionalSupplier
whosedeterminism()
method will returnOptionalSupplier.Determinism.PRESENT
and whoseget()
method will return the suppliedvalue
.optional()
default <U> Optional<U>
optional
(BiFunction<? super T, ? super RuntimeException, ? extends U> handler) Invokes thehandle(BiFunction)
method, supplying it with the suppliedhandler
, supplies its return value to theOptional.ofNullable(Object)
method, and returns the result.optional
(Function<? super RuntimeException, ? extends T> handler) Invokes theexceptionally(Function)
method, supplying it with the suppliedhandler
, supplies its return value to theOptional.ofNullable(Object)
method, and returns the result.default T
Returns the result of invoking theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the supplied alternate value, which may benull
.default T
Returns the result of invoking theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the result of invoking theget()
method on the suppliedSupplier
, which may benull
.default T
Returns the result of invoking theget()
method, which may benull
.orElseThrow
(Supplier<? extends X> throwableSupplier) Returns the result of invoking theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, throws the return value of an invocation of the suppliedSupplier
'sget()
method.stream()
Returns the result of invoking theStream.of(Object)
method on the return value of an invocation of theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the result of invoking theStream.empty()
method.
-
Method Details
-
exceptionally
Returns either the result of invoking theget()
method, if aRuntimeException
does not occur, or the result of invoking theapply(Object)
method on the suppliedhandler
with anyRuntimeException
that does occur, includingNoSuchElementException
andUnsupportedOperationException
instances that are used to indicate value absence.- Parameters:
handler
- the exception handler; must not benull
- Returns:
- either the result of invoking the
get()
method, if aRuntimeException
does not occur, or the result of invoking theapply(Object)
method on the suppliedhandler
with anyRuntimeException
that does occur - Throws:
NullPointerException
- ifhandler
isnull
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method and its (discouraged) overrides may
return
null
- Thread Safety:
- This method itself is, and its (discouraged)
overrides must be, safe for concurrent use by multiple threads,
but the
apply(Object)
method of the suppliedhandler
may not be.
-
get
Returns a value, which may benull
.This method's contract extends
Supplier.get()
's contract with the following additional requirements:- It is deliberately and explicitly permitted for
implementations of this method to return
null
for any reason. Callers of this method must be appropriately prepared. - If an invocation of the
determinism()
method, on any thread, returns any ofOptionalSupplier.Determinism.ABSENT
,OptionalSupplier.Determinism.DETERMINISTIC
orOptionalSupplier.Determinism.PRESENT
, then additional requirements apply to the implementation of this method; see thedeterminism()
method documentation for details. - An implementation of this method need not be deterministic if
an invocation of the
determinism()
method returnsOptionalSupplier.Determinism.NON_DETERMINISTIC
. - An implementation of this method may indicate the (possibly
transitory) absence of a value by any of the following means:
- Throwing a
NoSuchElementException
. - Throwing an
UnsupportedOperationException
.
- Throwing a
- Specified by:
get
in interfaceSupplier<T>
- Returns:
- a value, which may be
null
- Throws:
NoSuchElementException
- to indicate (usually transitory) absenceUnsupportedOperationException
- to indicate (usually permanent) absence- See Also:
- Idempotency:
- No guarantees are made about idempotency or
determinism. An ideal implementation should be idempotent. If
the
determinism()
method returns aOptionalSupplier.Determinism
that is notOptionalSupplier.Determinism.NON_DETERMINISTIC
, then any implementation of this method must be deterministic. - Nullability:
- Implementations of this method may, and often will,
return
null
, even in the normal course of events. - Thread Safety:
- Implementations of this method must be safe for concurrent use by multiple threads.
- It is deliberately and explicitly permitted for
implementations of this method to return
-
handle
Returns the result of invoking theapply(Object, Object)
method on the suppliedhandler
, supplying it with the result of an invocation of theget()
method, which may benull
, or anyRuntimeException
that an invocation of theget()
method throws, includingNoSuchElementException
andUnsupportedOperationException
instances that are used to indicate value absence.The first argument to the handler will be the return value of the
get()
method, which may benull
. The second argument to the handler will be anyRuntimeException
that was thrown during the invocation of theget()
method, includingNoSuchElementException
andUnsupportedOperationException
instances that are used to indicate value absence.- Type Parameters:
U
- the type of the value returned- Parameters:
handler
- the handler; must not benull
- Returns:
- the result of invoking the
apply(Object, Object)
method on the suppliedhandler
, supplying it with the result of an invocation of theget()
method, which may benull
, or anyRuntimeException
that an invocation of theget()
method throws, includingNoSuchElementException
andUnsupportedOperationException
instances that are used to indicate value absence - Throws:
NullPointerException
- ifhandler
isnull
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method and its (discouraged) overrides may
return
null
. - Thread Safety:
- This method is, and its (discouraged) overrides must be, safe for concurrent use by multiple threads.
-
ifPresent
Invokes theaccept(Object)
method on the suppliedaction
with the return value of an invocation of theget()
method, which may benull
, unless theget()
method throws either aNoSuchElementException
or anUnsupportedOperationException
, indicating value absence, in which case no action is taken.- Parameters:
action
- theConsumer
representing the action to take; must not benull
- Throws:
NullPointerException
- ifaction
isnull
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Thread Safety:
- This method is, and its (discouraged) overrides
must be, safe for concurrent use by multiple threads, but the
supplied
Consumer
'saccept(Object)
method may not be
-
ifPresentOrElse
Invokes theaccept(Object)
method on the suppliedpresentAction
with the return value of an invocation of theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, invokes therun()
method of the suppliedabsentAction
instead.- Parameters:
presentAction
- theConsumer
representing the action to take if a value is present; must not benull
absentAction
- theRunnable
representing the action to take if a value is absent; must not benull
- Throws:
NullPointerException
- ifaction
orabsentAction
isnull
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Thread Safety:
- This method is, and its (discouraged) overrides
must be, safe for concurrent use by multiple threads, but the
supplied
Consumer
'saccept(Object)
method may not be and the suppliedRunnable
'srun()
method may not be
-
optional
Returns a non-null
but possibly emptyOptional
representing thisOptionalSupplier
's value.The default implementation of this method does not and its overrides must not return
null
.The default implementation of this method catches all
NoSuchElementException
s andUnsupportedOperationException
s and returns an emptyOptional
in these cases, which may not be valid for some use cases. To detect permanent versus transitory absence, potential callers should use theget()
method directly or either theoptional(Function)
oroptional(BiFunction)
methods.- Returns:
- a non-
null
but possibly emptyOptional
representing thisOptionalSupplier
's value - See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- The default implementation of this method does not,
and its (discouraged) overrides must not, return
null
. - Thread Safety:
- The default implementation of this method is, and its (discouraged) overrides must be, safe for concurrent use by multiple threads.
-
optional
default <U> Optional<U> optional(BiFunction<? super T, ? super RuntimeException, ? extends U> handler) Invokes thehandle(BiFunction)
method, supplying it with the suppliedhandler
, supplies its return value to theOptional.ofNullable(Object)
method, and returns the result.- Type Parameters:
U
- the type of the returnedOptional
- Parameters:
handler
- the handler; must not benull
- Returns:
- the result of invoking the
Optional.ofNullable(Object)
method supplied with the return value of invoking thehandle(BiFunction)
method with the suppliedhandler
- Throws:
NullPointerException
- ifhandler
isnull
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method does not, and its (discouraged)
overrides must not, return
null
. - Thread Safety:
- This method is, and its (discouraged) overrides must be, safe for concurrent use by multiple threads.
-
optional
Invokes theexceptionally(Function)
method, supplying it with the suppliedhandler
, supplies its return value to theOptional.ofNullable(Object)
method, and returns the result.- Parameters:
handler
- the handler; must not benull
- Returns:
- the result of invoking the
Optional.ofNullable(Object)
method supplied with the return value of invoking theexceptionally(Function)
method with the suppliedhandler
- Throws:
NullPointerException
- ifhandler
isnull
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method does not, and its (discouraged)
overrides must not, return
null
. - Thread Safety:
- This method is, and its (discouraged) overrides must be, safe for concurrent use by multiple threads.
-
orElse
Returns the result of invoking theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the supplied alternate value, which may benull
.- Parameters:
other
- the alternate value; may benull
- Returns:
- the result of invoking the
get()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the supplied alternate value, which may benull
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method and its (discouraged) overrides may
return
null
. - Thread Safety:
- This method is, and its (discouraged) overrides must be, safe for concurrent use by multiple threads.
-
orElseGet
Returns the result of invoking theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the result of invoking theget()
method on the suppliedSupplier
, which may benull
.- Parameters:
supplier
- the alternate valueSupplier
; must not benull
- Returns:
- the result of invoking the
get()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the result of invoking theget()
method on the suppliedSupplier
, which may benull
- Throws:
NullPointerException
- ifsupplier
isnull
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method and its (discouraged) overrides may
return
null
. - Thread Safety:
- This method is, and its (discouraged) overrides
msut be, safe for concurrent use by multiple threads, but the
get()
method of the suppliedsupplier
may not be
-
orElseThrow
Returns the result of invoking theget()
method, which may benull
.- Returns:
- the result of invoking the
get()
method, which may benull
- Throws:
NoSuchElementException
- if value absence was indicated by theget()
method- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method and its (discouraged) overrides may
return
null
. - Thread Safety:
- This method is, and its (discouraged) overrides must be, safe for concurrent use by multiple threads.
-
orElseThrow
Returns the result of invoking theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, throws the return value of an invocation of the suppliedSupplier
'sget()
method.- Type Parameters:
X
- the type ofThrowable
the suppliedthrowableSupplier
supplies- Parameters:
throwableSupplier
- theThrowable
Supplier
; must not benull
- Returns:
- the result of invoking the
get()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, throws the return value of an invocation of the suppliedSupplier
'sget()
method - Throws:
NullPointerException
- ifsupplier
isnull
X
- (actually<X>
) if theget()
method throws either aNoSuchElementException
or anUnsupportedOperationException
- See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method and its (discouraged) overrides may
return
null
. - Thread Safety:
- This method itself is, and its (discouraged) overrides must be, safe for concurrent use by multiple threads.
-
determinism
Returns anOptionalSupplier.Determinism
denoting the presence of values returned by thisOptionalSupplier
'sget()
method.Overrides of this method must be compatible with the implementation of the
get()
method. Specifically:- If an override of this method returns
OptionalSupplier.Determinism.PRESENT
, then the implementation of theget()
method must never throw either aNoSuchElementException
or anUnsupportedOperationException
, and, for any two invocations, on any thread, must return either the same object, or objects that are indistinguishable from one another and that can be substituted for each other interchangeably. - If an override of this method returns
OptionalSupplier.Determinism.ABSENT
, then it is expected that any two invocations of theget()
method, on any thread, will each throw either aNoSuchElementException
or anUnsupportedOperationException
. If an implementation of theget()
method instead returns a value, contrary to these requirements, then the value must be treated as irrelevant or undefined. - If an override of this method returns
OptionalSupplier.Determinism.DETERMINISTIC
, then any two invocations of theget()
method implementation, on any thread, must either throw either aNoSuchElementException
or anUnsupportedOperationException
, or must return either the same object, or objects that are indistinguishable from one another and that can be substituted for each other interchangeably. - If an override of this method returns
OptionalSupplier.Determinism.NON_DETERMINISTIC
, then there are no additional requirements placed upon the implementation of theget()
method besides those defined in its existing contract.
Additionally, once an override of this method returns a
OptionalSupplier.Determinism
whosedeterministic()
method returnstrue
, then it must forever afterwards, for all invocations, on all possible threads, return the sameOptionalSupplier.Determinism
.If any of the requirements above is not met, undefined behavior may result.
The default implementation of this method returns
OptionalSupplier.Determinism.NON_DETERMINISTIC
. Overrides are strongly encouraged.- Returns:
- an
OptionalSupplier.Determinism
denoting the presence of values returned by thisOptionalSupplier
'sget()
method - Idempotency:
- This method is idempotent and deterministic. Its (encouraged) overrides must be idempotent. They may not be deterministic (see above).
- Nullability:
- This method does not, and its (encouraged) overrides
must not, return
null
. - Thread Safety:
- This method is, and its (encouraged) overrides must be, safe for concurrent use by multiple threads.
- If an override of this method returns
-
stream
Returns the result of invoking theStream.of(Object)
method on the return value of an invocation of theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the result of invoking theStream.empty()
method.Note that this means the sole element of the
Stream
that is returned may benull
. If this is undesirable, consider invokingstream()
on the return value of theoptional()
method instead.- Returns:
- the result of invoking the
Stream.of(Object)
method on the return value of an invocation of theget()
method, which may benull
, or, if theget()
method indicates value absence by throwing either aNoSuchElementException
or anUnsupportedOperationException
, returns the result of invoking theStream.empty()
method - See Also:
- Idempotency:
- No guarantees are made about idempotency or determinism.
- Nullability:
- This method does not, and its (discouraged)
overrides must not, return
null
. - Thread Safety:
- This method is, and its (discouraged) overrides must be, safe for concurrent use by multiple threads.
-
of
static <T> OptionalSupplier<T> of(OptionalSupplier.Determinism determinism, Supplier<? extends T> supplier) Returns a newOptionalSupplier
whosedeterminism()
method will return the suppliedOptionalSupplier.Determinism
and whoseget()
method will return the result of invoking theSupplier.get()
method on the suppliedsupplier
.- Type Parameters:
T
- the type of value the returnedOptionalSupplier
will supply- Parameters:
determinism
- theOptionalSupplier.Determinism
to use; must not benull
supplier
- theSupplier
whoseget()
method will be called; must not benull
- Returns:
- a new
OptionalSupplier
whosedeterminism()
method will return the suppliedOptionalSupplier.Determinism
and whoseget()
method will return the result of invoking theSupplier.get()
method on the suppliedsupplier
- Throws:
NullPointerException
- ifdeterminism
orsupplier
isnull
- Idempotency:
- This method is not idempotent but is deterministic.
- Nullability:
- This method never returns
null
. - Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
of
InvokesAbsence.instance()
and returns the result.- Type Parameters:
T
- the type of the nonexistent value the returnedOptionalSupplier
will never supply- Returns:
- the result of invoking
Absence.instance()
- See Also:
- Idempotency:
- This method is idempotent and deterministic.
- Nullability:
- This method never returns
null
. - Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
of
Returns anOptionalSupplier
representing the suppliedSupplier
.- Type Parameters:
T
- the type of value the returnedOptionalSupplier
will supply- Parameters:
supplier
- theSupplier
; may benull
in which case the return value of an invocation ofAbsence.instance()
will be returned- Returns:
- an
OptionalSupplier
representing the suppliedSupplier
- Idempotency:
- This method is not idempotent but is deterministic.
- Nullability:
- This method never returns
null
. - Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
of
Returns anOptionalSupplier
that will, loosely speaking, try the suppliedsupplier
first, and, if it throws aNoSuchElementException
or anUnsupportedOperationException
from itsSupplier.get()
method, will then try the supplieddefaults
.The supplied
Supplier
instances can, themselves, beOptionalSupplier
s, and, if so, the resultingOptionalSupplier
will have itsdeterminism()
method appropriately implemented.- Type Parameters:
T
- the type of object the returnedOptionalSupplier
will supply- Parameters:
supplier
- the firstSupplier
to try; may benull
defaults
- the fallbackSupplier
to try; may benull
- Returns:
- a
DefaultingOptionalSupplier
- Idempotency:
- This method is idempotent and deterministic.
- Nullability:
- This method never returns
null
. - Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
of
Returns a newOptionalSupplier
whosedeterminism()
method will returnOptionalSupplier.Determinism.PRESENT
and whoseget()
method will return the suppliedvalue
.- Type Parameters:
T
- the type of value the returnedOptionalSupplier
will supply- Parameters:
value
- the value the newOptionalSupplier
will return from itsget()
method; may benull
- Returns:
- a new
OptionalSupplier
whosedeterminism()
method will returnOptionalSupplier.Determinism.PRESENT
and whoseget()
method will return the suppliedvalue
- Idempotency:
- This method is not idempotent but is deterministic.
- Nullability:
- This method never returns
null
. - Thread Safety:
- This method is safe for concurrent use by multiple threads.
-