Class Either<T1,​T2>

    • Field Summary

      Fields 
      Modifier and Type Field Description
      T1 _1
      The potential first element in this tuple
      T2 _2
      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.
    • Field Detail

      • _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 Detail

      • 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 Detail

      • 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(), 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()
      • 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:
        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)
      • 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()
      • 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:
        get_2()
      • 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
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object