- Type Parameters:
T- the type of value
A container object which may or may not contain a value which can be null. If a value has been set, isSet() returns true. If not value has been set, the object is considered
undefined and isUndefined() returns false.
Unlike Optional which is used to represent "no result": a value or null, a Settable is used to represent undefined values: a value, null or undefined.
This basically allows to implement overriding logic where a base value can be left untouched or overridden with a new value or null depending on whether a corresponding Settable was
set or not.
It should be used as return method type where there is a need to represent undefined values for which it is important to determine whether a value, including null, was set or not. A
variable whose type is Settable should never itself be null; it should always point to a Settable instance.
- Since:
- 1.12
- Author:
- Jeremy Kuhn
-
Method Summary
Modifier and TypeMethodDescriptionboolean<U> Settable<U> If a value is set, returns the result of applying the givenSettable-bearing mapping function to the value, otherwise returns an undefinedSettable.get()If a value is set, returns the value, otherwise throwsNoSuchElementException.inthashCode()voidIf a value is set, performs the given action with the value, otherwise does nothing.voidifSetOrElse(Consumer<? super T> action, Runnable undefinedAction) If a value is set, performs the given action with the value, otherwise performs the given empty-based action.booleanisSet()If a value is set, returnstrue, otherwisefalse.booleanIf no value is set, returnstrue, otherwisefalse.<U> Settable<U> If a value is set, returns anSettabledescribing the result of applying the given mapping function to the value, otherwise returns an undefinedSettable.static <T> Settable<T> of(T value) Returns anSettabledescribing the given value which can benull.If a value is set, returns the value, otherwise returnsother.If a value is set, returns the value, otherwise returns the result produced by the supplying function.If a value is set, returns the value, otherwise throwsNoSuchElementException.orElseThrow(Supplier<? extends X> exceptionSupplier) If a value is set, returns the value, otherwise throws an exception produced by the exception supplying function.Converts theSettableto anOptional.static <T> Settable<T> Returns an undefinedSettableinstance.
-
Method Details
-
flatMap
public <U> Settable<U> flatMap(Function<? super T, ? extends Settable<? extends U>> mapper) throws NullPointerExceptionIf a value is set, returns the result of applying the given
Settable-bearing mapping function to the value, otherwise returns an undefinedSettable.This method is similar to
map(Function), but the mapping function is one whose result is already anSettable, and if invoked,flatMapdoes not wrap it within an additionalSettable.- Type Parameters:
U- the type of value of theSettablereturned by the mapping function- Parameters:
mapper- the mapping function to apply to a value, if set- Returns:
- the result of applying an
Settable-bearing mapping function to the value of thisSettable, if a value is set, otherwise an undefinedSettable - Throws:
NullPointerException- if the mapping function isnullor returns anullresult
-
get
If a value is set, returns the value, otherwise throws
NoSuchElementException.The preferred alternative to this method is
orElseThrow().- Returns:
- the value described by this
Settablewhich can benull - Throws:
NoSuchElementException- if no value is set
-
ifSet
If a value is set, performs the given action with the value, otherwise does nothing.
- Parameters:
action- the action to be performed, if a value is set- Throws:
NullPointerException- if value is set and the given action isnull
-
ifSetOrElse
public void ifSetOrElse(Consumer<? super T> action, Runnable undefinedAction) throws NullPointerException If a value is set, performs the given action with the value, otherwise performs the given empty-based action.
- Parameters:
action- the action to be performed, if a value is setundefinedAction- the empty-based action to be performed, if no value is set- Throws:
NullPointerException- if a value is set and the given action isnull, or no value is set and the given empty-based action isnull.
-
isSet
public boolean isSet()If a value is set, returns
true, otherwisefalse.- Returns:
trueif a value is set, otherwisefalse
-
isUndefined
public boolean isUndefined()If no value is set, returns
true, otherwisefalse.- Returns:
trueif no value is set, otherwisefalse
-
map
If a value is set, returns an
Settabledescribing the result of applying the given mapping function to the value, otherwise returns an undefinedSettable.By the definition, the mapping function can return a
nullresulting in aSettablewith anullvalue being returned.- Type Parameters:
U- the type of the value returned from the mapping function- Parameters:
mapper- the mapping function to apply to a value, if set- Returns:
- a
Settabledescribing the result of applying a mapping function to the value of thisSettable, if a value is set, otherwise an emptySettable - Throws:
NullPointerException- if the mapping function isnull
-
orElse
If a value is set, returns the value, otherwise returns
other.- Parameters:
other- the value to be returned, if no value is set. May benull.- Returns:
- the value, if set, otherwise
other
-
orElseGet
If a value is set, returns the value, otherwise returns the result produced by the supplying function.
- Parameters:
supplier- the supplying function that produces a value to be returned- Returns:
- the value, if set, otherwise the result produced by the supplying function
- Throws:
NullPointerException- if no value is set and the supplying function isnull
-
orElseThrow
If a value is set, returns the value, otherwise throws
NoSuchElementException.- Returns:
- the value described by this
Settablewhich can benull - Throws:
NoSuchElementException- if no value is set
-
orElseThrow
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X, NullPointerException If a value is set, returns the value, otherwise throws an exception produced by the exception supplying function.
- Type Parameters:
X- the type of the exception to be thrown- Parameters:
exceptionSupplier- the supplying function that produces an exception to be thrown- Returns:
- the value described by this
Settablewhich can benull - Throws:
X- if no value is setNullPointerException- if no value is set and the exception supplying function isnull
-
toOptional
Converts the
Settableto anOptional.The returned
Optionalis empty when theSettableis undefined or if its value isnull.- Returns:
- an
Optionalwith a present value if theSettableis set with a non-nullvalue or an emptyOptionalif it is not set or set with anullvalue
-
undefined
Returns an undefined
Settableinstance. No value is set for thisSettable.Though it may be tempting to do so, avoid testing if an object is undefined by comparing with
==or!=against instances returned bySettable.undefined(). There is no guarantee that it is a singleton. Instead, useisUndefined()orisSet().- Type Parameters:
T- The type of the non-existent value- Returns:
- an undefined
Settable
-
of
Returns an
Settabledescribing the given value which can benull.- Type Parameters:
T- the type of the value- Parameters:
value- the value to describe, which can benull- Returns:
- a
Settablewith the value set
-
equals
-
hashCode
public int hashCode()
-