Package com.pulumi.core
Class Either<L,R>
java.lang.Object
com.pulumi.core.Either<L,R>
- Type Parameters:
L- the type contained by theLeftinstanceR- the type contained by theRightinstance
- All Implemented Interfaces:
Serializable
Represents a value of one of two possible types (a disjoint union).
Instances of
Either are either an instance of Left or Right.
Either is an algebraic data type similar to the an Option/Optional.
A common use ofEither is as an alternative to Optional for dealing with possible missing values.
In this usage, Absent is replaced with a Left which can contain useful information.
Right takes the place of Present.
Convention dictates that Left is used for failure and Right 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:
-
Method Summary
Modifier and TypeMethodDescriptionabstract <V> VAppliesleftFunctionif this is aLeftorrightFunctionif this is aRight.abstract booleanReturnstrueifobjectis anEitherinstance, and either the contained references are equal to each other.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.Appliesfunctionif this is aRightor returnsLeft.abstract inthashCode()booleanisError()Same asisLeft(), a convenience method for Value/Error use case.abstract booleanisLeft()abstract booleanisRight()booleanisValue()Same asisRight(), a convenience method for Value/Error use case.abstract Lleft()Returns the contained instance, which must be present.Appliesfunctionif this is aRightor returnsLeft.abstract <T,E extends Exception>
TmapOrThrow(Function<L, E> leftFunction, Function<R, T> rightFunction) Returns theRightinstance, if present, mapped with the givenrightFunction;throw leftFunction.apply(left())otherwise.static <L,R> Either<L, R> ofLeft(L reference) Returns anEitherinstance containing the given non-null reference.static <L,R> Either<L, R> ofRight(R reference) Returns anEitherinstance containing the given non-null reference.abstract RReturns thisEithervalue if it has theRightvalue present;defaultValueotherwise.Returns theRightvalue if it is present;throw leftFunction.apply(left())otherwise.abstract Rright()Returns the contained instance, which must be present.swap()If this is aLeft, then return the left value inRightor vice versa.abstract StringtoString()abstract <A,B> Either<A, B> AppliesleftFunctionif this is aLeftorrightFunctionif this is aRight.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 Details
-
valueOf
Same asofRight(R), a convenience method for Value/Error use case.- Type Parameters:
L- the type contained by theLeftinstanceR- the type contained by theRightinstance- Parameters:
reference- the value to create theRightinstance with- Returns:
- new
Rightinstance with the given value
-
errorOf
Same asofLeft(L), a convenience method for Value/Error use case.- Type Parameters:
L- the type contained by theLeftinstanceR- the type contained by theRightinstance- Parameters:
reference- the value to create theLeftinstance with- Returns:
- new
Leftinstance with the given value
-
ofLeft
Returns anEitherinstance containing the given non-null reference.- Type Parameters:
L- the type contained by theLeftinstanceR- the type contained by theRightinstance- Parameters:
reference- the value to create theLeftinstance with- Returns:
- new
Leftinstance with the given value - Throws:
NullPointerException- ifreferenceis null
-
ofRight
Returns anEitherinstance containing the given non-null reference.- Type Parameters:
L- the type contained by theLeftinstanceR- the type contained by theRightinstance- Parameters:
reference- the value to create theRightinstance with- Returns:
- new
Rightinstance with the given value - Throws:
NullPointerException- ifreferenceis 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
Same asright(), a convenience method for Value/Error use case.- Returns:
- the
Rightinstance if present or throw
-
error
Same asleft(), a convenience method for Value/Error use case.- Returns:
- the
Leftinstance if present or throw
-
left
Returns the contained instance, which must be present. Otherwise, throw.- Returns:
- the
Leftinstance if present or throw - Throws:
IllegalStateException- if the instance is absent (isLeft()returnsfalse); depending on this specific exception type (over the more generalRuntimeException) is discouraged
-
right
Returns the contained instance, which must be present. Otherwise, throw.- Returns:
- the
Rightinstance if present or throw - Throws:
IllegalStateException- if the instance is absent (isRight()returnsfalse); depending on this specific exception type (over the more generalRuntimeException) is discouraged
-
or
- Parameters:
secondChoice- the alternativeEither- Returns:
thisinstance ifRightis present orsecondChoiceotherwise.
-
or
Returns thisEithervalue if it has theRightvalue present;defaultValueotherwise.- Parameters:
defaultValue- the alternative value- Returns:
thisvalue ifRightis present ordefaultValueotherwise.
-
orThrow
Returns theRightvalue if it is present;throw leftFunction.apply(left())otherwise.- Type Parameters:
E- type of thrown exception mapped from left- Parameters:
leftFunction- theLeftmapping function from left to an exception- Returns:
- the
Rightvalue or throw the result ofleftFunction - Throws:
E- type of thrown exception mapped fromLeftNullPointerException- if right value is absent or the given function isnull
-
mapOrThrow
public abstract <T,E extends Exception> T mapOrThrow(Function<L, E> leftFunction, Function<R, throws ET> rightFunction) Returns theRightinstance, if present, mapped with the givenrightFunction;throw leftFunction.apply(left())otherwise.- Type Parameters:
T- type of the returned value mapped fromRightE- type of thrown exception mapped fromLeft- Parameters:
leftFunction- theLeftmapping functionrightFunction- theRightmapping function- Returns:
- T mapped with
rightFunctionifRightis present, or throw - Throws:
E- mapped with leftFunction ifLeftis presentNullPointerException- ifRightvalue is absent or a given function isnull
-
either
AppliesleftFunctionif this is aLeftorrightFunctionif this is aRight.- Type Parameters:
V- type of the value returned by eitherleftFunctionorrightFunction- Parameters:
leftFunction-Leftvalue mapperrightFunction-Rightvalue mapper- Returns:
- the result of the
leftFunctionorrightFunction - Throws:
NullPointerException- if any of the functions arenull
-
transform
public abstract <A,B> Either<A,B> transform(Function<L, A> leftFunction, Function<R, B> rightFunction) AppliesleftFunctionif this is aLeftorrightFunctionif this is aRight.- Type Parameters:
A- theLefttype after transformationB- theRighttype after transformation- Parameters:
leftFunction-Leftvalue mapperrightFunction-Rightvalue mapper- Returns:
- an Either after transformation by
leftFunctionorrightFunction. - Throws:
NullPointerException- if any of the functions arenull
-
flatMap
public abstract <R1> Either<L,R1> flatMap(Function<? super R, ? extends Either<? extends L, ? extends R1>> mapper) Appliesfunctionif this is aRightor returnsLeft.- Type Parameters:
R1- the type contained by the mappedRightinstance- Parameters:
mapper-Rightvalue mapper, withEitheras result- Returns:
- an Either after conditional transformation by
function. - Throws:
NullPointerException- iffunctionisnull
-
map
Appliesfunctionif this is aRightor returnsLeft.- Type Parameters:
R1- the type contained by the mappedRightinstance- Parameters:
mapper-Rightvalue mapper, withR1as result- Returns:
- an Either after conditional transformation by
function. - Throws:
NullPointerException- iffunctionisnull
-
swap
If this is aLeft, then return the left value inRightor vice versa.- Returns:
- a new
Either<R,L>
-
equals
Returnstrueifobjectis anEitherinstance, and either the contained references are equal to each other. Note thatEitherinstances of differing parameterized types can be equal. -
hashCode
public abstract int hashCode() -
toString
-