- 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
.int
hashCode()
void
If a value is set, performs the given action with the value, otherwise does nothing.void
ifSetOrElse
(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.boolean
isSet()
If a value is set, returnstrue
, otherwisefalse
.boolean
If no value is set, returnstrue
, otherwisefalse
.<U> Settable
<U> If a value is set, returns anSettable
describing the result of applying the given mapping function to the value, otherwise returns an undefinedSettable
.static <T> Settable
<T> of
(T value) Returns anSettable
describing 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 theSettable
to anOptional
.static <T> Settable
<T> Returns an undefinedSettable
instance.
-
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,flatMap
does not wrap it within an additionalSettable
.- Type Parameters:
U
- the type of value of theSettable
returned 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 isnull
or returns anull
result
-
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
Settable
which 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:
true
if a value is set, otherwisefalse
-
isUndefined
public boolean isUndefined()If no value is set, returns
true
, otherwisefalse
.- Returns:
true
if no value is set, otherwisefalse
-
map
If a value is set, returns an
Settable
describing the result of applying the given mapping function to the value, otherwise returns an undefinedSettable
.By the definition, the mapping function can return a
null
resulting in aSettable
with anull
value 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
Settable
describing 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
Settable
which 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
Settable
which can benull
- Throws:
X
- if no value is setNullPointerException
- if no value is set and the exception supplying function isnull
-
toOptional
Converts the
Settable
to anOptional
.The returned
Optional
is empty when theSettable
is undefined or if its value isnull
.- Returns:
- an
Optional
with a present value if theSettable
is set with a non-null
value or an emptyOptional
if it is not set or set with anull
value
-
undefined
Returns an undefined
Settable
instance. 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
Settable
describing 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
Settable
with the value set
-
equals
-
hashCode
public int hashCode()
-