Package it.polimi.ingsw.Misc
Class OptionalValue<T>
java.lang.Object
it.polimi.ingsw.Misc.OptionalValue<T>
- Type Parameters:
T
- the internal type of the OptionalValue
- All Implemented Interfaces:
Serializable
This class takes inspiration from
Optional
but adds serialization support and foregoes some
lesser used methods in some instances.
While Optional
was never meant to be used as a persistent value in memory, the layer of abstraction
it provides over null values was valuable enough to warrant a rewrite into an object that could be Serialized and shared
over the net.- See Also:
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprivate
Private constructor for the objectprivate
OptionalValue
(T value) Private constructor for the object -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> OptionalValue<T>
empty()
Get an empty value instanceboolean
Indicates whether some other object is "equal to" this OptionalValue.<U> OptionalValue<U>
flatMap
(Function<? super T, OptionalValue<U>> mapper) If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty OptionalValue.get()
Retrieve the inner valueint
hashCode()
Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.void
If a value is present, run the provided consumer over itboolean
isEmpty()
Check if no value is present inside the optionalboolean
Check if a value is present inside the optionalstatic <T> OptionalValue<T>
of
(T element) Creates a new wrapping around a non null valuestatic <T> OptionalValue<T>
ofNullable
(T element) Creates a new wrapping around a valueRetrieve the inner value
-
Field Details
-
value
-
-
Constructor Details
-
OptionalValue
private OptionalValue()Private constructor for the object -
OptionalValue
Private constructor for the object
-
-
Method Details
-
of
Creates a new wrapping around a non null value- Type Parameters:
T
- the type of element stored inside the Optional- Parameters:
element
- non null element to wrap an optional value around- Returns:
- the
OptionalValue
containing element
-
ofNullable
Creates a new wrapping around a value- Type Parameters:
T
- The type of value stored inside the optional value- Parameters:
element
- possibly null value to wrap this object around- Returns:
- an empty
OptionalValue
if element is null, otherwise anOptionalValue
wrapping element
-
isEmpty
public boolean isEmpty()Check if no value is present inside the optional- Returns:
- true if the internal value is absent
-
isPresent
public boolean isPresent()Check if a value is present inside the optional- Returns:
- true if the internal value is present
-
get
Retrieve the inner value- Returns:
- the inner value of the optional, if present
- Throws:
NullPointerException
- if no inner value is present
-
orElse
Retrieve the inner value- Parameters:
otherwise
- the default value to return if no inner value is present- Returns:
- the inner value of the optional, if present. Otherwise returns the input value
-
ifPresent
If a value is present, run the provided consumer over it- Parameters:
consumer
- a function to run over the contained value (if present)
-
flatMap
If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty OptionalValue.- Type Parameters:
U
- The type of value of the Optional returned by the mapping function- Parameters:
mapper
- the mapping function to apply to a value, if present- Returns:
- the result of applying an Optional-bearing mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
-
empty
Get an empty value instance- Type Parameters:
T
- The type of value stored inside the optional value- Returns:
- an empty
OptionalValue
-
hashCode
public int hashCode()Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present. -
equals
Indicates whether some other object is "equal to" this OptionalValue. The other object is considered equal if:it is also an Optional and; both instances have no value present or; the present values are "equal to" each other via equals().
-