Interface Option<T>

  • Type Parameters:
    T - The type of the value represented by this instance
    All Superinterfaces:
    java.lang.Iterable<T>
    All Known Implementing Classes:
    None, Some

    public interface Option<T>
    extends java.lang.Iterable<T>
    Represents optional values.
    Instances of Option are either an instance of Some or None.
    Some holds a non-null value whilst None holds no value.
    The primary use case is to replace ugly null-checks.
    Consider the method:
     SomeData getDataIfExists(SomeInput input) {
       if(haveData(input) {
          return getSomeDataFromInternalStorage();
       }
       return null;
     }
     
    The above method causes the user to do null-checks in case the method returned a proper value or not.
    A neater way would be:
     Option<SomeData> getDataIfExists(SomeInput input) {
       if(haveData(input) {
          return new Some<>(getSomeDataFromInternalStorage());
       }
       return Option.None();
     }
     
    Another example is the usage of Maps.
    Performing a get on a Map will either yield the value of the key or null if no such key existed.
    Consider the Map:
    Map<String, SomeData> map = ....

    To avoid null checks one could do like so:
    Option<SomeData> option = Option.apply(map.get("someKey"));
    Now you have a guaranteed non-null value with which you can work with without performing constant null-checks.
    Statically importing methods from the companion class (OptionCompanion) to Option one can get that Scala feel of declaration.
     import static javascalautils.OptionCompanion.Option;
    
     Option<String> option = Option(map.get("someKey"));
     
    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static None DEFAULT_NONE
      This is a singleton None since it anyways cannot represent a state.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      static <T> Option<T> apply​(T value)
      Creates an instance of Option.
      default java.util.Optional<T> asOptional()
      Converts this Option to a corresponding Optional.
      default boolean contains​(T other)
      Returns true if this is a Some containing the provided object, else false.
      default int count()
      Returns the count which means 1 for nonempty Option's and 0 for empty.
      static <T> Option<T> empty()
      Returns an empty Option.
      boolean exists​(java.util.function.Predicate<T> predicate)
      Returns true if this option is nonempty and the predicate p returns true when applied to this Option's value.
      default Option<T> filter​(java.util.function.Predicate<T> predicate)
      Returns this Option if it is nonempty and applying the predicate p to this Option's value returns true.
      default Option<T> filterNot​(java.util.function.Predicate<T> predicate)
      Returns this Option if it is nonempty and applying the predicate p to this Option's value returns false.
      <R> Option<R> flatMap​(ThrowableFunction1<T,​Option<R>> function)
      Returns an Option consisting of the result of applying the given function to the current Some.
      default boolean forall​(java.util.function.Predicate<T> predicate)
      Returns true if the Option is nonempty and the predicate holds true, else false.
      T get()
      Returns this Option's value if such exists, else NoSuchElementException is raised.
      T getOrElse​(java.util.function.Supplier<T> supplier)
      Returns this Option's value if such exists, else the value provided by the supplier.
      default boolean isDefined()
      Returns true if the option is an instance of Some, false otherwise.
      default boolean isEmpty()
      Returns true if the option is an instance of None, false otherwise
      java.util.Iterator<T> iterator()
      Returns the Option's value in an Iterator if it is nonempty, or an empty Iterator if it is empty.
      <R> Option<R> map​(ThrowableFunction1<T,​R> function)
      Returns an Option consisting of the result of applying the given function to the current Some.
      static <T> Option<T> None()
      Returns an empty Option.
      static <T> Option<T> ofOptional​(java.util.Optional<T> optional)
      Converts the Optional to a corresponding Option.
      Option<T> orElse​(java.util.function.Supplier<Option<T>> supplier)
      Returns this Option if it is nonempty, otherwise return the result of provided by the supplier.
      default T orNull()
      Returns the Option's value if it is nonempty, or null if it is empty.
      java.util.stream.Stream<T> stream()
      Returns the Option's value in a Stream if it is nonempty, or an empty Stream if it is empty.
      <R> Either<T,​R> toLeft​(java.util.function.Supplier<R> right)
      Returns the value of this Some as a Left, or in case of None a Right containing the value from the provided supplier.
      <L> Either<L,​T> toRight​(java.util.function.Supplier<L> left)
      Returns the value of this Some as a Right, or in case of None a Left containing the value from the provided supplier.
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • DEFAULT_NONE

        static final None DEFAULT_NONE
        This is a singleton None since it anyways cannot represent a state.
        Can also be accessed using empty()
    • Method Detail

      • apply

        static <T> Option<T> apply​(T value)
        Creates an instance of Option.
        If a null value is provided then None is returned, else Some containing the provided value.
        Type Parameters:
        T - The type for the value this Option represents
        Parameters:
        value - The value this Option shall represent
        Returns:
        The Option representing the provided value
        Since:
        1.0
      • empty

        static <T> Option<T> empty()
        Returns an empty Option.
        In practice this returns a singleton as it anyways cannot represent a value/state.
        Type Parameters:
        T - The type for the value this Option represents
        Returns:
        The default None instance
        Since:
        1.0
      • None

        static <T> Option<T> None()
        Returns an empty Option.
        This is the same as empty() but with the difference it provides a more Scala like feeling if the method is statically imported.
        One can use None() as if it was a apply method on a companion object in Scala.
        E.g.
         import static javascalautils.Option.None;
        
         Option<String> opt = None();
         
        Type Parameters:
        T - The type for the value this Option represents
        Returns:
        The default None instance
        Since:
        1.2
      • contains

        default boolean contains​(T other)
        Returns true if this is a Some containing the provided object, else false.
        Parameters:
        other - The other object to compare to
        Returns:
        If this Some contains the provided object
        Since:
        1.0
      • count

        default int count()
        Returns the count which means 1 for nonempty Option's and 0 for empty.
        Returns:
        The count
        Since:
        1.0
      • exists

        boolean exists​(java.util.function.Predicate<T> predicate)
        Returns true if this option is nonempty and the predicate p returns true when applied to this Option's value.
        Parameters:
        predicate - The predicate
        Returns:
        If the predicate matches
        Since:
        1.0
      • filter

        default Option<T> filter​(java.util.function.Predicate<T> predicate)
        Returns this Option if it is nonempty and applying the predicate p to this Option's value returns true.
        Parameters:
        predicate - The predicate
        Returns:
        The Option representing the match
        Since:
        1.0
      • filterNot

        default Option<T> filterNot​(java.util.function.Predicate<T> predicate)
        Returns this Option if it is nonempty and applying the predicate p to this Option's value returns false.
        Parameters:
        predicate - The predicate
        Returns:
        The Option representing the match
        Since:
        1.0
      • forall

        default boolean forall​(java.util.function.Predicate<T> predicate)
        Returns true if the Option is nonempty and the predicate holds true, else false.
        As an Option is a zero-or-one sized collection this is in an essence exactly the same as exists(Predicate).
        Parameters:
        predicate - The predicate
        Returns:
        If the predicate matches
        Since:
        1.0
      • get

        T get()
        Returns this Option's value if such exists, else NoSuchElementException is raised.
        Returns:
        The value of the Option
        Since:
        1.0
      • getOrElse

        T getOrElse​(java.util.function.Supplier<T> supplier)
        Returns this Option's value if such exists, else the value provided by the supplier.
        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
      • isDefined

        default boolean isDefined()
        Returns true if the option is an instance of Some, false otherwise.
        Returns:
        true this Option is a Some, else false
        Since:
        1.0
      • isEmpty

        default boolean isEmpty()
        Returns true if the option is an instance of None, false otherwise
        Returns:
        true this Option is a None, else false
        Since:
        1.0
      • iterator

        java.util.Iterator<T> iterator()
        Returns the Option's value in an Iterator if it is nonempty, or an empty Iterator if it is empty.
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        The iterator for the Option
        Since:
        1.0
      • map

        <R> Option<R> map​(ThrowableFunction1<T,​R> function)
        Returns an Option consisting of the result of applying the given function to the current Some.
        Applying map to None will always yield None.
        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

        <R> Option<R> flatMap​(ThrowableFunction1<T,​Option<R>> function)
        Returns an Option consisting of the result of applying the given function to the current Some.
        Applying map to None will always yield None.
        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

        Option<T> orElse​(java.util.function.Supplier<Option<T>> supplier)
        Returns this Option if it is nonempty, otherwise return the result of provided by the supplier.
        Parameters:
        supplier - The supplier to use in case of None
        Returns:
        This Option or the one provided by the supplier
        Since:
        1.0
      • orNull

        default T orNull()
        Returns the Option's value if it is nonempty, or null if it is empty.
        Returns:
        The value of the Option or null in case of None
        Since:
        1.0
      • stream

        java.util.stream.Stream<T> stream()
        Returns the Option's value in a Stream if it is nonempty, or an empty Stream if it is empty.
        Returns:
        The stream for the Option
        Since:
        1.0
      • toLeft

        <R> Either<T,​R> toLeft​(java.util.function.Supplier<R> right)
        Returns the value of this Some as a Left, or in case of None a Right containing the value from the provided supplier.
        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

        <L> Either<L,​T> toRight​(java.util.function.Supplier<L> left)
        Returns the value of this Some as a Right, or in case of None a Left containing the value from the provided supplier.
        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
      • asOptional

        default java.util.Optional<T> asOptional()
        Converts this Option to a corresponding Optional.
        Returns:
        The Optional instance
        Since:
        1.0
      • ofOptional

        static <T> Option<T> ofOptional​(java.util.Optional<T> optional)
        Converts the Optional to a corresponding Option.
        Type Parameters:
        T - The type for the value this Option represents
        Parameters:
        optional - The Optional to convert
        Returns:
        The Option for the provided Optional
        Since:
        1.0