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:
java.io.Serializable
@CheckReturnValue public abstract class Either<L,R> extends java.lang.Object implements java.io.SerializableRepresents a value of one of two possible types (a disjoint union). Instances ofEitherare either an instance ofLeftorRight.Either is an algebraic data type similar to the an Option/Optional.
A common use ofEitheris as an alternative toOptionalfor dealing with possible missing values. In this usage,Absentis replaced with aLeftwhich can contain useful information.Righttakes the place ofPresent. Convention dictates thatLeftis used for failure andRightis 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> Veither(java.util.function.Function<L,V> leftFunction, java.util.function.Function<R,V> rightFunction)AppliesleftFunctionif this is aLeftorrightFunctionif this is aRight.abstract booleanequals(java.lang.Object object)Returnstrueifobjectis anEitherinstance, and either the contained references are equal to each other.Lerror()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)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.<R1> Either<L,R1>map(java.util.function.Function<? super R,? extends R1> mapper)Appliesfunctionif this is aRightor returnsLeft.abstract <T,E extends java.lang.Exception>
TmapOrThrow(java.util.function.Function<L,E> leftFunction, java.util.function.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 Either<L,R>or(Either<? extends L,? extends R> secondChoice)abstract Ror(R defaultValue)Returns thisEithervalue if it has theRightvalue present;defaultValueotherwise.abstract <E extends java.lang.Exception>
RorThrow(java.util.function.Function<L,E> leftFunction)Returns theRightvalue if it is present;throw leftFunction.apply(left())otherwise.abstract Rright()Returns the contained instance, which must be present.abstract Either<R,L>swap()If this is aLeft, then return the left value inRightor vice versa.abstract java.lang.StringtoString()abstract <A,B>
Either<A,B>transform(java.util.function.Function<L,A> leftFunction, java.util.function.Function<R,B> rightFunction)AppliesleftFunctionif this is aLeftorrightFunctionif this is aRight.Rvalue()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 theLeftinstanceR- the type contained by theRightinstance- Parameters:
reference- the value to create theRightinstance with- Returns:
- new
Rightinstance 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 theLeftinstanceR- the type contained by theRightinstance- Parameters:
reference- the value to create theLeftinstance with- Returns:
- new
Leftinstance with the given value
-
ofLeft
public static <L,R> Either<L,R> ofLeft(L reference)
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:
java.lang.NullPointerException- ifreferenceis null
-
ofRight
public static <L,R> Either<L,R> ofRight(R reference)
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:
java.lang.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
public R value()
Same asright(), a convenience method for Value/Error use case.- Returns:
- the
Rightinstance if present or throw
-
error
public L error()
Same asleft(), a convenience method for Value/Error use case.- Returns:
- the
Leftinstance if present or throw
-
left
public abstract L left()
Returns the contained instance, which must be present. Otherwise, throw.- Returns:
- the
Leftinstance 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
Rightinstance 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:
thisinstance ifRightis present orsecondChoiceotherwise.
-
or
public abstract R or(R defaultValue)
Returns thisEithervalue if it has theRightvalue present;defaultValueotherwise.- Parameters:
defaultValue- the alternative value- Returns:
thisvalue ifRightis present ordefaultValueotherwise.
-
orThrow
public abstract <E extends java.lang.Exception> R orThrow(java.util.function.Function<L,E> leftFunction) throws E extends java.lang.Exception
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 fromLeftjava.lang.NullPointerException- if right value is absent or the given function isnullE 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 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 presentjava.lang.NullPointerException- ifRightvalue is absent or a given function isnullE extends java.lang.Exception
-
either
public abstract <V> V either(java.util.function.Function<L,V> leftFunction, java.util.function.Function<R,V> rightFunction)
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:
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)
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:
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)
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:
java.lang.NullPointerException- iffunctionisnull
-
map
public <R1> Either<L,R1> map(java.util.function.Function<? super R,? extends R1> mapper)
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:
java.lang.NullPointerException- iffunctionisnull
-
swap
public abstract Either<R,L> swap()
If this is aLeft, then return the left value inRightor vice versa.- Returns:
- a new
Either<R,L>
-
equals
public abstract boolean equals(@Nullable java.lang.Object object)Returnstrueifobjectis anEitherinstance, and either the contained references are equal to each other. Note thatEitherinstances of differing parameterized types can be equal.- Overrides:
equalsin classjava.lang.Object- Parameters:
object- the object to compare with- Returns:
trueif givenobjectis equal tothis
-
hashCode
public abstract int hashCode()
- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code for
thisinstance.
-
toString
public abstract java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation for
thisinstance.
-
-