Package org.silverpeas.kernel.util
Class Mutable<T>
java.lang.Object
org.silverpeas.kernel.util.Mutable<T>
A mutable value wrapper which may or may not contain a non-null value.
If a value is present, isPresent() will return true and get() will return
the value.
Additional methods that depend on the presence or absence of a contained
value are provided, such as orElse() (return a default value if value not
present) and ifPresent() (execute a block of code if the value is
present).
- Author:
- Yohann Chastagnier
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Mutable<T> empty()Returns an emptyMutable.booleanIndicates whether some other object is "equal to" this Mutable.If a value is present, and the value matches the given predicate, return anMutabledescribing the value, otherwise return an emptyMutable.<U> Mutable<U> If a value is present, apply the providedMutable-bearing mapping function to it, return that result, otherwise return an emptyMutable.get()If a value is present in thisMutable, returns the value, otherwise throwsNoSuchElementException.inthashCode()Returns the hash code value of the present value, if any, or 0 (zero) if no value is present.voidIf a value is present, invoke the specified consumer with the value, otherwise do nothing.booleanIndicates id the given value is equal to the one of theMutableinstance.booleanisEmpty()Returntrueif no value is present, otherwisefalse.booleanReturntrueif there is a value present, otherwisefalse.<U> Mutable<U> If a value is present, apply the provided mapping function to it, and if the result is non-null, return anMutabledescribing the result.static <T> Mutable<T> of(T value) Returns anMutablewith the specified value.static <T> Mutable<T> ofNullable(T value) Returns anMutablewith the specified value which can be null.Return the value if present, otherwise returnother.Return the value if present, otherwise invokeotherand return the result of that invocation.orElseThrow(Supplier<? extends X> exceptionSupplier) Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.voidSets the value of theMutableinstance.toString()Returns a non-empty string representation of this Mutable suitable for debugging.
-
Method Details
-
of
Returns anMutablewith the specified value.- Type Parameters:
T- the class of the value- Parameters:
value- the value to be present, which must be non-null- Returns:
- an
Mutablewith the value present - Throws:
NullPointerException- if value is null
-
ofNullable
Returns anMutablewith the specified value which can be null.- Type Parameters:
T- the class of the value- Parameters:
value- the value to be present, which must be null- Returns:
- an
Mutablewith the value present
-
empty
Returns an emptyMutable. Its value is by default null.- Type Parameters:
T- class of the value.- Returns:
- a
Mutablewith a null value.
-
get
If a value is present in thisMutable, returns the value, otherwise throwsNoSuchElementException.- Returns:
- the non-null value held by this
Mutable - Throws:
NoSuchElementException- if there is no value present- See Also:
-
set
Sets the value of theMutableinstance.- Parameters:
value- the value to set.
-
is
Indicates id the given value is equal to the one of theMutableinstance.- Parameters:
value- the value to verify.- Returns:
- true if the value is equal to the wrapped one in this mutable. False otherwise.
-
isPresent
public boolean isPresent()Returntrueif there is a value present, otherwisefalse.- Returns:
trueif there is a value present, otherwisefalse
-
isEmpty
public boolean isEmpty()Returntrueif no value is present, otherwisefalse.- Returns:
trueif there is no value, otherwisefalse
-
ifPresent
If a value is present, invoke the specified consumer with the value, otherwise do nothing.- Parameters:
consumer- block to be executed if a value is present- Throws:
NullPointerException- if value is present andconsumeris null
-
filter
If a value is present, and the value matches the given predicate, return anMutabledescribing the value, otherwise return an emptyMutable.- Parameters:
predicate- a predicate to apply to the value, if present- Returns:
- an
Mutabledescribing the value of thisMutableif a value is present and the value matches the given predicate, otherwise an emptyMutable - Throws:
NullPointerException- if the predicate is null
-
map
If a value is present, apply the provided mapping function to it, and if the result is non-null, return anMutabledescribing the result. Otherwise return an emptyMutable.- Type Parameters:
U- The type of the result of the mapping function- Parameters:
mapper- a mapping function to apply to the value, if present- Returns:
- an
Mutabledescribing the result of applying a mapping function to the value of thisMutable, if a value is present, otherwise an emptyMutable - Throws:
NullPointerException- if the mapping function is null- API Note:
- This method supports post-processing on mutable values, without the need to explicitly
check for a return status. For example, the following code traverses a stream of file names,
selects one that has not yet been processed, and then opens that file, returning an
Mutable<FileInputStream>:
Mutable<FileInputStream> fis = names.stream().filter(name -> !isProcessedYet(name)) .findFirst() .map(name -> new FileInputStream(name));Here,
findFirstreturns anMutable<String>, and thenmapreturns anMutable<FileInputStream>for the desired file if one exists.
-
flatMap
If a value is present, apply the providedMutable-bearing mapping function to it, return that result, otherwise return an emptyMutable. This method is similar tomap(Function), but the provided mapper is one whose result is already anMutable, and if invoked,flatMapdoes not wrap it with an additionalMutable.- Type Parameters:
U- The type parameter to theMutablereturned by- Parameters:
mapper- a mapping function to apply to the value, if present the mapping function- Returns:
- the result of applying an
Mutable-bearing mapping function to the value of thisMutable, if a value is present, otherwise an emptyMutable - Throws:
NullPointerException- if the mapping function is null or returns a null result
-
orElse
Return the value if present, otherwise returnother.- Parameters:
other- the value to be returned if there is no value present, may be null- Returns:
- the value, if present, otherwise
other
-
orElseGet
Return the value if present, otherwise invokeotherand return the result of that invocation.- Parameters:
other- aSupplierwhose result is returned if no value is present- Returns:
- the value if present otherwise the result of
other.get() - Throws:
NullPointerException- if value is not present andotheris null
-
orElseThrow
Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.- Type Parameters:
X- Type of the exception to be thrown- Parameters:
exceptionSupplier- The supplier which will return the exception to be thrown- Returns:
- the present value
- Throws:
X- if there is no value presentNullPointerException- if no value is present andexceptionSupplieris null- API Note:
- A method reference to the exception constructor with an empty argument list can be
used as the supplier. For example,
InvalidStateException::new
-
equals
Indicates whether some other object is "equal to" this Mutable. The other object is considered equal if:- it is also an
Mutableand; - both instances have no value present or;
- the present values are "equal to" each other via
equals().
- it is also an
-
hashCode
public int hashCode()Returns the hash code value of the present value, if any, or 0 (zero) if no value is present. -
toString
Returns a non-empty string representation of this Mutable suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.
-