Package com.swoval.functional
Class Either<L,R>
- java.lang.Object
-
- com.swoval.functional.Either<L,R>
-
- Type Parameters:
L
- The left valueR
- The right value
- Direct Known Subclasses:
Either.Left
,Either.Right
public abstract class Either<L,R> extends java.lang.Object
Represents a value that can be one of two types. Inspired by Either, it is right biased, but does not define all of the combinators that the scala version does.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Either.Left<L,R>
A left projectedEither
.static class
Either.NotLeftException
An error that is thrown if an attempt is made to project an Either toEither.Left
when the object is actually an instance ofEither.Right
.static class
Either.NotRightException
An error that is thrown if an attempt is made to project an Either toEither.Right
when the object is actually an instance ofEither.Left
.static class
Either.Right<L,R>
A right projectedEither
.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract boolean
equals(java.lang.Object other)
R
get()
Get the right projected value of the either.static <T> T
getOrElse(Either<?,? extends T> either, T t)
Get the right projected value of the either or a provided default value.abstract int
hashCode()
abstract boolean
isLeft()
Check whether this is a Left projection.abstract boolean
isRight()
Check whether this is a Right projection.static <L,R,T extends L>
Either<L,R>left(T value)
Returns a left projected either.static <L,R>
Either.Left<L,R>leftProjection(Either<L,R> either)
Returns the Left projection for the provided Either or throws an exception if the Either is actually an instance ofEither.Right
.static <L,R,T extends R>
Either<L,R>right(T value)
Returns a right projected either.static <L,R>
Either.Right<L,R>rightProjection(Either<L,R> either)
Returns the Right projection for the provided Either or throws an exception if the Either is actually an instance ofEither.Left
.
-
-
-
Method Detail
-
leftProjection
public static <L,R> Either.Left<L,R> leftProjection(Either<L,R> either) throws Either.NotLeftException
Returns the Left projection for the provided Either or throws an exception if the Either is actually an instance ofEither.Right
.- Type Parameters:
L
- the left type of the either.R
- the right type of the either.- Parameters:
either
- the either, assumed to be an instance of left, that will- Returns:
- a Left projection.
- Throws:
Either.NotLeftException
- if the value is aEither.Right
.
-
rightProjection
public static <L,R> Either.Right<L,R> rightProjection(Either<L,R> either) throws Either.NotRightException
Returns the Right projection for the provided Either or throws an exception if the Either is actually an instance ofEither.Left
.- Type Parameters:
L
- the left type of the either.R
- the right type of the either.- Parameters:
either
- the either, assumed to be an instance of left, that will- Returns:
- a Right projection.
- Throws:
Either.NotRightException
- if the value is aEither.Left
.
-
isLeft
public abstract boolean isLeft()
Check whether this is a Left projection.- Returns:
- true if this is a Reft projection
-
isRight
public abstract boolean isRight()
Check whether this is a Right projection.- Returns:
- true if this is a Right projection
-
get
public R get()
Get the right projected value of the either. This is unsafe to call without checking whether the value is a right first.- Returns:
- the wrapped value if is a right projection
- Throws:
Either.NotRightException
- if this is a left projection
-
getOrElse
public static <T> T getOrElse(Either<?,? extends T> either, T t)
Get the right projected value of the either or a provided default value.- Type Parameters:
T
- the default type- Parameters:
either
- the either from which the method extracts the result if it is aEither.Right
.t
- the default value- Returns:
- the wrapped value if this is a right projection, otherwise the default
-
hashCode
public abstract int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public abstract boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
left
public static <L,R,T extends L> Either<L,R> left(T value)
Returns a left projected either.- Type Parameters:
L
- the type of the left parameter of the resultR
- the type of the right parameter of the resultT
- a refinement type that allows us to wrap subtypes of L- Parameters:
value
- the value to wrap- Returns:
- A left projected either
-
right
public static <L,R,T extends R> Either<L,R> right(T value)
Returns a right projected either.- Type Parameters:
L
- the type of the left parameter of the resultR
- the type of the right parameter of the resultT
- a refinement type that allows us to wrap subtypes of R- Parameters:
value
- the value to wrap- Returns:
- a right projected either.
-
-