Class None<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 None<T>
    extends java.lang.Object
    implements Option<T>, java.io.Serializable
    Represents an empty Option.
    The None is a replacement for null values representing a non-existing value.
    One can consider None as a collection of size 0.
    Instances of None should not be created directly, rather use the factory methods provided on Option. Since None anyways cannot represent a value it is preferable to use it as a singleton saving unnecessary instance creation.
    For examples on usage refer to the documentation for Option.
    Since:
    1.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      None()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object other)
      Returns true if other is a None as None is stateless comparing it to some other None is therefore always the same, false otherwise.
      boolean exists​(java.util.function.Predicate<T> predicate)
      Always returns false.
      <R> Option<R> flatMap​(ThrowableFunction1<T,​Option<R>> function)
      Always returns this.
      T get()
      Always throws NoSuchElementException since None cannot per definition hold any value.
      T getOrElse​(java.util.function.Supplier<T> supplier)
      Always the value provided by the supplier.
      int hashCode()
      Always returns 0 as None is stateless and has no value.
      java.util.Iterator<T> iterator()
      Always returns an empty iterator.
      <R> Option<R> map​(ThrowableFunction1<T,​R> function)
      Always returns this.
      Option<T> orElse​(java.util.function.Supplier<Option<T>> supplier)
      Always the value provided by the supplier.
      java.util.stream.Stream<T> stream()
      Always returns an empty stream.
      <R> Either<T,​R> toLeft​(java.util.function.Supplier<R> right)
      Returns a Right containing the value from the provided supplier.
      <L> Either<L,​T> toRight​(java.util.function.Supplier<L> left)
      Returns a Left containing the value from the provided supplier.
      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

      • None

        public None()
    • Method Detail

      • get

        public T get()
        Always throws NoSuchElementException since None cannot per definition hold any 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> supplier)
        Always the value provided by the supplier.
        Specified by:
        getOrElse in interface Option<T>
        Parameters:
        supplier - 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
      • exists

        public boolean exists​(java.util.function.Predicate<T> predicate)
        Always returns false.
        I.e. the predicate is never used as None represents nothing/no value.
        Specified by:
        exists in interface Option<T>
        Parameters:
        predicate - The predicate
        Returns:
        If the predicate matches
        Since:
        1.0
      • map

        public <R> Option<R> map​(ThrowableFunction1<T,​R> function)
        Always returns this.
        I.e. the function is never used as None represents nothing/no value.
        Specified by:
        map 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.0
      • flatMap

        public <R> Option<R> flatMap​(ThrowableFunction1<T,​Option<R>> function)
        Always returns this.
        I.e. the function is never used as None represents nothing/no 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, 1.0
      • orElse

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

        public <R> Either<T,​R> toLeft​(java.util.function.Supplier<R> right)
        Returns a Right containing the value from the provided supplier.
        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, 1.0
      • toRight

        public <L> Either<L,​T> toRight​(java.util.function.Supplier<L> left)
        Returns a Left containing the value from the provided supplier.
        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, 1.0
      • equals

        public boolean equals​(java.lang.Object other)
        Returns true if other is a None as None is stateless comparing it to some other None is therefore always the same, false otherwise.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - The other object to compare to
        Returns:
        true if other is None, false otherwise
        Since:
        1.0
      • hashCode

        public int hashCode()
        Always returns 0 as None is stateless and has no value.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        0
        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()
        Always returns an empty iterator.
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        An empty iterator
        Since:
        1.0
      • stream

        public final java.util.stream.Stream<T> stream()
        Always returns an empty stream.
        Returns:
        An empty stream
        Since:
        1.0