- Type Parameters:
T- Type of the wrapped value.
- All Superinterfaces:
Option<T>
- All Known Implementing Classes:
EmptyNonNull,FullNonNullOption
Option type that does not permit null values.
-
Method Summary
Modifier and TypeMethodDescription@NotNull NonNullOption<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 NonNullOption<T>Executes the givenRunnableif and only if thisOptionis empty.@NotNull NonNullOption<T>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 NonNullOption<R>Calls the givenFunctionon the value wrapped by thisOptionif and only if thisOptionis not empty.<R> @NotNull NonNullOption<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 NullableOption<T>Converts thisNonNullOptioninto an instance ofNullableOption.@NotNull NullableOption<T>toNullable(boolean emptyToNull)Converts thisNonNullOptioninto an instance ofNullableOption.unwrap()Attempts to unwrap thisOption's value.@NotNull NonNullOption<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
-
isPresent
@Contract(pure=true) boolean isPresent()Tests whether thisOptioncontains a value.IMPORTANT: Implementations of this interface do not allow wrapping
nullvalues. -
isEmpty
@Contract(pure=true) boolean isEmpty()Tests whether thisOptiondoes not contain a value.IMPORTANT: Implementations of this interface do not allow wrapping
nullvalues. -
unwrap
Attempts to unwrap thisOption's value. If this option is empty, this method throws anUnwrapException.IMPORTANT: Implementations of this interface do not allow wrapping
nullvalues.- Specified by:
unwrapin interfaceOption<T>- Returns:
- The value wrapped by this
Option. - 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 do not allow wrapping
nullvalues. -
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 do not allow wrapping
nullvalues.- 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 do not allow wrapping
nullvalues.- 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. 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: Implementations of this interface do not allow wrapping
nullvalues.- 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 NonNullOption<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 do not allow wrapping
nullvalues.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.- 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. ThisOptionwill be empty if the return value offnwasnull, otherwise the returnedOptionwill be wrapping the value returned by the givenFunction. - Throws:
NullPointerException- If the givenFunctionisnull.
-
map
@NotNull @Contract(pure=true) <R> @NotNull NonNullOption<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 do not allow wrapping
nullvalues.- 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 arenull.
-
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 do not allow wrapping
nullvalues.- 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 do not allow wrappingnullvalues.- 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 do not allow wrapping
nullvalues. -
ifPresent
@NotNull @Contract(value="_ -> this", pure=true) @NotNull NonNullOption<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 do not allow wrapping
nullvalues.- 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 NonNullOption<T> ifEmpty(@NotNull @NotNull Runnable fn)Executes the givenRunnableif and only if thisOptionis empty.IMPORTANT: Implementations of this interface do not allow wrapping
nullvalues.- 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
@Contract(value="_, _ -> this", pure=true) @NotNull @NotNull NonNullOption<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 do not allow wrapping
nullvalues.- 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(value="_ -> this", pure=true) @NotNull NonNullOption<T> filter(@NotNull @NotNull Predicate<? super T> fn)Calls the givenPredicateon the value wrapped by thisOption, if thisOptionis not empty.IMPORTANT: Implementations of this interface do not allow wrapping
nullvalues. -
toNullable
Converts thisNonNullOptioninto an instance ofNullableOption.If the current
Optionis empty, the returnedOptionwill also be empty.This method is equivalent to calling
toNullable(false).- Returns:
- A
NullableOptionthat is either empty or wrapping the same value as thisOption.
-
toNullable
Converts thisNonNullOptioninto an instance ofNullableOption.If the current option is not empty, the returned
Optionwill wrap the same value as thisOption.If the current option is empty, either a new
NullableOptionwrappingnullor an emptyOptionwill be returned depending on the value ofemptyToNull.If
emptyToNull == true && this.isEmpty(), then aNullableOptionthat is not-empty, wrapping the valuenullwill be returned.If
emptyToNull == false && this.isEmpty(), then an empty option will be returned.- Parameters:
emptyToNull- Flag indicating whether an empty option instance should be converted to a null wrapping option or an empty option.- Returns:
- A new option that will be wrapping this option's value, null, or will be empty.
-