io.sarl.lang.core.util
Class MutableOptional<T>
Type Parameters:
T - the type of the data that is stored in the optional.
final class MutableOptional<T>
extends java.lang.Object
A specific implementation of a Optional that allows to change its value. This class is thread-safe. For an implementation that is not thread-safe, the class OutParameter is provided. It has the similar features as this mutable optional but without synchronized code.
Parameters:
<T> - the type of the data that is stored in the optional.
Maven Group Identifier:
io.sarl.lang
Maven Artifact Identifier:
core
Since:
0.15
See:
OutParameter
Action Summary
Modifier and type Action and description
static MutableOptional<T> empty
Returns an empty MutableOptional instance.
boolean equals(Object)
Indicates whether some other object is "equal to" this Optional.
MutableOptional<T> filter(Predicate<T>)
If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty MutableOptional.
MutableOptional<T> flatMap(Function<T,R>)
If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty Optional.
T get
If a value is present, returns the value, otherwise throws NoSuchElementException.
int hashCode
Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
void ifPresent(Consumer<T>)
If a value is present, performs the given action with the value, otherwise does nothing.
void ifPresentOrElse(Consumer<T>,Runnable)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
boolean isEmpty
If a value is not present, returns true, otherwise false.
boolean isPresent
If a value is present, returns true, otherwise false.
MutableOptional<T> map(Function<T,R>)
If a value is present, returns an MutableOptional describing the result of applying the given mapping function to the value, otherwise returns an empty Optional.
static MutableOptional<T> of(T)
Returns an empty MutableOptional instance.
MutableOptional<T> or(Supplier<T>)
If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
T orElse(T)
If a value is present, returns the value, otherwise returns other.
T orElseGet(Supplier<T>)
If a value is present, returns the value, otherwise returns the result produced by the supplying function.
T orElseThrow
If a value is present, returns the value, otherwise throws NoSuchElementException.
T orElseThrow(Supplier<T>)
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
void set(T)
Change the value stored in this MutableOptional.
java.lang.String toString
Returns a non-empty string representation of this Optional suitable for debugging.
Action Details
empty
def empty : MutableOptional<T>

with T

Returns an empty MutableOptional instance. No value is present for this MutableOptional.

Though it may be tempting to do so, avoid testing if an object is empty by comparing with == or != against instances returned by Optional.empty(). There is no guarantee that it is a singleton. Instead, use isEmpty() or isPresent().
Parameters:
<T> - The type of the non-existent value
Returns:
an empty MutableOptional
equals(Object)
def equals(Object) : boolean
Indicates whether some other object is "equal to" this Optional. The other object is considered equal if:
    Parameters:
    obj - an object to be tested for equality
    Returns:
    true if the other object is "equal to" this object otherwise false
    filter(Predicate<T>)
    def filter(Predicate<T>) : MutableOptional<T>
    If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty MutableOptional.
    Parameters:
    predicate - the predicate to apply to a value, if present
    Returns:
    an Optional describing the value of this Optional , if a value is present and the value matches the given predicate, otherwise an empty Optional
    Throws exception:
    • java.lang.NullPointerException: if the predicate is null
    flatMap(Function<T,R>)
    def flatMap(Function<T,R>) : MutableOptional<T>

    with U

    If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty Optional.

    This method is similar to map(Function), but the mapping function is one whose result is already an Optional, and if invoked, flatMap does not wrap it within an additional Optional.
    Parameters:
    <U> - The type of value of the Optional returned by the mapping function
    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
    Throws exception:
    • java.lang.NullPointerException: if the mapping function is null or returns a null result
    get
    def get : T
    If a value is present, returns the value, otherwise throws NoSuchElementException.

    The preferred alternative to this method is orElseThrow().
    Returns:
    the non- null value described by this Optional
    Throws exception:
    • : if no value is present
    hashCode
    def hashCode : int
    Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
    Returns:
    hash code value of the present value or 0 if no value is present
    ifPresent(Consumer<T>)
    def ifPresent(Consumer<T>)
    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 exception:
    • java.lang.NullPointerException: if value is present and the given action is null
    ifPresentOrElse(Consumer<T>,Runnable)
    def ifPresentOrElse(Consumer<T>,Runnable)
    If a value is present, 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 present
    emptyAction - the empty-based action to be performed, if no value is present
    Throws exception:
    • java.lang.NullPointerException: if a value is present and the given action is null , or no value is present and the given empty-based action is null .
    isEmpty
    def isEmpty : boolean
    If a value is not present, returns true, otherwise false.
    Returns:
    true if a value is not present, otherwise false
    isPresent
    def isPresent : boolean
    If a value is present, returns true, otherwise false.
    Returns:
    true if a value is present, otherwise false
    map(Function<T,R>)
    def map(Function<T,R>) : MutableOptional<T>

    with U

    If a value is present, returns an MutableOptional describing the result of applying the given mapping function to the value, otherwise returns an empty Optional.

    If the mapping function returns a null result then this method returns an empty Optional.

    This method supports post-processing on Optional values, without the need to explicitly check for a return status. For example, the following code traverses a stream of URIs, selects one that has not yet been processed, and creates a path from that URI, returning an MutableOptional<Path>:
    br     MutableOptional<Path> p =br         uris.stream().filter(uri -> !isProcessedYet(uri))br                       .findFirst()br                       .map(Paths::get);br 
    Here, findFirst returns an MutableOptional<URI>, and then map returns an MutableOptional<Path> for the desired URI if one exists.
    Parameters:
    <U> - The type of the value returned from the mapping function
    mapper - the mapping function to apply to a value, if present
    Returns:
    an MutableOptional describing the result of applying a mapping function to the value of this MutableOptional , if a value is present, otherwise an empty MutableOptional
    Throws exception:
    • java.lang.NullPointerException: if the mapping function is null
    of(T)
    def of(T) : MutableOptional<T>

    with T

    Returns an empty MutableOptional instance. No value is present for this MutableOptional.

    Though it may be tempting to do so, avoid testing if an object is empty by comparing with == or != against instances returned by Optional.empty(). There is no guarantee that it is a singleton. Instead, use isEmpty() or isPresent().
    Parameters:
    <T> - The type of the non-existent value
    value - the value to put in the MutableOptional.
    Returns:
    an empty MutableOptional
    or(Supplier<T>)
    def or(Supplier<T>) : MutableOptional<T>
    If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
    Parameters:
    supplier - the supplying function that produces an Optional to be returned
    Returns:
    returns an Optional describing the value of this Optional , if a value is present, otherwise an Optional produced by the supplying function.
    Throws exception:
    • java.lang.NullPointerException: if the supplying function is null or produces a null result.
    orElse(T)
    def orElse(T) : T
    If a value is present, returns the value, otherwise returns other.
    Parameters:
    other - the value to be returned, if no value is present. May be null.
    Returns:
    the value, if present, otherwise other
    orElseGet(Supplier<T>)
    def orElseGet(Supplier<T>) : T
    If a value is present, 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 exception:
    • java.lang.NullPointerException: if no value is present and the supplying function is null
    orElseThrow
    def orElseThrow : T
    If a value is present, returns the value, otherwise throws NoSuchElementException.
    Returns:
    the non- null value described by this Optional
    Throws exception:
    • : if no value is present.
    orElseThrow(Supplier<T>)
    def orElseThrow(Supplier<T>) : T

    with X extends java.lang.Throwable

    throws X

    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
    Parameters:
    <X> - Type of the exception to be thrown
    exceptionSupplier - the supplying function that produces an exception to be thrown
    Returns:
    the value, if present
    Throws exception:
    • : if no value is present
    • java.lang.NullPointerException: if no value is present and the exception supplying function is null
    set(T)
    def set(T)
    Change the value stored in this MutableOptional.
    Parameters:
    value - the new value. If it is null, then the MutableOptional becomes empty.
    toString
    def toString : java.lang.String
    Returns a non-empty string representation of this Optional suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions. If a value is present the result must include its string representation in the result. Empty and present Optionals must be unambiguously differentiable.
    Returns:
    the string representation of this instance