Class Eithers

java.lang.Object
io.atlassian.fugue.Eithers

public class Eithers extends Object
Utility functions for Eithers.
Since:
1.2
  • Constructor Details

    • Eithers

      private Eithers()
  • Method Details

    • merge

      public static <T> T merge(Either<T,T> either)
      Extracts an object from an Either, regardless of the side in which it is stored, provided both sides contain the same type. This method will never return null.
      Type Parameters:
      T - the type for both the LHS and the RHS
      Parameters:
      either - use whichever side holds the value to return
      Returns:
      the value from whichever side holds it
    • cond

      public static <L,R> Either<L,R> cond(boolean predicate, L left, R right)
      Creates an Either based on a boolean expression. If predicate is true, a Right will be returned containing the supplied right value; if it is false, a Left will be returned containing the supplied left value.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Parameters:
      predicate - if predicate is true, a Right will be returned if it is false, a Left will be returned containing the supplied left value.
      left - the LHS value
      right - the RHS value
      Returns:
      an either with the appropriately selected value
    • getOrThrow

      public static <X extends Exception, A> A getOrThrow(Either<X,A> either) throws X
      Simplifies extracting a value or throwing a checked exception from an Either.
      Type Parameters:
      X - the exception type
      A - the value type
      Parameters:
      either - to extract from
      Returns:
      the value from the RHS
      Throws:
      X - the exception on the LHS
    • isLeft

      public static <L,R> Predicate<Either<L,R>> isLeft()
      A predicate that tests if the supplied either is a left.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Returns:
      the predicate testing left-hand-sidedness
    • isRight

      public static <L,R> Predicate<Either<L,R>> isRight()
      A predicate that tests if the supplied either is a right.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Returns:
      the predicate testing right-hand-sidedness
    • leftMapper

      public static <L,R> Function<Either<L,R>, Option<L>> leftMapper()
      A function that maps an either to an option of its left type. The Function will return a defined Option containing the either's left value if {Either#isLeft()} is true, an undefined Option otherwise.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Returns:
      the function returning a defined option for left-hand-sided eithers
    • rightMapper

      public static <L,R> Function<Either<L,R>, Option<R>> rightMapper()
      A function that maps an either to an option of its right type. The Function will return a defined Option containing the either's right value if {Either#isRight()} is true, an undefined Option otherwise.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Returns:
      the function returning a defined option for right-hand-sided eithers
    • toLeft

      public static <L,R> Function<L, Either<L,R>> toLeft()
      Function to convert from an value to a Either.Left containing that value.
      Type Parameters:
      L - left type.
      R - right type.
      Returns:
      a Function returning a Either.Left.
    • toLeft

      public static <L,R> Function<L, Either<L,R>> toLeft(Class<L> leftType, Class<R> rightType)
      Function to convert from a value to a Either.Left containing that value. Allows hinting the correct types.
      Type Parameters:
      L - left type.
      R - right type.
      Parameters:
      leftType - expected left type.
      rightType - expected right type.
      Returns:
      a Function returning a Either.Left.
    • toLeft

      public static <L,R> Supplier<Either<L,R>> toLeft(L l)
      Supplier returning a Either.Left.
      Type Parameters:
      L - left type.
      R - right type.
      Parameters:
      l - value to return inside the left.
      Returns:
      a Supplier returning a Either.Left..
    • toLeft

      public static <L,R> Supplier<Either<L,R>> toLeft(L l, Class<R> rightType)
      Supplier returning a Either.Left. Allows hinting the correct right type.
      Type Parameters:
      L - left type.
      R - right type.
      Parameters:
      l - value to return inside the left.
      rightType - type hint for the right type of the either.
      Returns:
      a Supplier returning a Either.Left.
    • toRight

      public static <L,R> Function<R, Either<L,R>> toRight()
      Function to convert from an value to a Either.Right. Allows hinting the correct types.
      Type Parameters:
      L - left type.
      R - right type.
      Returns:
      a Function returning a Either.Right.
    • toRight

      public static <L,R> Function<R, Either<L,R>> toRight(Class<L> leftType, Class<R> rightType)
      Function to convert from a value to a Either.Right containing that value. Allows hinting the correct types.
      Type Parameters:
      L - left type.
      R - right type.
      Parameters:
      leftType - expected left type.
      rightType - expected right type.
      Returns:
      a Function returning a Either.Right.
    • toRight

      public static <L,R> Supplier<Either<L,R>> toRight(R r)
      Supplier returning a Either.Right.
      Type Parameters:
      L - left type.
      R - right type.
      Parameters:
      r - value to return inside the right.
      Returns:
      a Supplier returning a Either.Right..
    • toRight

      public static <L,R> Supplier<Either<L,R>> toRight(Class<L> leftType, R r)
      Supplier returning a Either.Right. Allows hinting the correct right type.
      Type Parameters:
      L - left type.
      R - right type.
      Parameters:
      leftType - type hint for the left type of the either.
      r - value to return inside the right.
      Returns:
      a Supplier returning a Either.Right.
    • upcastLeft

      public static <LL, L extends LL, R> Either<LL,R> upcastLeft(Either<L,R> e)
      Upcasts an either of left type L to an either of left type LL, which is a super type of L, keeping the right type unchanged.
      Type Parameters:
      LL - the super type of the contained left type
      L - the base type to upcast
      R - the contained right type
      Parameters:
      e - the source either
      Returns:
      an either of left type LL and right type R
      Since:
      2.0
    • upcastRight

      public static <L, RR, R extends RR> Either<L,RR> upcastRight(Either<L,R> e)
      Upcasts an either of right type R to an either of right type RR, which is a super type of R, keeping the left type unchanged.
      Type Parameters:
      L - the contained left type
      RR - the super type of the contained right type
      R - the base type to upcast
      Parameters:
      e - the source either
      Returns:
      an either of left type L and right type RR
      Since:
      2.0
    • filterLeft

      public static <L,R> Iterable<L> filterLeft(Iterable<Either<L,R>> it)
      Takes an Iterable of eithers, and collects the left values of every either which has a left value
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Parameters:
      it - iterable of eithers to filter and transform from
      Returns:
      the left values contained in the contents of it
    • filterRight

      public static <L,R> Iterable<R> filterRight(Iterable<Either<L,R>> it)
      Takes an Iterable of eithers, and collects the right values of every either which has a left value
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Parameters:
      it - iterable of eithers to filter and transform from
      Returns:
      the right values contained in the contents of it
    • sequenceRight

      public static <L,R> Either<L, Iterable<R>> sequenceRight(Iterable<Either<L,R>> eithers)
      Collect the right values if there are only rights, otherwise return the first left encountered.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Parameters:
      eithers - an Iterable of either values
      Returns:
      either the iterable of right values, or the first left encountered.
    • sequenceRight

      public static <L,R,A,C> Either<L,C> sequenceRight(Iterable<Either<L,R>> eithers, Collector<R,A,C> collector)
      Collect the right values if there are only rights, otherwise return the first left encountered.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      A - The intermediate accumulator type
      C - The result type
      Parameters:
      eithers - an Iterable of either values
      collector - result collector
      Returns:
      either the iterable of right values, or the first left encountered.
      Since:
      4.6.0
    • sequenceLeft

      public static <L,R> Either<Iterable<L>, R> sequenceLeft(Iterable<Either<L,R>> eithers)
      Collect the left values if there are only lefts, otherwise return the first right encountered.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      Parameters:
      eithers - an Iterable of either values
      Returns:
      either the iterable of left values, or the first right encountered.
    • sequenceLeft

      public static <L,R,A,C> Either<C,R> sequenceLeft(Iterable<Either<L,R>> eithers, Collector<L,A,C> collector)
      Collect the left values if there are only lefts, otherwise return the first right encountered.
      Type Parameters:
      L - the LHS type
      R - the RHS type
      A - The intermediate accumulator type
      C - The result type
      Parameters:
      eithers - an Iterable of either values
      collector - result collector
      Returns:
      either the iterable of left values, or the first right encountered.
      Since:
      4.6.0