- Type Parameters:
T- Type of the represented/wrapped value.
- All Known Subinterfaces:
NonNullOption<T>,NullableOption<T>
- All Known Implementing Classes:
EmptyNonNull,EmptyNullable,FullNonNullOption,FullNullableOption,FullOption,NullOption
Option represents and immutably wraps a single value that may or may
not exist.
Nullability
The baseOption type does not set any rules for whether implementing
types may allow null values to be wrapped.
Implementations should detail their rules regarding null values.
For these implementations there are 3 states. Present containing a
non-null value, present containing a null value, and empty.
-
Method Summary
Modifier and TypeMethodDescriptionCalls the givenPredicateon the value wrapped by thisOption, if thisOptionis not empty.<R> @NotNull Option<R>Calls the givenFunctionif thisOptionis not empty.<R> @NotNull Option<R>flatMap(@NotNull Function<? super T,? extends Option<? extends R>> ifPresent, @NotNull Supplier<? extends Option<? extends R>> ifEmpty)Calls the given mappingFunctionifPresentif thisOptionis not empty, otherwise calls the givenSupplierifEmpty.Executes the givenRunnableif and only if thisOptionis empty.Executes the givenConsumerwith the value wrapped by thisOptionif thisOptionis not empty.booleanisEmpty()Tests whether thisOptiondoes not contain a value.booleanTests whether thisOptioncontains a value.<R> @NotNull Option<R>Calls the givenFunctionon the value wrapped by thisOptionif and only if thisOptionis not empty.<R> @NotNull Option<R>Calls the givenFunctionifPresentif thisOptionis not empty, otherwise calls the givenSupplierifEmpty.Returns either the value wrapped by thisOption, if it is not empty, or the given value if thisOptionis empty.orElseThrow(@NotNull Supplier<? extends E> fn)Returns the value wrapped by thisOptionif it is not empty, otherwise throws theExceptionreturned by the givenSupplier.Returns either the value wrapped by thisOption, if it is not empty, or the value returned by the givenSupplier.orThrow(E err)Returns the value wrapped by thisOptionif it is not empty, otherwise throws the givenExceptionvalue.stream()Creates a newStreamcontaining either one value, if thisOptionis not empty, or containing zero values if thisOptionis empty.unwrap()Attempts to unwrap thisOption's value.booleanvalueEquals(@Nullable Object value)Returns whether the value wrapped by this option equals the given input value.Executes the givenConsumerifPresenton the wrapped value if thisOptionis not empty, otherwise calls the givenRunnableifEmpty.
-
Method Details
-
isPresent
@Contract(pure=true) boolean isPresent()Tests whether thisOptioncontains a value.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, a return value oftruedoes not mean the value contained is notnull. It simply means that a value is wrapped.Implementers should detail their rules regarding
nullvalues.- Returns:
trueif thisOptioncontains a value, otherwisefalse.
-
isEmpty
@Contract(pure=true) boolean isEmpty()Tests whether thisOptiondoes not contain a value.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, a return value offalsedoes not indicate whether the value wrapped isnull, instead it simply means that no value whatsoever was wrapped.Implementers should detail their rules regarding
nullvalues.- Returns:
trueif thisOptiondoes not contain a value, otherwisefalse.
-
unwrap
Attempts to unwrap thisOption's value. If this option is empty, this method throws anUnwrapException.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, this method will not throw an exception if the value returned isnull. Instead it will only throw an exception if no value was set whatsoever.Implementers should detail their rules regarding
nullvalues.- Returns:
- The value wrapped by this
Option. This value may benulldepending on whether this implementation ofOptionallowsnullvalues. - Throws:
UnwrapException- If this method is called on an emptyOption.
-
or
Returns either the value wrapped by thisOption, if it is not empty, or the given value if thisOptionis empty.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, theothervalue will not be returned if theOptionis wrapping anullvalue as, for those implementations,nullis a legal and expected possible value.Implementers should detail their rules regarding
nullvalues.- Parameters:
other- Fallback value to return if thisOptionis empty.This argument may be
null.- Returns:
- Either the value wrapped by this
Option, or the value ofother.
-
orGet
Returns either the value wrapped by thisOption, if it is not empty, or the value returned by the givenSupplier.The given
Supplierwill not be called if thisOptionis not empty.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, theSupplierwill not be called if theOptionis wrapping anullvalue as, for those implementations,nullis a legal and expected possible value.Implementers should detail their rules regarding
nullvalues.- Parameters:
fn-Supplierfor the fallback value.This argument must not be null.
The value returned by the given
Suppliermay be null.- Returns:
- Either the value wrapped by this
Option, or the value returned by the givenSupplier. - Throws:
NullPointerException- if the givenSupplieris null.
-
orThrow
Returns the value wrapped by thisOptionif it is not empty, otherwise throws the givenExceptionvalue.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, the givenExceptionwill not be thrown if the wrapped value isnull. Instead, the exception will only be thrown if thisOptionis wrapping no value whatsoever.Implementers should detail their rules regarding
nullvalues.- Type Parameters:
E- Type of theExceptionvalue that will be thrown if thisOptionis empty.- Parameters:
err-Exceptionto throw if thisOptionis empty.This argument must not be null.
- Returns:
- The value wrapped by this
Option. Individual implementations decide whethernullvalues may be wrapped and returned. - Throws:
E- Thrown if thisOptionis empty.NullPointerException- If the givenExceptionisnull.
-
orElseThrow
@Contract(pure=true) <E extends Throwable> T orElseThrow(@NotNull @NotNull Supplier<? extends E> fn) throws EReturns the value wrapped by thisOptionif it is not empty, otherwise throws theExceptionreturned by the givenSupplier.The given
Supplierwill not be called if thisOptionis not empty.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, theSupplierwill not be called, and noExceptionwill be thrown if the wrapped value isnull. Instead, theSupplierwill be called andExceptionthrown only if thisOptionis wrapping no value whatsoever.Implementers should detail their rules regarding
nullvalues.- Type Parameters:
E- Type of the exception that will be returned by the givenSupplierand thrown.- Parameters:
fn-Supplierfor theExceptionto throw if thisOptionis empty.This argument must not be
null.This
Suppliermust not returnnull.- Returns:
- The value wrapped by this
Option. Individual implementations decide whethernullvalues may be wrapped. - Throws:
E- If thisOptionis empty.NullPointerException- if the givenSupplierisnullor if it returnsnull.
-
map
@Contract(pure=true) @NotNull <R> @NotNull Option<R> map(@NotNull @NotNull Function<? super T,? extends R> fn)Calls the givenFunctionon the value wrapped by thisOptionif and only if thisOptionis not empty.If the given function returns
nullan emptyOptionwill be returned. If the given function does not returnnulla newOptionwill be returned wrapping the result value of the call to the givenFunction.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, the givenFunctionshould be prepared to acceptnullvalues.Implementers should detail their rules regarding
nullvalues.- Type Parameters:
R- The return type of the given mappingFunction.- Parameters:
fn- Function to call on the value wrapped by thisOption.This argument must not be
null- Returns:
- A new
Optionof generic typeR. ThisOptionwill be empty if the return value offnwasnull, otherwise the returnedOptionwill be wrapping the value returned by the givenFunction. - Throws:
NullPointerException- If the givenFunctionis null.
-
map
@NotNull @Contract(pure=true) <R> @NotNull Option<R> map(@NotNull @NotNull Function<? super T,? extends R> ifPresent, @NotNull @NotNull Supplier<? extends R> ifEmpty)Calls the givenFunctionifPresentif thisOptionis not empty, otherwise calls the givenSupplierifEmpty.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, if theOptionis wrapping a null value, it will call theFunctionifPresent, passing in the wrappednullvalue. This means, for thoseOptionimplementations, theFunctionpassed asifPresentshould be prepared to handlenullvalues.Implementers should detail their rules regarding
nullvalues.- Type Parameters:
R- Generic type of the returnedOption.- Parameters:
ifPresent- Mapping function to call on the value wrapped by thisOption.This argument must not be
null.This function will only be called if this
Optionis not empty.ifEmpty- Value supplier to call when thisOptionis empty.This argument must not be
null.- Returns:
- A new
Optionof generic typeR. - Throws:
NullPointerException- If either of the given functions isnull.
-
flatMap
@NotNull @Contract(pure=true) <R> @NotNull Option<R> flatMap(@NotNull @NotNull Function<? super T,? extends Option<? extends R>> fn)Calls the givenFunctionif thisOptionis not empty.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. For those implementations, if the wrapped value isnull, the givenFunctionwill be called, and passed the wrappednullvalue. If the current implementation allows nulls, thisFunctionshould be prepared to handlenullinputs.Implementers should detail their rules regarding
nullvalues.- Type Parameters:
R- Generic type of the returnedOption.- Parameters:
fn- Mapping function to call if thisOptionis not empty.This argument must not be null.
This function must not return null
This function will not be called if this
Optionis empty.- Returns:
- A new
Optionof generic typeR. - Throws:
NullPointerException- if the given function isnullor if the given function returnsnull.
-
flatMap
@NotNull @Contract(pure=true) <R> @NotNull Option<R> flatMap(@NotNull @NotNull Function<? super T,? extends Option<? extends R>> ifPresent, @NotNull @NotNull Supplier<? extends Option<? extends R>> ifEmpty)Calls the given mappingFunctionifPresentif thisOptionis not empty, otherwise calls the givenSupplierifEmpty. IMPORTANT: Individual implementations ofOptiondecide whether anullvalue may be wrapped. For those implementations, if theOptionis wrapping a null value, it will call theFunctionifPresent, passing in the wrappednullvalue. This means, for thoseOptionimplementations, theFunctionpassed asifPresentshould be prepared to handlenullvalues.Implementers should detail their rules regarding
nullvalues.- Type Parameters:
R- Generic type for the returnedOption.- Parameters:
ifPresent- Mapping function to call if thisOptionis not empty.This argument must not be
null.This function must not return
null.ifEmpty- Supplier function to call if thisOptionis empty.This argument must not be null.
This value must not return null.
- Returns:
- A new
Optionof generic typeR. - Throws:
NullPointerException- IfifPresentisnull,ifEmptyisnull, or if the called function returnsnull.
-
stream
Creates a newStreamcontaining either one value, if thisOptionis not empty, or containing zero values if thisOptionis empty.IMPORTANT: Individual implementations of
Optiondecide whether anullvalue may be wrapped. This means, for those implementations, the single value in the returnedStreamfor non-emptyOptions may benull.Implementers should detail their rules regarding
nullvalues.- Returns:
- A new
Streamthat may contain the value wrapped by thisOption, if such a value exists, otherwise contains no values.
-
ifPresent
@NotNull @Contract(value="_ -> this", pure=true) @NotNull Option<T> ifPresent(@NotNull @NotNull Consumer<? super T> fn)Executes the givenConsumerwith the value wrapped by thisOptionif thisOptionis not empty.The given function will not be called if this
Optionis empty.IMPORTANT: Individual
Optionimplementations decide whether anullvalue may be wrapped. For those implementations, the givenConsumerwill be called if theOptionis wrapping anullvalue. This means, for those implementations, the passedConsumershould be prepared to handle anullinput.Implementers should detail their rules regarding
nullvalues.- Parameters:
fn-Consumerto call on the wrapped value if thisOptionis not empty.This argument must not be null.
- Returns:
- This
Optioninstance. - Throws:
NullPointerException- If the givenConsumervalue isnull.
-
ifEmpty
@NotNull @Contract(value="_ -> this", pure=true) @NotNull Option<T> ifEmpty(@NotNull @NotNull Runnable fn)Executes the givenRunnableif and only if thisOptionis empty.IMPORTANT: Individual
Optionimplementations decide whether anullvalue may be wrapped. For those implementations, if thisOptionis wrapping anullvalue, the given function will not be called.Implementers should detail their rules regarding
nullvalues.- Parameters:
fn-Runnableto call if thisOptionis empty.This argument must not be null.
- Returns:
- This
Optioninstance. - Throws:
NullPointerException- if the givenRunnablevalue isnull.
-
with
@Contract(value="_, _ -> this", pure=true) @NotNull @NotNull Option<T> with(@NotNull @NotNull Consumer<? super T> ifPresent, @NotNull @NotNull Runnable ifEmpty)Executes the givenConsumerifPresenton the wrapped value if thisOptionis not empty, otherwise calls the givenRunnableifEmpty.IMPORTANT: Individual
Optionimplementations decide whether anullvalue may be wrapped. For those implementations, if the wrapped value isnull, the given functionifPresentwill be called and passed the wrappednullvalue,nullvalues are considered legal and possible valid values. This means, for thoseOptionimplementations, the givenifPresentfunction should be prepared to handle anullinput.Implementers should detail their rules regarding
nullvalues.- Parameters:
ifPresent-Consumerto call with the value wrapped by thisOptionif thisOptionis not empty.This argument must not be null.
ifEmpty-Runnableto call if thisOptionis empty.This argument must not be null.
- Returns:
- This
Optioninstance. - Throws:
NullPointerException- IfifPresentisnull, or ififEmptyisnull.
-
filter
Calls the givenPredicateon the value wrapped by thisOption, if thisOptionis not empty.The given
Predicatewill not be called if thisOptionis empty.If the given
Predicatereturnstrue, thisOptionwill be returned, otherwise an emptyOptionwill be returned.IMPORTANT: Individual
Optionimplementations decide whether anullvalue may be wrapped. For those implementations, the givenPredicatewill be called if theOptionis wrapping anullvalue, and will be passed the wrappednull. This means, for those implementations, the givenPredicateshould be prepared to handle anullinput.Implementers should detail their rules regarding
nullvalues.- Parameters:
fn-Predicateto apply to the value wrapped by thisOptionif thisOptionis not empty.This value must not be null.
- Returns:
- This
Optionif the givenPredicatereturnstrue, otherwise an emptyOption.
-
valueEquals
Returns whether the value wrapped by this option equals the given input value.For empty options, this method will always return
false.- Parameters:
value- Value to compare against the wrapped value.- Returns:
- Whether the given value equals the value wrapped by this option.
If this option is empty, this value will always be
false. If this option allowsnullvalues and is wrapping anullvalue, this method will only returntrueif the given input is alsonull.
-