Package io.codemodder

Class Either<L,R>

java.lang.Object
io.codemodder.Either<L,R>

public class Either<L,R> extends Object
An implementation of the Either monad. It holds a single value of two possible types.
  • Method Details

    • isLeft

      public boolean isLeft()
      Returns true if the left value is present.
    • isRight

      public boolean isRight()
      Returns true if the right value is present.
    • getLeft

      public L getLeft()
      Returns the left value, may be null
    • getRight

      public R getRight()
      Returns the right value, may be null
    • left

      public static <A, B> Either<A,B> left(A value)
      Returns an Either containing value as a left value.
    • right

      public static <A, B> Either<A,B> right(B value)
      Returns an Either containing value as a right value.
    • map

      public <A> Either<A,R> map(Function<L,A> func)
      Applies func to the left value if present.
    • flatMap

      public <A> Either<A,R> flatMap(Function<L,Either<A,R>> func)
      Applies func to the right value if present and returns an Either containing the result. Otherwise, returns an Either containing the left value.
    • mapRight

      public <A> Either<L,A> mapRight(Function<R,A> func)
      Applies func to the left value if present.
    • flatMapRight

      public <A> Either<L,A> flatMapRight(Function<R,Either<L,A>> func)
      Applies func to the right value if present and returns an Either containing the result. Otherwise, returns an Either containing the left value.
    • fromOptional

      public static <L, R> Either<L,R> fromOptional(Optional<L> maybe, R orElse)
      Builds Either<L,R> from an Optional<L> where either it contains the value of the Optional<L> or the orElse object.
    • filter

      public Either<L,R> filter(Predicate<L> pred, R orElse)
      If it contains the left value, applies the Predicate pred and return an Either containing orElse if it fails. Otherwise do nothing
    • ifLeftOrElse

      public void ifLeftOrElse(Consumer<? super L> leftAction, Consumer<? super R> rightAction)
      Applies the Consumer leftAction if it contains the left value, or the rightAction otherwise.
    • ifLeftOrElseGet

      public <A> A ifLeftOrElseGet(Function<? super L,A> leftFunction, Function<? super R,A> rightFunction)
      Returns the result of the Functions leftFunction or rightFunction.
    • toString

      public String toString()
      Overrides:
      toString in class Object