Class Some<T>

  • Type Parameters:
    T - The type of the value represented by this instance
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<T>, Option<T>

    public final class Some<T>
    extends java.lang.Object
    implements Option<T>, java.io.Serializable
    Represents an Option holding a value.
    The instance of Some is guaranteed to keep a non-null value object.
    Null values are not allowed as it implies an instance of None.
    One can consider Some as a collection of size 1.

    Either use the implementation directly:
    Option<String> option = new Some<>("Peter is Great!");

    or use the factory/apply method from Option:
    Option<String> option = Option.apply("Peter is Great!");
    For examples on usage refer to the documentation for Option .
    Since:
    1.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Some​(T value)
      Creates an instance for the provided value.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object other)
      Returns true if the other object is Some containing a value that equals the value of this Some, else false.
      boolean exists​(java.util.function.Predicate<T> p)
      Returns true if the predicate matches the value.
      <R> Option<R> flatMap​(ThrowableFunction1<T,​Option<R>> function)
      Returns an Option consisting of the result of applying the given function to the current value.
      T get()
      Always returns the value for this instance.
      T getOrElse​(java.util.function.Supplier<T> s)
      Always returns the value for this instance.
      int hashCode()
      Returns the hashCode based on the value of this instance.
      java.util.Iterator<T> iterator()
      Returns an iterator containing the single value of this instance.
      <R> Option<R> map​(ThrowableFunction1<T,​R> f)
      Returns an Option containing the value of applying the given function to the current value.
      Option<T> orElse​(java.util.function.Supplier<Option<T>> s)
      Always returns this.
      java.util.stream.Stream<T> stream()
      Returns a stream of size one containing the value of this instance.
      <R> Either<T,​R> toLeft​(java.util.function.Supplier<R> right)
      Returns a Left containing the value of this instance.
      <L> Either<L,​T> toRight​(java.util.function.Supplier<L> left)
      Returns a Right containing the value of this instance.
      java.lang.String toString()
      Returns a String representation of the instance.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • Some

        public Some​(T value)
        Creates an instance for the provided value.
        Null objects are not allowed and will render an exception.
        Parameters:
        value - The value represented by this Some
        Since:
        1.0
    • Method Detail

      • exists

        public boolean exists​(java.util.function.Predicate<T> p)
        Returns true if the predicate matches the value.
        Specified by:
        exists in interface Option<T>
        Parameters:
        p - The predicate
        Returns:
        If the predicate matches
        Since:
        1.0
      • get

        public T get()
        Always returns the value for this instance.
        Guaranteed to return a non-null value.
        Specified by:
        get in interface Option<T>
        Returns:
        The value of the Option
        Since:
        1.0
      • getOrElse

        public T getOrElse​(java.util.function.Supplier<T> s)
        Always returns the value for this instance.
        Specified by:
        getOrElse in interface Option<T>
        Parameters:
        s - The supplier to use in case this is a None
        Returns:
        The value of the Option or the value provided by the supplier
        Since:
        1.0
      • map

        public <R> Option<R> map​(ThrowableFunction1<T,​R> f)
        Returns an Option containing the value of applying the given function to the current value.
        Specified by:
        map in interface Option<T>
        Type Parameters:
        R - The type for the return value from the function
        Parameters:
        f - The function to use
        Returns:
        The Option containing the mapped value
        Since:
        1.0
      • flatMap

        public <R> Option<R> flatMap​(ThrowableFunction1<T,​Option<R>> function)
        Returns an Option consisting of the result of applying the given function to the current value.
        Specified by:
        flatMap in interface Option<T>
        Type Parameters:
        R - The type for the return value from the function
        Parameters:
        function - The function to use
        Returns:
        The Option containing the mapped value
        Since:
        1.2
      • orElse

        public Option<T> orElse​(java.util.function.Supplier<Option<T>> s)
        Always returns this.
        Specified by:
        orElse in interface Option<T>
        Parameters:
        s - The supplier to use in case of None
        Returns:
        This Option or the one provided by the supplier
        Since:
        1.0
      • toLeft

        public <R> Either<T,​R> toLeft​(java.util.function.Supplier<R> right)
        Returns a Left containing the value of this instance.
        Specified by:
        toLeft in interface Option<T>
        Type Parameters:
        R - The type in case Right returned
        Parameters:
        right - The supplier to use in case this is a None
        Returns:
        The Either instance
        Since:
        1.4
      • toRight

        public <L> Either<L,​T> toRight​(java.util.function.Supplier<L> left)
        Returns a Right containing the value of this instance.
        Specified by:
        toRight in interface Option<T>
        Type Parameters:
        L - The type in case Left returned
        Parameters:
        left - The supplier to use in case this is a None
        Returns:
        The Either instance
        Since:
        1.4
      • equals

        public boolean equals​(java.lang.Object other)
        Returns true if the other object is Some containing a value that equals the value of this Some, else false.
        Overrides:
        equals in class java.lang.Object
        Since:
        1.0
      • hashCode

        public int hashCode()
        Returns the hashCode based on the value of this instance.
        Overrides:
        hashCode in class java.lang.Object
        Since:
        1.0
      • toString

        public java.lang.String toString()
        Returns a String representation of the instance.
        Overrides:
        toString in class java.lang.Object
        Since:
        1.0
      • iterator

        public final java.util.Iterator<T> iterator()
        Returns an iterator containing the single value of this instance.
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        An iterator containing the single value
        Since:
        1.0
      • stream

        public final java.util.stream.Stream<T> stream()
        Returns a stream of size one containing the value of this instance.
        Returns:
        A stream containing the single value
        Since:
        1.0