- Type Parameters:
T- Generic type of this option.
- All Implemented Interfaces:
NullableOption<T>,Option<T>
null option.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanIndicates whether some other object is "equal to" this one.@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>> ignored)Calls the given mappingFunctionifPresentif thisOptionis not empty, otherwise calls the givenSupplierifEmpty.inthashCode()Returns a hash code value for this object.@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.static <T> @NotNull NullableOption<T>instance()Returns the singletonNullOptioninstance.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.toString()Returns a string representation of this record class.unwrap()Attempts to unwrap thisOption's value.booleanvalueEquals(@Nullable Object value)Returns whether the value wrapped by this option equals the given input value.@NotNull NullableOption<T>Executes the givenConsumerifPresenton the wrapped value if thisOptionis not empty, otherwise calls the givenRunnableifEmpty.
-
Constructor Details
-
NullOption
public NullOption()Creates an instance of aNullOptionrecord class.
-
-
Method Details
-
isNull
public boolean isNull()Description copied from interface:NullableOptionTests 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,NullableOption.isPresent()and/orNullableOption.isEmpty()should be used.- Specified by:
isNullin interfaceNullableOption<T>- Returns:
trueif thisOptionis both non-empty and is wrapping anullvalue.falseif this option is empty, or is not wrapping anullvalue.
-
isPresent
public boolean isPresent()Description copied from interface:NullableOptionTests 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, useNullableOption.isNull(). -
isEmpty
public boolean isEmpty()Description copied from interface:NullableOptionTests 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, useNullableOption.isNull(). -
unwrap
Description copied from interface:NullableOptionAttempts 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 interfaceNullableOption<T>- 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
Description copied from interface:NullableOptionReturns 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
Description copied from interface:NullableOptionReturns 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 interfaceNullableOption<T>- 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.
-
orThrow
Description copied from interface:NullableOptionReturns 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 interfaceNullableOption<T>- 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.
-
orElseThrow
Description copied from interface:NullableOptionReturns 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 interfaceNullableOption<T>- 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.
-
map
@NotNull public <R> @NotNull NullableOption<R> map(@NotNull @NotNull Function<? super T,? extends R> fn)Description copied from interface:NullableOptionCalls 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 interfaceNullableOption<T>- 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.
-
map
@NotNull public <R> @NotNull NullableOption<R> map(@NotNull @NotNull Function<? super T,? extends R> ifPresent, @NotNull @NotNull Supplier<? extends R> ignored)Description copied from interface:NullableOptionCalls 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 interfaceNullableOption<T>- 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.ignored- Value supplier to call when thisOptionis empty.This argument must not be
null.- Returns:
- A new
Optionof generic typeR.
-
flatMap
@NotNull public <R> @NotNull Option<R> flatMap(@NotNull @NotNull Function<? super T,? extends Option<? extends R>> fn)Description copied from interface:NullableOptionCalls 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 interfaceNullableOption<T>- 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.
-
flatMap
@NotNull public <R> @NotNull Option<R> flatMap(@NotNull @NotNull Function<? super T,? extends Option<? extends R>> ifPresent, @NotNull @NotNull Supplier<? extends Option<? extends R>> ignored)Description copied from interface:NullableOptionCalls 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 interfaceNullableOption<T>- 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.ignored- 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.
-
stream
Description copied from interface:NullableOptionCreates 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
Description copied from interface:NullableOptionExecutes 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. -
ifEmpty
Description copied from interface:NullableOptionExecutes 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. -
with
@NotNull public @NotNull NullableOption<T> with(@NotNull @NotNull Consumer<? super T> ifPresent, @NotNull @NotNull Runnable ignored)Description copied from interface:NullableOptionExecutes 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 interfaceNullableOption<T>- Specified by:
within interfaceOption<T>- Parameters:
ifPresent-Consumerto call with the value wrapped by thisOptionif thisOptionis not empty.This argument must not be null.
ignored-Runnableto call if thisOptionis empty.This argument must not be null.
- Returns:
- This
Optioninstance.
-
filter
Description copied from interface:NullableOptionCalls 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.- Specified by:
filterin interfaceNullableOption<T>- Specified by:
filterin interfaceOption<T>- 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.
-
toNonNullable
Description copied from interface:NullableOptionConverts thisOptionto aNonNullOption.The conversion happens as follows:
Option conversion rules. This Value Returns Not- nullNon-empty Optionwrapping thisOption's value.nullEmpty Optionempty Empty Option- Specified by:
toNonNullablein interfaceNullableOption<T>- Returns:
- A new
NonNullOptionwhich may be empty or non-empty based on the rules detailed above.
-
instance
Returns the singletonNullOptioninstance.- Type Parameters:
T- Generic type of the returned option.- Returns:
- The singleton
NullOptioninstance.
-
valueEquals
Description copied from interface:OptionReturns whether the value wrapped by this option equals the given input value.For empty options, this method will always return
false.- Specified by:
valueEqualsin interfaceOption<T>- 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.
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal.
-