Package com.pulumi.core
Class Either<L,R>
- java.lang.Object
-
- com.pulumi.core.Either<L,R>
-
- Type Parameters:
L
- the type contained by theLeft
instanceR
- the type contained by theRight
instance
- All Implemented Interfaces:
java.io.Serializable
@CheckReturnValue public abstract class Either<L,R> extends java.lang.Object implements java.io.Serializable
Represents a value of one of two possible types (a disjoint union). Instances ofEither
are either an instance ofLeft
orRight
.Either is an algebraic data type similar to the an Option/Optional.
A common use ofEither
is as an alternative toOptional
for dealing with possible missing values. In this usage,Absent
is replaced with aLeft
which can contain useful information.Right
takes the place ofPresent
. Convention dictates thatLeft
is used for failure andRight
is used for success.A non-null
Either<L,R>
reference can be used as an alternative to the classic error handling (exceptions).public static Either<Exception, Integer> divide(int x, int y) { try { return Either.valueOf(x / y); } catch (Exception e) { return Either.errorOf(e); } }
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract <V> V
either(java.util.function.Function<L,V> leftFunction, java.util.function.Function<R,V> rightFunction)
AppliesleftFunction
if this is aLeft
orrightFunction
if this is aRight
.abstract boolean
equals(java.lang.Object object)
Returnstrue
ifobject
is anEither
instance, and either the contained references are equal to each other.L
error()
Same asleft()
, a convenience method for Value/Error use case.static <L,R>
Either<L,R>errorOf(L reference)
Same asofLeft(L)
, a convenience method for Value/Error use case.abstract <R1> Either<L,R1>
flatMap(java.util.function.Function<? super R,? extends Either<? extends L,? extends R1>> mapper)
Appliesfunction
if this is aRight
or returnsLeft
.abstract int
hashCode()
boolean
isError()
Same asisLeft()
, a convenience method for Value/Error use case.abstract boolean
isLeft()
abstract boolean
isRight()
boolean
isValue()
Same asisRight()
, a convenience method for Value/Error use case.abstract L
left()
Returns the contained instance, which must be present.<R1> Either<L,R1>
map(java.util.function.Function<? super R,? extends R1> mapper)
Appliesfunction
if this is aRight
or returnsLeft
.abstract <T,E extends java.lang.Exception>
TmapOrThrow(java.util.function.Function<L,E> leftFunction, java.util.function.Function<R,T> rightFunction)
Returns theRight
instance, if present, mapped with the givenrightFunction
;throw leftFunction.apply(left())
otherwise.static <L,R>
Either<L,R>ofLeft(L reference)
Returns anEither
instance containing the given non-null reference.static <L,R>
Either<L,R>ofRight(R reference)
Returns anEither
instance containing the given non-null reference.abstract Either<L,R>
or(Either<? extends L,? extends R> secondChoice)
abstract R
or(R defaultValue)
Returns thisEither
value if it has theRight
value present;defaultValue
otherwise.abstract <E extends java.lang.Exception>
RorThrow(java.util.function.Function<L,E> leftFunction)
Returns theRight
value if it is present;throw leftFunction.apply(left())
otherwise.abstract R
right()
Returns the contained instance, which must be present.abstract Either<R,L>
swap()
If this is aLeft
, then return the left value inRight
or vice versa.abstract java.lang.String
toString()
abstract <A,B>
Either<A,B>transform(java.util.function.Function<L,A> leftFunction, java.util.function.Function<R,B> rightFunction)
AppliesleftFunction
if this is aLeft
orrightFunction
if this is aRight
.R
value()
Same asright()
, a convenience method for Value/Error use case.static <L,R>
Either<L,R>valueOf(R reference)
Same asofRight(R)
, a convenience method for Value/Error use case.
-
-
-
Method Detail
-
valueOf
public static <L,R> Either<L,R> valueOf(R reference)
Same asofRight(R)
, a convenience method for Value/Error use case.- Type Parameters:
L
- the type contained by theLeft
instanceR
- the type contained by theRight
instance- Parameters:
reference
- the value to create theRight
instance with- Returns:
- new
Right
instance with the given value
-
errorOf
public static <L,R> Either<L,R> errorOf(L reference)
Same asofLeft(L)
, a convenience method for Value/Error use case.- Type Parameters:
L
- the type contained by theLeft
instanceR
- the type contained by theRight
instance- Parameters:
reference
- the value to create theLeft
instance with- Returns:
- new
Left
instance with the given value
-
ofLeft
public static <L,R> Either<L,R> ofLeft(L reference)
Returns anEither
instance containing the given non-null reference.- Type Parameters:
L
- the type contained by theLeft
instanceR
- the type contained by theRight
instance- Parameters:
reference
- the value to create theLeft
instance with- Returns:
- new
Left
instance with the given value - Throws:
java.lang.NullPointerException
- ifreference
is null
-
ofRight
public static <L,R> Either<L,R> ofRight(R reference)
Returns anEither
instance containing the given non-null reference.- Type Parameters:
L
- the type contained by theLeft
instanceR
- the type contained by theRight
instance- Parameters:
reference
- the value to create theRight
instance with- Returns:
- new
Right
instance with the given value - Throws:
java.lang.NullPointerException
- ifreference
is null
-
isValue
public boolean isValue()
Same asisRight()
, a convenience method for Value/Error use case.- Returns:
- true if this is a
Right
, false otherwise.
-
isError
public boolean isError()
Same asisLeft()
, a convenience method for Value/Error use case.- Returns:
- true if this is a
Left
, false otherwise.
-
isLeft
public abstract boolean isLeft()
- Returns:
- true if this is a
Left
, false otherwise.
-
isRight
public abstract boolean isRight()
- Returns:
- true if this is a
Right
, false otherwise.
-
value
public R value()
Same asright()
, a convenience method for Value/Error use case.- Returns:
- the
Right
instance if present or throw
-
error
public L error()
Same asleft()
, a convenience method for Value/Error use case.- Returns:
- the
Left
instance if present or throw
-
left
public abstract L left()
Returns the contained instance, which must be present. Otherwise, throw.- Returns:
- the
Left
instance if present or throw - Throws:
java.lang.IllegalStateException
- if the instance is absent (isLeft()
returnsfalse
); depending on this specific exception type (over the more generalRuntimeException
) is discouraged
-
right
public abstract R right()
Returns the contained instance, which must be present. Otherwise, throw.- Returns:
- the
Right
instance if present or throw - Throws:
java.lang.IllegalStateException
- if the instance is absent (isRight()
returnsfalse
); depending on this specific exception type (over the more generalRuntimeException
) is discouraged
-
or
public abstract Either<L,R> or(Either<? extends L,? extends R> secondChoice)
- Parameters:
secondChoice
- the alternativeEither
- Returns:
this
instance ifRight
is present orsecondChoice
otherwise.
-
or
public abstract R or(R defaultValue)
Returns thisEither
value if it has theRight
value present;defaultValue
otherwise.- Parameters:
defaultValue
- the alternative value- Returns:
this
value ifRight
is present ordefaultValue
otherwise.
-
orThrow
public abstract <E extends java.lang.Exception> R orThrow(java.util.function.Function<L,E> leftFunction) throws E extends java.lang.Exception
Returns theRight
value if it is present;throw leftFunction.apply(left())
otherwise.- Type Parameters:
E
- type of thrown exception mapped from left- Parameters:
leftFunction
- theLeft
mapping function from left to an exception- Returns:
- the
Right
value or throw the result ofleftFunction
- Throws:
E
- type of thrown exception mapped fromLeft
java.lang.NullPointerException
- if right value is absent or the given function isnull
E extends java.lang.Exception
-
mapOrThrow
public abstract <T,E extends java.lang.Exception> T mapOrThrow(java.util.function.Function<L,E> leftFunction, java.util.function.Function<R,T> rightFunction) throws E extends java.lang.Exception
Returns theRight
instance, if present, mapped with the givenrightFunction
;throw leftFunction.apply(left())
otherwise.- Type Parameters:
T
- type of the returned value mapped fromRight
E
- type of thrown exception mapped fromLeft
- Parameters:
leftFunction
- theLeft
mapping functionrightFunction
- theRight
mapping function- Returns:
- T mapped with
rightFunction
ifRight
is present, or throw - Throws:
E
- mapped with leftFunction ifLeft
is presentjava.lang.NullPointerException
- ifRight
value is absent or a given function isnull
E extends java.lang.Exception
-
either
public abstract <V> V either(java.util.function.Function<L,V> leftFunction, java.util.function.Function<R,V> rightFunction)
AppliesleftFunction
if this is aLeft
orrightFunction
if this is aRight
.- Type Parameters:
V
- type of the value returned by eitherleftFunction
orrightFunction
- Parameters:
leftFunction
-Left
value mapperrightFunction
-Right
value mapper- Returns:
- the result of the
leftFunction
orrightFunction
- Throws:
java.lang.NullPointerException
- if any of the functions arenull
-
transform
public abstract <A,B> Either<A,B> transform(java.util.function.Function<L,A> leftFunction, java.util.function.Function<R,B> rightFunction)
AppliesleftFunction
if this is aLeft
orrightFunction
if this is aRight
.- Type Parameters:
A
- theLeft
type after transformationB
- theRight
type after transformation- Parameters:
leftFunction
-Left
value mapperrightFunction
-Right
value mapper- Returns:
- an Either after transformation by
leftFunction
orrightFunction
. - Throws:
java.lang.NullPointerException
- if any of the functions arenull
-
flatMap
public abstract <R1> Either<L,R1> flatMap(java.util.function.Function<? super R,? extends Either<? extends L,? extends R1>> mapper)
Appliesfunction
if this is aRight
or returnsLeft
.- Type Parameters:
R1
- the type contained by the mappedRight
instance- Parameters:
mapper
-Right
value mapper, withEither
as result- Returns:
- an Either after conditional transformation by
function
. - Throws:
java.lang.NullPointerException
- iffunction
isnull
-
map
public <R1> Either<L,R1> map(java.util.function.Function<? super R,? extends R1> mapper)
Appliesfunction
if this is aRight
or returnsLeft
.- Type Parameters:
R1
- the type contained by the mappedRight
instance- Parameters:
mapper
-Right
value mapper, withR1
as result- Returns:
- an Either after conditional transformation by
function
. - Throws:
java.lang.NullPointerException
- iffunction
isnull
-
swap
public abstract Either<R,L> swap()
If this is aLeft
, then return the left value inRight
or vice versa.- Returns:
- a new
Either<R,L>
-
equals
public abstract boolean equals(@Nullable java.lang.Object object)
Returnstrue
ifobject
is anEither
instance, and either the contained references are equal to each other. Note thatEither
instances of differing parameterized types can be equal.- Overrides:
equals
in classjava.lang.Object
- Parameters:
object
- the object to compare with- Returns:
true
if givenobject
is equal tothis
-
hashCode
public abstract int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- a hash code for
this
instance.
-
toString
public abstract java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation for
this
instance.
-
-