Class Options


  • public class Options
    extends java.lang.Object
    Utility methods for working with options.
    Since:
    1.1
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Options()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <A,​B>
      Option<B>
      ap​(Option<A> oa, Option<java.util.function.Function<A,​B>> of)
      Applies an option of A to an option of a function with input type A and result type B and return an option of B.
      static <A> java.lang.Iterable<Option<A>> filterNone​(java.lang.Iterable<Option<A>> options)
      Filter out undefined options.
      static <A> Option<A> find​(java.lang.Iterable<Option<A>> options)
      Find the first option that isDefined, or if there aren't any, then none.
      static <A> java.lang.Iterable<A> flatten​(java.lang.Iterable<Option<A>> options)
      Flattens an Iterable of options into an iterable of the things, filtering out any nones.
      static <A,​B>
      java.util.function.Function<java.util.function.Function<A,​B>,​java.util.function.Function<Option<A>,​Option<B>>>
      lift()
      Returns a function that will lift a function that takes an A and returns a B into a function that takes an option of A and returns an option of B.
      static <A,​B>
      java.util.function.Function<Option<A>,​Option<B>>
      lift​(java.util.function.Function<A,​B> f)
      Lifts a function that takes an A and returns a B into a function that takes an option of A and returns an option of B.
      static <A> java.util.function.Predicate<Option<A>> lift​(java.util.function.Predicate<? super A> pred)
      Lifts a predicate that takes an A into a predicate that takes an option of A.
      static <A,​B,​C>
      java.util.function.Function<java.util.function.BiFunction<A,​B,​C>,​java.util.function.BiFunction<Option<A>,​Option<B>,​Option<C>>>
      lift2()
      Returns a function that will lift a function that takes an A and a B and returns a C into a function that takes an option of A and an option of B and returns an option of C.
      static <A,​B,​C>
      java.util.function.BiFunction<Option<A>,​Option<B>,​Option<C>>
      lift2​(java.util.function.BiFunction<A,​B,​C> f2)
      Lifts a function that takes an A and a B and returns a C into a function that takes an option of A and an option of B and returns an option of C.
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      nullSafe​(java.util.function.Function<A,​B> nullProducing)
      Turn a null producing function into one that returns an option instead.
      static <A> java.util.function.Function<A,​Option<A>> toOption()
      Function for wrapping values in a Some or None.
      static <AA,​A extends AA>
      Option<AA>
      upcast​(Option<A> o)
      Upcasts an option of type A to an option of its super type AA.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Options

        private Options()
    • Method Detail

      • find

        public static <A> Option<A> find​(java.lang.Iterable<Option<A>> options)
        Find the first option that isDefined, or if there aren't any, then none.
        Type Parameters:
        A - the contained type
        Parameters:
        options - an Iterable of options to search through
        Returns:
        the first defined option, or none if there aren't any
      • upcast

        public static <AA,​A extends AA> Option<AA> upcast​(Option<A> o)
        Upcasts an option of type A to an option of its super type AA.
        Type Parameters:
        AA - the super type of the contained type
        A - a A object.
        Parameters:
        o - the source option
        Returns:
        an option of the super type
        Since:
        2.0
      • lift

        public static <A,​B> java.util.function.Function<Option<A>,​Option<B>> lift​(java.util.function.Function<A,​B> f)
        Lifts a function that takes an A and returns a B into a function that takes an option of A and returns an option of B.
        Type Parameters:
        A - the input type of the original function
        B - the result type of the original function
        Parameters:
        f - the original function to be lifted, must not be null
        Returns:
        a function that takes an option of type A and returns an option of type B
        Since:
        2.0
      • lift

        public static <A,​B> java.util.function.Function<java.util.function.Function<A,​B>,​java.util.function.Function<Option<A>,​Option<B>>> lift()
        Returns a function that will lift a function that takes an A and returns a B into a function that takes an option of A and returns an option of B.
        Type Parameters:
        A - the input type of the function that can be lifted
        B - the result type of the function that can be lifted
        Returns:
        a function that can lift a function of input type A and result type B into Option
        Since:
        2.0
      • lift

        public static <A> java.util.function.Predicate<Option<A>> lift​(java.util.function.Predicate<? super A> pred)
        Lifts a predicate that takes an A into a predicate that takes an option of A.
        Type Parameters:
        A - the input type of the predicate
        Parameters:
        pred - the original predicate to be lifted, must not be null
        Returns:
        a predicate that takes an option of type A
        Since:
        2.2
      • ap

        public static <A,​B> Option<B> ap​(Option<A> oa,
                                               Option<java.util.function.Function<A,​B>> of)
        Applies an option of A to an option of a function with input type A and result type B and return an option of B.
        Type Parameters:
        A - the input type of the function wrapped in the option 'of'
        B - the result type of the function wrapped in the option 'of'
        Parameters:
        oa - an option of the argument to the function
        of - an option of a function that takes an A and returns a B
        Returns:
        an option of B
        Since:
        2.0
      • lift2

        public static <A,​B,​C> java.util.function.BiFunction<Option<A>,​Option<B>,​Option<C>> lift2​(java.util.function.BiFunction<A,​B,​C> f2)
        Lifts a function that takes an A and a B and returns a C into a function that takes an option of A and an option of B and returns an option of C.
        Type Parameters:
        A - the input type of the first argument of the original function
        B - the input type of the second argument of the original function
        C - the result type of the original function
        Parameters:
        f2 - the original function to be lifted
        Returns:
        a function that takes an option of type A and an option of B and returns an option of type C
        Since:
        2.0
      • lift2

        public static <A,​B,​C> java.util.function.Function<java.util.function.BiFunction<A,​B,​C>,​java.util.function.BiFunction<Option<A>,​Option<B>,​Option<C>>> lift2()
        Returns a function that will lift a function that takes an A and a B and returns a C into a function that takes an option of A and an option of B and returns an option of C.
        Type Parameters:
        A - the input type of the first argument of the function that can be lifted
        B - the input type of the second argument of the function that can be lifted
        C - the result type of the function that can be lifted
        Returns:
        a function that can lift a function of input type A and B and result type C into Option
        Since:
        2.0
      • filterNone

        public static <A> java.lang.Iterable<Option<A>> filterNone​(java.lang.Iterable<Option<A>> options)
        Filter out undefined options.
        Type Parameters:
        A - the contained type
        Parameters:
        options - many options that may or may not be defined
        Returns:
        the filtered options
      • flatten

        public static <A> java.lang.Iterable<A> flatten​(java.lang.Iterable<Option<A>> options)
        Flattens an Iterable of options into an iterable of the things, filtering out any nones.
        Type Parameters:
        A - the contained type
        Parameters:
        options - the iterable of options
        Returns:
        an Iterable of the contained type
      • toOption

        public static <A> java.util.function.Function<A,​Option<A>> toOption()
        Function for wrapping values in a Some or None.
        Type Parameters:
        A - the contained type
        Returns:
        a Function to wrap values
        Since:
        3.0
      • nullSafe

        public static <A,​B> java.util.function.Function<A,​Option<B>> nullSafe​(java.util.function.Function<A,​B> nullProducing)
        Turn a null producing function into one that returns an option instead.
        Type Parameters:
        A - input type to the function.
        B - output type of the function.
        Parameters:
        nullProducing - the function that may return null
        Returns:
        a function that turns nulls into None, and wraps non-null values in Some.
        Since:
        3.0