Class Either<T1,T2>
java.lang.Object
dk.cloudcreate.essentials.shared.functional.tuple.Either<T1,T2>
- Type Parameters:
T1- the first element typeT2- the second element type
- All Implemented Interfaces:
Tuple<Either<T1,,T2>> Serializable
Represents a
This is used to represent a choice type that can have two different values, but only one value at a time.
The value can either be
Use
Use
Conditional logic can be applied using
Tuple with two potential elements, but where only one element can have a value
at a timeThis is used to represent a choice type that can have two different values, but only one value at a time.
The value can either be
_1() OR _2()Use
Either(Object, Object) or of_1(Object)/of_2(Object) to create
a new Either instanceUse
is_1() or is_2() to check which value is non-null
and _1() or _2() to get the value of the element.Conditional logic can be applied using
ifIs_1(Consumer) or ifIs_2(Consumer)- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription_1()The first element in this tuple (can be null)_2()The second element in this tuple (can be null)intarity()Number of arguments/elements in the Tuplebooleanget_1()The first element in this tuple wrapped as anOptionalget_2()The second element in this tuple wrapped as anOptionalinthashCode()voidvoidbooleanis_1()Does first element in this tuple have a non-null valuebooleanis_2()Does second element in this tuple have a non-null value<R1,R2> Either<R1, R2> map(BiFunction<? super T1, ? super T2, Either<R1, R2>> mappingFunction) Maps the elements of thisEitherusing the mapping function<R1,R2> Either<R1, R2> map(Function<? super T1, ? super R1> mappingFunction1, Function<? super T2, ? super R2> mappingFunction2) Maps the elements of thisEitherusing two distinct mapping functionsMap the first element of thisEitherusing the mapping functionMap the second element of thisEitherusing the mapping functionstatic <T1,T2> Either<T1, T2> of_1(T1 t1) static <T1,T2> Either<T1, T2> of_2(T2 t2) swap()Swap the elements of thisEitherList<?>toList()Convert the Tuple to a listtoString()
-
Field Details
-
_1
The potential first element in this tuple -
_2
The potential second element in this tuple
-
-
Constructor Details
-
Either
Construct a newTuplewith 2 potential elements, but where only one of them can have a non-null value and one element MUST have a non-null value.- Parameters:
t1- the first elementt2- the second element- Throws:
IllegalArgumentException- if both elements has a non-null value or if both elements are null
-
-
Method Details
-
of_1
-
of_2
-
arity
public int arity()Description copied from interface:TupleNumber of arguments/elements in the Tuple -
toList
Description copied from interface:TupleConvert the Tuple to a list -
_1
The first element in this tuple (can be null)- Returns:
- The first element in this tuple (can be null)
- See Also:
-
get_1
The first element in this tuple wrapped as anOptional- Returns:
- The first element in this tuple wrapped as an
Optional - See Also:
-
is_1
public boolean is_1()Does first element in this tuple have a non-null value- Returns:
- true if the first element in this tuple has a non-null value
- See Also:
-
ifIs_1
-
_2
The second element in this tuple (can be null)- Returns:
- The second element in this tuple (can be null)
- See Also:
-
get_2
The second element in this tuple wrapped as anOptional- Returns:
- The second element in this tuple wrapped as an
Optional - See Also:
-
is_2
public boolean is_2()Does second element in this tuple have a non-null value- Returns:
- true if the second element in this tuple has a non-null value
- See Also:
-
ifIs_2
-
swap
Swap the elements of thisEither -
map
Maps the elements of thisEitherusing the mapping function- Type Parameters:
R1- result type for first element of theEitherafter applying the mapping functionR2- result type for second element of theEitherafter applying the mapping function- Parameters:
mappingFunction- the mapping function- Returns:
- a new
Eitherwith the result of applying the mapping function to thisEither
-
map
public <R1,R2> Either<R1,R2> map(Function<? super T1, ? super R1> mappingFunction1, Function<? super T2, ? super R2> mappingFunction2) Maps the elements of thisEitherusing two distinct mapping functions- Type Parameters:
R1- result type for first element of theEitherafter applying the mapping functionR2- result type for second element of theEitherafter applying the mapping function- Parameters:
mappingFunction1- the mapping function for element number 1mappingFunction2- the mapping function for element number 2- Returns:
- a new
Eitherwith the result of applying the mapping function to thisEither
-
map1
Map the first element of thisEitherusing the mapping function -
map2
Map the second element of thisEitherusing the mapping function -
equals
-
hashCode
public int hashCode() -
toString
-