Class OptionalValue<T>

java.lang.Object
com.sinch.sdk.core.models.OptionalValue<T>
Type Parameters:
T - the type of value

public final class OptionalValue<T> extends Object
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 Type
    Method
    Description
    static <T> OptionalValue<T>
    Returns an empty OptionalValue instance.
    boolean
    Indicates whether some other object is "equal to" this Optional.
    get()
    If a value is present, returns the value, otherwise throws NoSuchElementException.
    int
    Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
    void
    ifPresent(Consumer<? super T> action)
    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, returns true, otherwise false.
    map(Function<? super T,? extends U> mapper)
    If a value is present, returns an OptionalValue describing the result of applying the given mapping function to the value, otherwise returns an empty OptionalValue.
    static <T> OptionalValue<T>
    of(T value)
    Returns an OptionalValue describing the given value.
    orElse(T other)
    If a value is set, returns the value, otherwise returns other.
    orElseGet(Supplier<? extends T> supplier)
    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 throws NoSuchElementException.
    <X extends Throwable>
    T
    orElseThrow(Supplier<? extends X> exceptionSupplier)
    If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
    Returns a non-empty string representation of this OptionalValue suitable for debugging.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • empty

      public static <T> OptionalValue<T> empty()
      Returns an empty OptionalValue instance. No value is present for this Optional.

      Though it may be tempting to do so, avoid testing if an object is empty by comparing with == against instances returned by Optional.empty(). There is no guarantee that it is a singleton. Instead, use isPresent().

      Type Parameters:
      T - The type of the non-existent value
      Returns:
      an empty OptionalValue
    • of

      public static <T> OptionalValue<T> of(T value)
      Returns an OptionalValue 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

      public T get()
      If a value is present, returns the value, otherwise throws NoSuchElementException.

      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, returns true, otherwise false.
      Returns:
      true if a value is present, otherwise false
    • ifPresent

      public void ifPresent(Consumer<? super T> action)
      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 is null
    • ifPresentOrElse

      public 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.
      Parameters:
      action - the action to be performed, if a value is set
      emptyAction - the empty-based action to be performed, if no value is present
      Throws:
      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.
      Since:
      9
    • orElse

      public T orElse(T other)
      If a value is set, 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

      public T orElseGet(Supplier<? extends T> supplier)
      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 is null
    • orElseThrow

      public T orElseThrow()
      If a value is set, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the non-null value described by this Optional
      Throws:
      NoSuchElementException - if no value is present
      Since:
      10
    • orElseThrow

      public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) 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

      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 present
      NullPointerException - if no value is present and the exception supplying function is null
    • map

      public <U> OptionalValue<U> map(Function<? super T,? extends U> mapper)
      If a value is present, returns an OptionalValue describing the result of applying the given mapping function to the value, otherwise returns an empty OptionalValue.
      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 this OptionalValue, if a value is present, otherwise an empty OptionalValue
      Throws:
      NullPointerException - if the mapping function is null
    • equals

      public boolean equals(Object obj)
      Indicates whether some other object is "equal to" this Optional. 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().
      Overrides:
      equals in class Object
      Parameters:
      obj - an object to be tested for equality
      Returns:
      true if the other object is "equal to" this object otherwise false
    • hashCode

      public int hashCode()
      Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
      Overrides:
      hashCode in class Object
      Returns:
      hash code value of the present value or 0 if no value is present
    • toString

      public String toString()
      Returns a non-empty string representation of this OptionalValue 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) OptionalValues must be unambiguously differentiable.

      Overrides:
      toString in class Object
      Returns:
      the string representation of this instance