Class FullOption<T,I extends Option<T>>
- Type Parameters:
T- Type of the wrapped value.I- Type of the extending class.
- All Implemented Interfaces:
Option<T>
- Direct Known Subclasses:
FullNonNullOption,FullNullableOption
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFullOption(T value)Constructs a new option instance wrapping the given value. -
Method Summary
Modifier and TypeMethodDescription<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.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.Returns either the value wrapped by thisOption, if it is not empty, or the given value if thisOptionis empty.orElseThrow(@NotNull Supplier<? extends E> ignored)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 ignored)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.
-
Field Details
-
value
Value wrapped by this option.
-
-
Constructor Details
-
FullOption
Constructs a new option instance wrapping the given value.This method does not check if the given input value is
null, this check should be performed by implementation classes.- Parameters:
value- Value to wrap.
-
-
Method Details
-
isPresent
public boolean isPresent()Description copied from interface:OptionTests 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. -
isEmpty
public boolean isEmpty()Description copied from interface:OptionTests 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. -
unwrap
Description copied from interface:OptionAttempts 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.- Specified by:
unwrapin interfaceOption<T>- 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
Description copied from interface:OptionReturns 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. -
orGet
Description copied from interface:OptionReturns 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. -
orThrow
Description copied from interface:OptionReturns 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.- Specified by:
orThrowin interfaceOption<T>- Type Parameters:
E- Type of theExceptionvalue that will be thrown if thisOptionis empty.- Parameters:
ignored-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.
-
orElseThrow
Description copied from interface:OptionReturns 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.- Specified by:
orElseThrowin interfaceOption<T>- Type Parameters:
E- Type of the exception that will be returned by the givenSupplierand thrown.- Parameters:
ignored-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.
-
flatMap
@NotNull public <R> @NotNull Option<R> flatMap(@NotNull @NotNull Function<? super T,? extends Option<? extends R>> fn)Description copied from interface:OptionCalls 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.- 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:OptionCalls 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.- 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:OptionCreates 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. -
ifPresent
Description copied from interface:OptionExecutes 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. -
ifEmpty
Description copied from interface:OptionExecutes 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. -
with
@NotNull public I with(@NotNull @NotNull Consumer<? super T> ifPresent, @NotNull @NotNull Runnable ignored)Description copied from interface:OptionExecutes 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. -
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.
-