Class Choice<T>


  • public abstract class Choice<T>
    extends Object
    A representation of a choice with zero or more options, which may be evaluated lazily or strictly.

    This resembles the list monad in Haskell.

    Author:
    [email protected] (Louis Wasserman)
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Choice<T> any​(Collection<Choice<T>> choices)
      Returns a choice between any of the options from any of the specified choices.
      Choice<T> condition​(boolean condition)
      Returns this choice if condition, otherwise the empty choice.
      static <T> Choice<T> condition​(boolean condition, T t)
      Returns a Choice with t as an option if condition, and no options otherwise.
      Choice<T> condition​(com.google.common.base.Predicate<? super T> predicate)
      Filters the choices to those that satisfy the provided Predicate.
      com.google.common.base.Optional<T> first()
      Returns the first valid option from this Choice.
      static <T> Choice<T> from​(Collection<T> choices)  
      static <T> Choice<T> fromOptional​(com.google.common.base.Optional<T> optional)
      Returns a choice of the optional value, if it is present, or the empty choice if it is absent.
      protected abstract Iterator<T> iterator()  
      static <T> Choice<T> none()
      The empty Choice.
      static <T> Choice<T> of​(T t)
      Returns a Choice with only one option, t.
      Choice<T> or​(Choice<T> other)
      Returns a choice of the options from this Choice or from other.
      <R> Choice<R> thenChoose​(com.google.common.base.Function<? super T,​Choice<R>> function)
      Returns all the choices obtained by choosing from this Choice and then choosing from the Choice yielded by this function on the result.
      <R> Choice<R> thenOption​(com.google.common.base.Function<? super T,​com.google.common.base.Optional<R>> function)
      Returns all the choices obtained by choosing from this Choice and yielding a present Optional.
      String toString()  
      <R> Choice<R> transform​(com.google.common.base.Function<? super T,​R> function)
      Maps the choices with the specified function.
    • Method Detail

      • none

        public static <T> Choice<T> none()
        The empty Choice.
      • of

        public static <T> Choice<T> of​(T t)
        Returns a Choice with only one option, t.
      • condition

        public static <T> Choice<T> condition​(boolean condition,
                                              T t)
        Returns a Choice with t as an option if condition, and no options otherwise.
      • fromOptional

        public static <T> Choice<T> fromOptional​(com.google.common.base.Optional<T> optional)
        Returns a choice of the optional value, if it is present, or the empty choice if it is absent.
      • any

        public static <T> Choice<T> any​(Collection<Choice<T>> choices)
        Returns a choice between any of the options from any of the specified choices.
      • first

        public com.google.common.base.Optional<T> first()
        Returns the first valid option from this Choice.
      • thenChoose

        public <R> Choice<R> thenChoose​(com.google.common.base.Function<? super T,​Choice<R>> function)
        Returns all the choices obtained by choosing from this Choice and then choosing from the Choice yielded by this function on the result.

        The function may be applied lazily or immediately, at the discretion of the implementation.

        This is the monadic bind for Choice.

      • thenOption

        public <R> Choice<R> thenOption​(com.google.common.base.Function<? super T,​com.google.common.base.Optional<R>> function)
        Returns all the choices obtained by choosing from this Choice and yielding a present Optional.

        The function may be applied lazily or immediately, at the discretion of the implementation.

      • transform

        public <R> Choice<R> transform​(com.google.common.base.Function<? super T,​R> function)
        Maps the choices with the specified function.
      • or

        public Choice<T> or​(Choice<T> other)
        Returns a choice of the options from this Choice or from other.
      • condition

        public Choice<T> condition​(boolean condition)
        Returns this choice if condition, otherwise the empty choice.
      • condition

        public Choice<T> condition​(com.google.common.base.Predicate<? super T> predicate)
        Filters the choices to those that satisfy the provided Predicate.