public abstract class

Either

extends Object
java.lang.Object
   ↳ com.atlassian.plugin.util.Either<L, R>

Class Overview

A class that acts as a container for a value of one of two types. An Either will be either Either.Left Left or Either.Right Right.

Checking which type an Either is can be done by calling the @isLeft() and isRight() methods.

Eithers can be used to express a success or failure case. By convention, Right is used to store the success value, (you can use the play on words "right" == "correct" as a mnemonic) and Left is used to store failure values (such as exceptions).

While this class is public and abstract it does not expose a constructor as only the concrete Left and Right subclasses are meant to be used.

Eithers are immutable, but do not force immutability on contained objects; if the contained objects are mutable then equals and hashcode methods should not be relied on.

Copied from Atlassian FAGE/FUGUE, to be retired when that is adopted.

Summary

Public Methods
static <L, R> Either<L, R> cond(boolean predicate, R right, L left)
Creates an Either based on a boolean expression.
abstract <V> V fold(Function<? super L, V> ifLeft, Function<? super R, V> ifRight)
Applies the function to the wrapped value, applying ifLeft it this is a Left and ifRight if this is a Right.
static <X extends Exception, A> A getOrThrow(Either<X, A> either)
Simplifies extracting a value or throwing a checked exception from an Either.
boolean isLeft()
boolean isRight()
static <L, R> Either<L, R> left(L left)
Option<L> left()
static <T> T merge(Either<T, T> either)
Extracts an object from an Either, regardless of the side in which it is stored, provided both sides contain the same type.
Option<R> right()
static <L, R> Either<L, R> right(R right)
abstract Either<R, L> swap()
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static Either<L, R> cond (boolean predicate, R right, L left)

Creates an Either based on a boolean expression. If predicate is true, a Right wil be returned containing the supplied right value; if it is false, a Left will be returned containing the supplied left value.

public abstract V fold (Function<? super L, V> ifLeft, Function<? super R, V> ifRight)

Applies the function to the wrapped value, applying ifLeft it this is a Left and ifRight if this is a Right.

Parameters
ifLeft the function to apply if this is a Left
ifRight the function to apply if this is a Right
Returns
  • the result of the applies function

public static A getOrThrow (Either<X, A> either)

Simplifies extracting a value or throwing a checked exception from an Either.

Parameters
either to extract from
Returns
  • the value from the RHS
Throws
the exception on the LHS
Exception

public boolean isLeft ()

public boolean isRight ()

public static Either<L, R> left (L left)

Parameters
left the value to be stored, must not be null
Returns
  • a Left containing the supplied value

public Option<L> left ()

Returns
  • an Option wrapping the left value of this either

public static T merge (Either<T, T> either)

Extracts an object from an Either, regardless of the side in which it is stored, provided both sides contain the same type. This method will never return null.

public Option<R> right ()

Returns
  • an Option wrapping the right value of this either

public static Either<L, R> right (R right)

Parameters
right the value to be stored, must not be null
Returns
  • a Right containing the supplied value

public abstract Either<R, L> swap ()

Returns
  • an Either that is a Left if this is a Right or a Right if this is a Left. The value remains the same.