- Type Parameters:
T- Type of the represented/wrapped value.
- All Superinterfaces:
Option<T>
- All Known Implementing Classes:
EmptyNullable,FullNullableOption,NullOption
NullableOption represents and immutable wraps a single, nullable
value that may or may not exist.
Nullability
Implementations of this interface allow wrappingnull values. This
means this Option type has 3 states.
- Value is present and not
null. - Value is present and
null. - Value is absent.
For methods on this interface that accept Function or
Consumer arguments, the passed functions should be prepared to handle
a null input value.
-
Method Summary
Modifier and TypeMethodDescription@NotNull NullableOption<T>Calls 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.@NotNull NullableOption<T>Executes the givenRunnableif and only if thisOptionis empty.@NotNull NullableOption<T>Executes the givenConsumerwith the value wrapped by thisOptionif thisOptionis not empty.booleanisEmpty()Tests whether thisOptiondoes not contain a value.booleanisNull()Tests whether the wrapped value isnull.booleanTests whether thisOptioncontains a value.<R> @NotNull NullableOption<R>Calls the givenFunctionon the value wrapped by thisOptionif and only if thisOptionis not empty.<R> @NotNull NullableOption<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.@NotNull NonNullOption<T>Converts thisOptionto aNonNullOption.unwrap()Attempts to unwrap thisOption's value.@NotNull NullableOption<T>Executes the givenConsumerifPresenton the wrapped value if thisOptionis not empty, otherwise calls the givenRunnableifEmpty.Methods inherited from interface io.foxcapades.lib.opt.Option
valueEquals
-
Method Details
-
isNull
@Contract(pure=true) boolean isNull()Tests whether the wrapped value isnull.A return value of
truemeans that thisOptionis not empty and the value it wraps isnull.A return value of
falsemay mean either that thisOptionis empty, or that the wrapped value is notnull. To test if this is the case,isPresent()and/orisEmpty()should be used.- Returns:
trueif thisOptionis both non-empty and is wrapping anullvalue.falseif this option is empty, or is not wrapping anullvalue.
-
toNonNullable
Converts thisOptionto aNonNullOption.The conversion happens as follows:
Option conversion rules. This Value Returns Not- nullNon-empty Optionwrapping thisOption's value.nullEmpty Optionempty Empty Option- Returns:
- A new
NonNullOptionwhich may be empty or non-empty based on the rules detailed above.
-
isPresent
@Contract(pure=true) boolean isPresent()Tests whether thisOptioncontains a value.IMPORTANT: Implementations of this interface allow wrapping
nullvalues. The return value of this method does not indicate whether the wrapped value isnull. To check whether the wrapped value isnull, useisNull(). -
isEmpty
@Contract(pure=true) boolean isEmpty()Tests whether thisOptiondoes not contain a value.IMPORTANT: Implementations of this interface allow wrapping
nullvalues. The return value of this method does not indicate whether the wrapped value isnull. To check whether the wrapped value isnull, useisNull(). -
unwrap
Attempts to unwrap thisOption's value. If this option is empty, this method throws anUnwrapException.IMPORTANT: Implementations of this interface allow wrapping
nullvalues. This means the value returned may enull, and in that case, noUnwrapExceptionwill be thrown.- Specified by:
unwrapin interfaceOption<T>- Returns:
- The value wrapped by this
Option. This value may benull. - 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: Implementations of this interface allow wrapping
nullvalues. This means theothervalue will not be returned if thisOptionis wrapping anullvalue asnullis a legal and expected possible value. -
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: Implementations of this interface allow wrapping
nullvalues. This means the givenSupplierwill not be called if the wrapped value isnullasnullis a legal and expected possible value.The given
Suppliershould be prepared to handle anullinput.- Specified by:
orGetin interfaceOption<T>- 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: Implementations of this interface allow wrapping
nullvalues. This means the givenExceptionwill not be thrown if the wrapped value isnull. Instead, the exception will only be thrown if thisOptionis wrapping no value whatsoever.- Specified by:
orThrowin interfaceOption<T>- 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. - Throws:
E- Thrown if thisOptionis empty.NullPointerException- If the givenExceptionisnull.
-
orElseThrow
@Nullable @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: Implementations of this interface allow wrapping
nullvalues. This means 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.- Specified by:
orElseThrowin interfaceOption<T>- 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. - Throws:
E- If thisOptionis empty.NullPointerException- if the givenSupplierisnullor if it returnsnull.
-
map
@Contract(pure=true) @NotNull <R> @NotNull NullableOption<R> map(@NotNull @NotNull Function<? super T,? extends R> fn)Calls the givenFunctionon the value wrapped by thisOptionif and only if thisOptionis not empty.IMPORTANT: Implementations of this interface allow wrapping
nullvalues. This means the givenFunctionshould be prepared to acceptnullvalues.The given
Functionmaynull.- Specified by:
mapin interfaceOption<T>- 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. - Throws:
NullPointerException- If the givenFunctionis null.
-
map
@NotNull @Contract(pure=true) <R> @NotNull NullableOption<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: Implementations of this interface allow wrapping
nullvalues. This means, if thisOptionis wrapping a null value, it will call theFunctionifPresent, passing in the wrappednullvalue. In this case,ifEmptywill not be called.The passed
FunctionifPresentmay returnnull, and should be prepared to accept a null input.If the called input function returns
null, the outputOptionwill be non-empty, wrapping anullvalue.- Specified by:
mapin interfaceOption<T>- 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 is null.
-
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: Implementations of this interface allow wrapping
nullvalues. This means, if the wrapped value isnull, the givenFunctionwill be called, and passed the wrappednullvalue.The given
Functionshould be prepared to handle anullinput.- Specified by:
flatMapin interfaceOption<T>- 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: Implementations of this interface allow wrappingnullvalues. This means, if thisOptionis wrapping anullvalue, it will call theFunctionifPresent, passing in the wrappednullvalue instead of callingifEmpty.The given
FunctionifPresentshould be prepared to accept anullinput value.- Specified by:
flatMapin interfaceOption<T>- 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: Implementations of this interface allow wrapping
nullvalues. This means the single value in the returnedStreamfor non-emptyOptions may benull. -
ifPresent
@NotNull @Contract(value="_ -> this", pure=true) @NotNull NullableOption<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: Implementations of this interface allow wrapping
nullvalues. This means the givenConsumerwill be called if theOptionis wrapping anullvalue passing in the wrappednullvalue.The given
Consumershould be prepared to handle anullinput.- Specified by:
ifPresentin interfaceOption<T>- 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 NullableOption<T> ifEmpty(@NotNull @NotNull Runnable fn)Executes the givenRunnableif and only if thisOptionis empty.IMPORTANT: Implementations of this interface allow wrapping
nullvalues. This means the givenRunnablewill not be called if thisOptionis wrapping anullvalue.- Specified by:
ifEmptyin interfaceOption<T>- Parameters:
fn-Runnableto call if thisOptionis empty.This argument must not be null.
- Returns:
- This
Optioninstance. - Throws:
NullPointerException- if the givenRunnablevalue isnull.
-
with
@NotNull @Contract(value="_, _ -> this", pure=true) @NotNull NullableOption<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: Implementations of this interface allow wrapping
nullvalues. This means if the wrapped value isnull, the given functionifPresentwill be called and passed the wrappednullvalue.If this
Optionis wrapping a null value,ifEmptywill not be called.The given
ConsumerifPresentshould be prepared to handle anullinput value.- Specified by:
within interfaceOption<T>- 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
@NotNull @Contract(pure=true) @NotNull NullableOption<T> filter(@NotNull @NotNull Predicate<? super T> fn)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: Implementations of this interface allow wrapping
nullvalues. This meansPredicatewill be called if theOptionis wrapping anullvalue, and will be passed the wrappednull.The given
Predicateshould be prepared to handle anullinput value.
-