S - first possible type for the value.T - second possible type for the value.public abstract class Either<S,T> extends Object
Either is of type Either.Left or Either.Right depending of the class of its value.
This class is useful when a class has two different states, each of which depends on a variable of a different type (for example a String name or Numeric id). While the normal pattern is to have two variables, only one of which is set, or two Optionals, only one of which is present, that pattern does not protect from having none or both variables set by programming errors. Instead, a single instance of Either can be used, which guarantees exactly one type will be set.
| Modifier and Type | Class and Description |
|---|---|
static class |
Either.Left<S,T>
An instance of
Either with value of type S. |
static class |
Either.Right<S,T>
An instance of
Either with value of type T. |
| Modifier and Type | Field and Description |
|---|---|
protected com.google.common.base.Optional<S> |
s |
protected com.google.common.base.Optional<T> |
t |
| Constructor and Description |
|---|
Either() |
| Modifier and Type | Method and Description |
|---|---|
Object |
get()
|
static <S,T> Either<S,T> |
left(S left)
Create an instance of
Either with value of type S. |
static <S,T> Either<S,T> |
right(T right)
Create an instance of
Either with value of type T. |
protected final com.google.common.base.Optional<S> s
protected final com.google.common.base.Optional<T> t
public static <S,T> Either<S,T> left(S left)
Either with value of type S.left - value of this instance.Either.Left.public static <S,T> Either<S,T> right(T right)
Either with value of type T.right - value of this instance.Either.Right.public Object get()
Either instance, returned as class Object. To get a strongly typed return,
check the specific type of Either and call Either.Left.getLeft() or Either.Right.getRight().Object.