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
public class Either<T1,T2> extends Object implements Tuple<Either<T1,T2>>
Represents aTuplewith two potential elements, but where only one element can have a value at a time
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_1()OR_2()
UseEither(Object, Object)orof_1(Object)/of_2(Object)to create a newEitherinstance
Useis_1()oris_2()to check which value is non-null and_1()or_2()to get the value of the element.
Conditional logic can be applied usingifIs_1(Consumer)orifIs_2(Consumer)- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T1_1()The first element in this tuple (can be null)T2_2()The second element in this tuple (can be null)intarity()Number of arguments/elements in the Tuplebooleanequals(Object o)Optional<T1>get_1()The first element in this tuple wrapped as anOptionalOptional<T2>get_2()The second element in this tuple wrapped as anOptionalinthashCode()voidifIs_1(Consumer<T1> consumer)voidifIs_2(Consumer<T2> consumer)booleanis_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 functions<R1> Either<R1,T2>map1(Function<? super T1,? super R1> mappingFunction1)Map the first element of thisEitherusing the mapping function<R2> Either<T1,R2>map2(Function<? super T2,? super R2> mappingFunction2)Map 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)Either<T2,T1>swap()Swap the elements of thisEitherList<?>toList()Convert the Tuple to a listStringtoString()
-
-
-
Constructor Detail
-
Either
public Either(T1 t1, T2 t2)
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 Detail
-
of_1
public static <T1,T2> Either<T1,T2> of_1(T1 t1)
-
of_2
public static <T1,T2> Either<T1,T2> of_2(T2 t2)
-
arity
public int arity()
Description copied from interface:TupleNumber of arguments/elements in the Tuple
-
_1
public T1 _1()
The first element in this tuple (can be null)- Returns:
- The first element in this tuple (can be null)
- See Also:
get_1(),is_1(),ifIs_1(Consumer)
-
get_1
public Optional<T1> get_1()
The first element in this tuple wrapped as anOptional- Returns:
- The first element in this tuple wrapped as an
Optional - See Also:
_1(),is_1(),ifIs_1(Consumer)
-
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(Consumer),get_1()
-
_2
public T2 _2()
The second element in this tuple (can be null)- Returns:
- The second element in this tuple (can be null)
- See Also:
get_2(),is_2(),ifIs_2(Consumer)
-
get_2
public Optional<T2> get_2()
The second element in this tuple wrapped as anOptional- Returns:
- The second element in this tuple wrapped as an
Optional - See Also:
_2(),is_2(),ifIs_2(Consumer)
-
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(Consumer),get_2()
-
map
public <R1,R2> Either<R1,R2> map(BiFunction<? super T1,? super T2,Either<R1,R2>> mappingFunction)
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
public <R1> Either<R1,T2> map1(Function<? super T1,? super R1> mappingFunction1)
Map the first element of thisEitherusing the mapping function
-
map2
public <R2> Either<T1,R2> map2(Function<? super T2,? super R2> mappingFunction2)
Map the second element of thisEitherusing the mapping function
-
-