Class Choice<T>

java.lang.Object
com.google.errorprone.refaster.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:
lowasser@google.com (Louis Wasserman)
  • Method Summary

    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.
    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.
    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>
    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>
     
    static <T> Choice<T>
    The empty Choice.
    static <T> Choice<T>
    of(T t)
    Returns a Choice with only one option, 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.
     
    <R> Choice<R>
    transform(com.google.common.base.Function<? super T,R> function)
    Maps the choices with the specified function.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • 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.
    • from

      public static <T> Choice<T> from(Collection<T> choices)
    • 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.
    • iterator

      @ForOverride protected abstract Iterator<T> iterator()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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.