java.lang.Object
dk.cloudcreate.essentials.shared.functional.tuple.Either<T1,T2>
Type Parameters:
T1 - the first element type
T2 - 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 a Tuple with 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()

Use Either(Object, Object) or of_1(Object)/of_2(Object) to create a new Either instance

Use 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
    Modifier and Type
    Field
    Description
    final T1
    The potential first element in this tuple
    final T2
    The potential second element in this tuple
  • Constructor Summary

    Constructors
    Constructor
    Description
    Either(T1 t1, T2 t2)
    Construct a new Tuple with 2 potential elements, but where only one of them can have a non-null value and one element MUST have a non-null value.
  • Method Summary

    Modifier and Type
    Method
    Description
    _1()
    The first element in this tuple (can be null)
    _2()
    The second element in this tuple (can be null)
    int
    Number of arguments/elements in the Tuple
    boolean
     
    The first element in this tuple wrapped as an Optional
    The second element in this tuple wrapped as an Optional
    int
     
    void
    ifIs_1(Consumer<T1> consumer)
    If is_1() returns true, then provide the value of _1() to the supplied consumer
    void
    ifIs_2(Consumer<T2> consumer)
    If is_2() returns true, then provide the value of _2() to the supplied consumer
    boolean
    Does first element in this tuple have a non-null value
    boolean
    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 this Either using 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 this Either using two distinct mapping functions
    <R1> Either<R1,T2>
    map1(Function<? super T1,? super R1> mappingFunction1)
    Map the first element of this Either using the mapping function
    <R2> Either<T1,R2>
    map2(Function<? super T2,? super R2> mappingFunction2)
    Map the second element of this Either using the mapping function
    static <T1, T2> Either<T1,T2>
    of_1(T1 t1)
    Construct a new Tuple with _1() element having a value
    static <T1, T2> Either<T1,T2>
    of_2(T2 t2)
    Construct a new Tuple with _2() element having a value
    Swap the elements of this Either
    List<?>
    Convert the Tuple to a list
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • _1

      public final T1 _1
      The potential first element in this tuple
    • _2

      public final T2 _2
      The potential second element in this tuple
  • Constructor Details

    • Either

      public Either(T1 t1, T2 t2)
      Construct a new Tuple with 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 element
      t2 - the second element
      Throws:
      IllegalArgumentException - if both elements has a non-null value or if both elements are null
  • Method Details

    • of_1

      public static <T1, T2> Either<T1,T2> of_1(T1 t1)
      Construct a new Tuple with _1() element having a value
      Parameters:
      t1 - the _1() element (_2() will have value null)
    • of_2

      public static <T1, T2> Either<T1,T2> of_2(T2 t2)
      Construct a new Tuple with _2() element having a value
      Parameters:
      t2 - the _2() element ( _1() will have value null)
    • arity

      public int arity()
      Description copied from interface: Tuple
      Number of arguments/elements in the Tuple
      Specified by:
      arity in interface Tuple<T1>
      Returns:
      Number of arguments/elements in the Tuple
    • toList

      public List<?> toList()
      Description copied from interface: Tuple
      Convert the Tuple to a list
      Specified by:
      toList in interface Tuple<T1>
      Returns:
      list of all Tuple elements
    • _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

      public Optional<T1> get_1()
      The first element in this tuple wrapped as an Optional
      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

      public void ifIs_1(Consumer<T1> consumer)
      If is_1() returns true, then provide the value of _1() to the supplied consumer
      Parameters:
      consumer - the consumer that will be provided the value of the _1() if is_1() returns true
      See Also:
    • _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

      public Optional<T2> get_2()
      The second element in this tuple wrapped as an Optional
      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

      public void ifIs_2(Consumer<T2> consumer)
      If is_2() returns true, then provide the value of _2() to the supplied consumer
      Parameters:
      consumer - the consumer that will be provided the value of the _2() if is_2() returns true
      See Also:
    • swap

      public Either<T2,T1> swap()
      Swap the elements of this Either
      Returns:
      A new Either where the first element is the second element of this Either AND where the second element is the first element of this Either
    • map

      public <R1, R2> Either<R1,R2> map(BiFunction<? super T1,? super T2,Either<R1,R2>> mappingFunction)
      Maps the elements of this Either using the mapping function
      Type Parameters:
      R1 - result type for first element of the Either after applying the mapping function
      R2 - result type for second element of the Either after applying the mapping function
      Parameters:
      mappingFunction - the mapping function
      Returns:
      a new Either with the result of applying the mapping function to this Either
    • map

      public <R1, R2> Either<R1,R2> map(Function<? super T1,? super R1> mappingFunction1, Function<? super T2,? super R2> mappingFunction2)
      Maps the elements of this Either using two distinct mapping functions
      Type Parameters:
      R1 - result type for first element of the Either after applying the mapping function
      R2 - result type for second element of the Either after applying the mapping function
      Parameters:
      mappingFunction1 - the mapping function for element number 1
      mappingFunction2 - the mapping function for element number 2
      Returns:
      a new Either with the result of applying the mapping function to this Either
    • map1

      public <R1> Either<R1,T2> map1(Function<? super T1,? super R1> mappingFunction1)
      Map the first element of this Either using the mapping function
      Type Parameters:
      R1 - result type for first element of the Either after applying the mapping function
      Parameters:
      mappingFunction1 - the mapping function for element number 1
      Returns:
      a new Either with the result of applying the mapping function to the first element of this Either
    • map2

      public <R2> Either<T1,R2> map2(Function<? super T2,? super R2> mappingFunction2)
      Map the second element of this Either using the mapping function
      Type Parameters:
      R2 - result type for second element of the Either after applying the mapping function
      Parameters:
      mappingFunction2 - the mapping function for element number 2
      Returns:
      a new Either with the result of applying the mapping function to the second element of this Either
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object