Package io.atlassian.fugue
Class Eithers
java.lang.Object
io.atlassian.fugue.Eithers
Utility functions for Eithers.
- Since:
- 1.2
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <L,R> Either <L, R> cond(boolean predicate, L left, R right) Creates an Either based on a boolean expression.static <L,R> Iterable <L> filterLeft(Iterable<Either<L, R>> it) static <L,R> Iterable <R> filterRight(Iterable<Either<L, R>> it) static <X extends Exception,A>
AgetOrThrow(Either<X, A> either) Simplifies extracting a value or throwing a checked exception from an Either.isLeft()A predicate that tests if the supplied either is a left.isRight()A predicate that tests if the supplied either is a right.A function that maps an either to an option of its left type.static <T> TExtracts an object from an Either, regardless of the side in which it is stored, provided both sides contain the same type.A function that maps an either to an option of its right type.sequenceLeft(Iterable<Either<L, R>> eithers) Collect the left values if there are only lefts, otherwise return the first right encountered.static <L,R, A, C>
Either<C, R> sequenceLeft(Iterable<Either<L, R>> eithers, Collector<L, A, C> collector) Collect the left values if there are only lefts, otherwise return the first right encountered.sequenceRight(Iterable<Either<L, R>> eithers) Collect the right values if there are only rights, otherwise return the first left encountered.static <L,R, A, C>
Either<L, C> sequenceRight(Iterable<Either<L, R>> eithers, Collector<R, A, C> collector) Collect the right values if there are only rights, otherwise return the first left encountered.toLeft()Function to convert from an value to aEither.Leftcontaining that value.Function to convert from a value to aEither.Leftcontaining that value.toLeft(L l) Supplier returning aEither.Left.Supplier returning aEither.Left.toRight()Function to convert from an value to aEither.Right.Function to convert from a value to aEither.Rightcontaining that value.Supplier returning aEither.Right.toRight(R r) Supplier returning aEither.Right.static <LL,L extends LL, R>
Either<LL, R> upcastLeft(Either<L, R> e) Upcasts aneitherof left type L to an either of left type LL, which is a super type of L, keeping the right type unchanged.static <L,RR, R extends RR>
Either<L, RR> upcastRight(Either<L, R> e) Upcasts aneitherof right type R to an either of right type RR, which is a super type of R, keeping the left type unchanged.
-
Constructor Details
-
Eithers
private Eithers()
-
-
Method Details
-
merge
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.- Type Parameters:
T- the type for both the LHS and the RHS- Parameters:
either- use whichever side holds the value to return- Returns:
- the value from whichever side holds it
-
cond
Creates an Either based on a boolean expression. If predicate is true, a Right will be returned containing the supplied right value; if it is false, a Left will be returned containing the supplied left value.- Type Parameters:
L- the LHS typeR- the RHS type- Parameters:
predicate- if predicate is true, a Right will be returned if it is false, a Left will be returned containing the supplied left value.left- the LHS valueright- the RHS value- Returns:
- an either with the appropriately selected value
-
getOrThrow
Simplifies extracting a value or throwing a checked exception from an Either.- Type Parameters:
X- the exception typeA- the value type- Parameters:
either- to extract from- Returns:
- the value from the RHS
- Throws:
X- the exception on the LHS
-
isLeft
A predicate that tests if the supplied either is a left.- Type Parameters:
L- the LHS typeR- the RHS type- Returns:
- the predicate testing left-hand-sidedness
-
isRight
A predicate that tests if the supplied either is a right.- Type Parameters:
L- the LHS typeR- the RHS type- Returns:
- the predicate testing right-hand-sidedness
-
leftMapper
A function that maps an either to an option of its left type. The Function will return a definedOptioncontaining the either's left value if {Either#isLeft()} is true, an undefinedOptionotherwise.- Type Parameters:
L- the LHS typeR- the RHS type- Returns:
- the function returning a defined option for left-hand-sided eithers
-
rightMapper
A function that maps an either to an option of its right type. The Function will return a definedOptioncontaining the either's right value if {Either#isRight()} is true, an undefinedOptionotherwise.- Type Parameters:
L- the LHS typeR- the RHS type- Returns:
- the function returning a defined option for right-hand-sided eithers
-
toLeft
Function to convert from an value to aEither.Leftcontaining that value.- Type Parameters:
L- left type.R- right type.- Returns:
- a
Functionreturning aEither.Left.
-
toLeft
Function to convert from a value to aEither.Leftcontaining that value. Allows hinting the correct types.- Type Parameters:
L- left type.R- right type.- Parameters:
leftType- expected left type.rightType- expected right type.- Returns:
- a
Functionreturning aEither.Left.
-
toLeft
Supplier returning aEither.Left.- Type Parameters:
L- left type.R- right type.- Parameters:
l- value to return inside the left.- Returns:
- a
Supplierreturning aEither.Left..
-
toLeft
Supplier returning aEither.Left. Allows hinting the correct right type.- Type Parameters:
L- left type.R- right type.- Parameters:
l- value to return inside the left.rightType- type hint for the right type of the either.- Returns:
- a
Supplierreturning aEither.Left.
-
toRight
Function to convert from an value to aEither.Right. Allows hinting the correct types.- Type Parameters:
L- left type.R- right type.- Returns:
- a
Functionreturning aEither.Right.
-
toRight
Function to convert from a value to aEither.Rightcontaining that value. Allows hinting the correct types.- Type Parameters:
L- left type.R- right type.- Parameters:
leftType- expected left type.rightType- expected right type.- Returns:
- a
Functionreturning aEither.Right.
-
toRight
Supplier returning aEither.Right.- Type Parameters:
L- left type.R- right type.- Parameters:
r- value to return inside the right.- Returns:
- a
Supplierreturning aEither.Right..
-
toRight
Supplier returning aEither.Right. Allows hinting the correct right type.- Type Parameters:
L- left type.R- right type.- Parameters:
leftType- type hint for the left type of the either.r- value to return inside the right.- Returns:
- a
Supplierreturning aEither.Right.
-
upcastLeft
Upcasts aneitherof left type L to an either of left type LL, which is a super type of L, keeping the right type unchanged.- Type Parameters:
LL- the super type of the contained left typeL- the base type to upcastR- the contained right type- Parameters:
e- the source either- Returns:
- an either of left type LL and right type R
- Since:
- 2.0
-
upcastRight
Upcasts aneitherof right type R to an either of right type RR, which is a super type of R, keeping the left type unchanged.- Type Parameters:
L- the contained left typeRR- the super type of the contained right typeR- the base type to upcast- Parameters:
e- the source either- Returns:
- an either of left type L and right type RR
- Since:
- 2.0
-
filterLeft
- Type Parameters:
L- the LHS typeR- the RHS type- Parameters:
it- iterable of eithers to filter and transform from- Returns:
- the left values contained in the contents of it
-
filterRight
- Type Parameters:
L- the LHS typeR- the RHS type- Parameters:
it- iterable of eithers to filter and transform from- Returns:
- the right values contained in the contents of it
-
sequenceRight
Collect the right values if there are only rights, otherwise return the first left encountered.- Type Parameters:
L- the LHS typeR- the RHS type- Parameters:
eithers- an Iterable of either values- Returns:
- either the iterable of right values, or the first left encountered.
-
sequenceRight
public static <L,R, Either<L,A, C> C> sequenceRight(Iterable<Either<L, R>> eithers, Collector<R, A, C> collector) Collect the right values if there are only rights, otherwise return the first left encountered.- Type Parameters:
L- the LHS typeR- the RHS typeA- The intermediate accumulator typeC- The result type- Parameters:
eithers- an Iterable of either valuescollector- result collector- Returns:
- either the iterable of right values, or the first left encountered.
- Since:
- 4.6.0
-
sequenceLeft
Collect the left values if there are only lefts, otherwise return the first right encountered.- Type Parameters:
L- the LHS typeR- the RHS type- Parameters:
eithers- an Iterable of either values- Returns:
- either the iterable of left values, or the first right encountered.
-
sequenceLeft
public static <L,R, Either<C,A, C> R> sequenceLeft(Iterable<Either<L, R>> eithers, Collector<L, A, C> collector) Collect the left values if there are only lefts, otherwise return the first right encountered.- Type Parameters:
L- the LHS typeR- the RHS typeA- The intermediate accumulator typeC- The result type- Parameters:
eithers- an Iterable of either valuescollector- result collector- Returns:
- either the iterable of left values, or the first right encountered.
- Since:
- 4.6.0
-