Package com.google.errorprone.refaster
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:
- [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 ifcondition
, otherwise the empty choice.static <T> Choice<T>
condition(boolean condition, T t)
Returns aChoice
witht
as an option ifcondition
, and no options otherwise.Choice<T>
condition(com.google.common.base.Predicate<? super T> predicate)
Filters the choices to those that satisfy the providedPredicate
.com.google.common.base.Optional<T>
first()
Returns the first valid option from thisChoice
.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 emptyChoice
.static <T> Choice<T>
of(T t)
Returns aChoice
with only one option,t
.Choice<T>
or(Choice<T> other)
Returns a choice of the options from thisChoice
or fromother
.<R> Choice<R>
thenChoose(com.google.common.base.Function<? super T,Choice<R>> function)
Returns all the choices obtained by choosing from thisChoice
and then choosing from theChoice
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 thisChoice
and yielding a presentOptional
.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 emptyChoice
.
-
of
public static <T> Choice<T> of(T t)
Returns aChoice
with only one option,t
.
-
condition
public static <T> Choice<T> condition(boolean condition, T t)
Returns aChoice
witht
as an option ifcondition
, 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()
-
first
public com.google.common.base.Optional<T> first()
Returns the first valid option from thisChoice
.
-
thenChoose
public <R> Choice<R> thenChoose(com.google.common.base.Function<? super T,Choice<R>> function)
Returns all the choices obtained by choosing from thisChoice
and then choosing from theChoice
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 thisChoice
and yielding a presentOptional
.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 thisChoice
or fromother
.
-
condition
public Choice<T> condition(boolean condition)
Returns this choice ifcondition
, otherwise the empty choice.
-
-