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

public class OptionalValue<T> extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    private final T
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor for the object
    private
    Private constructor for the object
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> OptionalValue<T>
    Get an empty value instance
    boolean
    Indicates whether some other object is "equal to" this OptionalValue.
    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 value
    int
    Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
    void
    ifPresent(Consumer<? super T> consumer)
    If a value is present, run the provided consumer over it
    boolean
    Check if no value is present inside the optional
    boolean
    Check if a value is present inside the optional
    static <T> OptionalValue<T>
    of(T element)
    Creates a new wrapping around a non null value
    static <T> OptionalValue<T>
    ofNullable(T element)
    Creates a new wrapping around a value
    orElse(T otherwise)
    Retrieve the inner value

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • value

      private final T value
  • Constructor Details

    • OptionalValue

      private OptionalValue()
      Private constructor for the object
    • OptionalValue

      private OptionalValue(T value)
      Private constructor for the object
  • Method Details

    • of

      public static <T> OptionalValue<T> of(T element)
      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

      public static <T> OptionalValue<T> ofNullable(T element)
      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 an OptionalValue 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

      public T get()
      Retrieve the inner value
      Returns:
      the inner value of the optional, if present
      Throws:
      NullPointerException - if no inner value is present
    • orElse

      public T orElse(T otherwise)
      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

      public void ifPresent(Consumer<? super T> consumer)
      If a value is present, run the provided consumer over it
      Parameters:
      consumer - a function to run over the contained value (if present)
    • flatMap

      public <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.
      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

      public static <T> OptionalValue<T> 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.
      Overrides:
      hashCode in class Object
      Returns:
      hash code value of the present value or 0 if no value is present
    • equals

      public boolean equals(Object o)
      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().
      Overrides:
      equals in class Object
      Parameters:
      o - the object to be tested for equality
      Returns:
      true if the two objects are considered equal