Package com.sinch.sdk.core.models
Class OptionalValue<T>
java.lang.Object
com.sinch.sdk.core.models.OptionalValue<T>
- Type Parameters:
T
- the type of value
class adapted from
Optional
enabling to:
- accept null value as authorized and defined value
- determine is a value was set with any value (null included) or not
So, this class enable to distinguish a value set to null VS an undefined value
- Since:
- 1.8
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> OptionalValue
<T> empty()
Returns an emptyOptionalValue
instance.boolean
Indicates whether some other object is "equal to" thisOptional
.get()
If a value is present, returns the value, otherwise throwsNoSuchElementException
.int
hashCode()
Returns the hash code of the value, if present, otherwise0
(zero) if no value is present.void
If a value is present, performs the given action with the value, otherwise does nothing.void
ifPresentOrElse
(Consumer<? super T> action, Runnable emptyAction) If a value is set, performs the given action with the value, otherwise performs the given empty-based action.boolean
If a value is present, returnstrue
, otherwisefalse
.<U> OptionalValue
<U> If a value is present, returns anOptionalValue
describing the result of applying the given mapping function to the value, otherwise returns an emptyOptionalValue
.static <T> OptionalValue
<T> of
(T value) Returns anOptionalValue
describing the given value.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 present, returns the value, otherwise throws an exception produced by the exception supplying function.toString()
Returns a non-empty string representation of thisOptionalValue
suitable for debugging.
-
Method Details
-
empty
Returns an emptyOptionalValue
instance. No value is present for thisOptional
.Though it may be tempting to do so, avoid testing if an object is empty by comparing with
==
against instances returned byOptional.empty()
. There is no guarantee that it is a singleton. Instead, useisPresent()
.- Type Parameters:
T
- The type of the non-existent value- Returns:
- an empty
OptionalValue
-
of
Returns anOptionalValue
describing the given value.- Type Parameters:
T
- the type of the value- Parameters:
value
- the value to describe- Returns:
- an
OptionalValue
with the value present
-
get
If a value is present, returns the value, otherwise throwsNoSuchElementException
.The preferred alternative to this method is
orElseThrow()
.- Returns:
- the value described by this
OptionalValue
- Throws:
NoSuchElementException
- if no value is present
-
isPresent
public boolean isPresent()If a value is present, returnstrue
, otherwisefalse
.- Returns:
true
if a value is present, otherwisefalse
-
ifPresent
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action
- the action to be performed, if a value is present- Throws:
NullPointerException
- if value is present and the given action isnull
-
ifPresentOrElse
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 setemptyAction
- the empty-based action to be performed, if no value is present- Throws:
NullPointerException
- if a value is present and the given action isnull
, or no value is present and the given empty-based action isnull
.- Since:
- 9
-
orElse
If a value is set, returns the value, otherwise returnsother
.- Parameters:
other
- the value to be returned, if no value is present. May benull
.- Returns:
- the value, if present, 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 present, otherwise the result produced by the supplying function
- Throws:
NullPointerException
- if no value is present and the supplying function isnull
-
orElseThrow
If a value is set, returns the value, otherwise throwsNoSuchElementException
.- Returns:
- the non-
null
value described by thisOptional
- Throws:
NoSuchElementException
- if no value is present- Since:
- 10
-
orElseThrow
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.A method reference to the exception constructor with an empty argument list can be used as the supplier. For example,
IllegalStateException::new
- Type Parameters:
X
- Type of the exception to be thrown- Parameters:
exceptionSupplier
- the supplying function that produces an exception to be thrown- Returns:
- the value, if present
- Throws:
X
- if no value is presentNullPointerException
- if no value is present and the exception supplying function isnull
-
map
If a value is present, returns anOptionalValue
describing the result of applying the given mapping function to the value, otherwise returns an emptyOptionalValue
.- Type Parameters:
U
- The type of the value returned from the mapping function- Parameters:
mapper
- the mapping function to apply to a value, if present- Returns:
- an
OptionalValue
describing the result of applying a mapping function to the value of thisOptionalValue
, if a value is present, otherwise an emptyOptionalValue
- Throws:
NullPointerException
- if the mapping function isnull
-
equals
Indicates whether some other object is "equal to" thisOptional
. The other object is considered equal if:- it is also an
OptionalValue
and; - both instances have no value set or;
- the present values are "equal to" each other via
equals()
.
- it is also an
-
hashCode
public int hashCode()Returns the hash code of the value, if present, otherwise0
(zero) if no value is present. -
toString
Returns a non-empty string representation of thisOptionalValue
suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.If a value is set the result must include its string representation in the result. undefined and defined (set)
OptionalValue
s must be unambiguously differentiable.
-