- 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
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumA token indicating transitory or permanent presence or absence. -
Method Summary
Modifier and TypeMethodDescriptiondefault OptionalSupplier.DeterminismReturns anOptionalSupplier.Determinismdenoting the presence of values returned by thisOptionalSupplier'sget()method.default Texceptionally(Function<? super RuntimeException, ? extends T> handler) Returns either the result of invoking theget()method, if aRuntimeExceptiondoes not occur, or the result of invoking theapply(Object)method on the suppliedhandlerwith anyRuntimeExceptionthat does occur, includingNoSuchElementExceptionandUnsupportedOperationExceptioninstances that are used to indicate value absence.get()Returns a value, which may benull.default <U> Uhandle(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 anyRuntimeExceptionthat an invocation of theget()method throws, includingNoSuchElementExceptionandUnsupportedOperationExceptioninstances that are used to indicate value absence.default voidInvokes theaccept(Object)method on the suppliedactionwith the return value of an invocation of theget()method, which may benull, unless theget()method throws either aNoSuchElementExceptionor anUnsupportedOperationException, indicating value absence, in which case no action is taken.default voidifPresentOrElse(Consumer<? super T> presentAction, Runnable absentAction) Invokes theaccept(Object)method on the suppliedpresentActionwith the return value of an invocation of theget()method, which may benull, or, if theget()method indicates value absence by throwing either aNoSuchElementExceptionor anUnsupportedOperationException, invokes therun()method of the suppliedabsentActioninstead.static <T> OptionalSupplier<T>of()InvokesAbsence.instance()and returns the result.static <T> OptionalSupplier<T>Returns anOptionalSupplierrepresenting the suppliedSupplier.static <T> OptionalSupplier<T>Returns anOptionalSupplierthat will, loosely speaking, try the suppliedsupplierfirst, and, if it throws aNoSuchElementExceptionor anUnsupportedOperationExceptionfrom itsSupplier.get()method, will then try the supplieddefaults.static <T> OptionalSupplier<T>of(OptionalSupplier.Determinism determinism, Supplier<? extends T> supplier) Returns a newOptionalSupplierwhosedeterminism()method will return the suppliedOptionalSupplier.Determinismand whoseget()method will return the result of invoking theSupplier.get()method on the suppliedsupplier.static <T> OptionalSupplier<T>of(T value) Returns a newOptionalSupplierwhosedeterminism()method will returnOptionalSupplier.Determinism.PRESENTand 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 TReturns the result of invoking theget()method, which may benull, or, if theget()method indicates value absence by throwing either aNoSuchElementExceptionor anUnsupportedOperationException, returns the supplied alternate value, which may benull.default TReturns the result of invoking theget()method, which may benull, or, if theget()method indicates value absence by throwing either aNoSuchElementExceptionor anUnsupportedOperationException, returns the result of invoking theget()method on the suppliedSupplier, which may benull.default TReturns 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 aNoSuchElementExceptionor 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 aNoSuchElementExceptionor anUnsupportedOperationException, returns the result of invoking theStream.empty()method.
-
Method Details
-
exceptionally
Returns either the result of invoking theget()method, if aRuntimeExceptiondoes not occur, or the result of invoking theapply(Object)method on the suppliedhandlerwith anyRuntimeExceptionthat does occur, includingNoSuchElementExceptionandUnsupportedOperationExceptioninstances 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 aRuntimeExceptiondoes not occur, or the result of invoking theapply(Object)method on the suppliedhandlerwith anyRuntimeExceptionthat does occur - Throws:
NullPointerException- ifhandlerisnull- 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 suppliedhandlermay 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
nullfor 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.DETERMINISTICorOptionalSupplier.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:
getin 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.Determinismthat 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 anyRuntimeExceptionthat an invocation of theget()method throws, includingNoSuchElementExceptionandUnsupportedOperationExceptioninstances 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 anyRuntimeExceptionthat was thrown during the invocation of theget()method, includingNoSuchElementExceptionandUnsupportedOperationExceptioninstances 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 anyRuntimeExceptionthat an invocation of theget()method throws, includingNoSuchElementExceptionandUnsupportedOperationExceptioninstances that are used to indicate value absence - Throws:
NullPointerException- ifhandlerisnull- 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 suppliedactionwith the return value of an invocation of theget()method, which may benull, unless theget()method throws either aNoSuchElementExceptionor anUnsupportedOperationException, indicating value absence, in which case no action is taken.- Parameters:
action- theConsumerrepresenting the action to take; must not benull- Throws:
NullPointerException- ifactionisnull- 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 suppliedpresentActionwith the return value of an invocation of theget()method, which may benull, or, if theget()method indicates value absence by throwing either aNoSuchElementExceptionor anUnsupportedOperationException, invokes therun()method of the suppliedabsentActioninstead.- Parameters:
presentAction- theConsumerrepresenting the action to take if a value is present; must not benullabsentAction- theRunnablerepresenting the action to take if a value is absent; must not benull- Throws:
NullPointerException- ifactionorabsentActionisnull- 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-nullbut possibly emptyOptionalrepresenting 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
NoSuchElementExceptions andUnsupportedOperationExceptions and returns an emptyOptionalin 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-
nullbut possibly emptyOptionalrepresenting 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- ifhandlerisnull- 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- ifhandlerisnull- 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 aNoSuchElementExceptionor 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 aNoSuchElementExceptionor 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 aNoSuchElementExceptionor 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 aNoSuchElementExceptionor anUnsupportedOperationException, returns the result of invoking theget()method on the suppliedSupplier, which may benull - Throws:
NullPointerException- ifsupplierisnull- 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 suppliedsuppliermay 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 aNoSuchElementExceptionor anUnsupportedOperationException, throws the return value of an invocation of the suppliedSupplier'sget()method.- Type Parameters:
X- the type ofThrowablethe suppliedthrowableSuppliersupplies- Parameters:
throwableSupplier- theThrowableSupplier; must not benull- Returns:
- the result of invoking the
get()method, which may benull, or, if theget()method indicates value absence by throwing either aNoSuchElementExceptionor anUnsupportedOperationException, throws the return value of an invocation of the suppliedSupplier'sget()method - Throws:
NullPointerException- ifsupplierisnullX- (actually<X>) if theget()method throws either aNoSuchElementExceptionor 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.Determinismdenoting 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 aNoSuchElementExceptionor 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 aNoSuchElementExceptionor 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 aNoSuchElementExceptionor 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.Determinismwhosedeterministic()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.Determinismdenoting 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 aNoSuchElementExceptionor anUnsupportedOperationException, returns the result of invoking theStream.empty()method.Note that this means the sole element of the
Streamthat 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 aNoSuchElementExceptionor 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 newOptionalSupplierwhosedeterminism()method will return the suppliedOptionalSupplier.Determinismand whoseget()method will return the result of invoking theSupplier.get()method on the suppliedsupplier.- Type Parameters:
T- the type of value the returnedOptionalSupplierwill supply- Parameters:
determinism- theOptionalSupplier.Determinismto use; must not benullsupplier- theSupplierwhoseget()method will be called; must not benull- Returns:
- a new
OptionalSupplierwhosedeterminism()method will return the suppliedOptionalSupplier.Determinismand whoseget()method will return the result of invoking theSupplier.get()method on the suppliedsupplier - Throws:
NullPointerException- ifdeterminismorsupplierisnull- 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 returnedOptionalSupplierwill 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 anOptionalSupplierrepresenting the suppliedSupplier.- Type Parameters:
T- the type of value the returnedOptionalSupplierwill supply- Parameters:
supplier- theSupplier; may benullin which case the return value of an invocation ofAbsence.instance()will be returned- Returns:
- an
OptionalSupplierrepresenting 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 anOptionalSupplierthat will, loosely speaking, try the suppliedsupplierfirst, and, if it throws aNoSuchElementExceptionor anUnsupportedOperationExceptionfrom itsSupplier.get()method, will then try the supplieddefaults.The supplied
Supplierinstances can, themselves, beOptionalSuppliers, and, if so, the resultingOptionalSupplierwill have itsdeterminism()method appropriately implemented.- Type Parameters:
T- the type of object the returnedOptionalSupplierwill supply- Parameters:
supplier- the firstSupplierto try; may benulldefaults- the fallbackSupplierto 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 newOptionalSupplierwhosedeterminism()method will returnOptionalSupplier.Determinism.PRESENTand whoseget()method will return the suppliedvalue.- Type Parameters:
T- the type of value the returnedOptionalSupplierwill supply- Parameters:
value- the value the newOptionalSupplierwill return from itsget()method; may benull- Returns:
- a new
OptionalSupplierwhosedeterminism()method will returnOptionalSupplier.Determinism.PRESENTand 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.
-