- Type Parameters:
L
- the type of the Left valueR
- the type of the Right value
A generic sealed interface representing a value of one of two possible types (a disjoint union).
Instances of Either are either an instance of Left or Right.
Inspired by the implemenation from Either (Scala) and Either (ArrowKt)
- Author:
- Juan Antonio BreƱa Moral, ChatGPT-40
-
Method Summary
Modifier and TypeMethodDescriptionCombines two Either instances using the given combiner function if both are Right.static <E,
A> Either <E, A> Converts a computation that might raise an exception of type `E` into an `Either` value.Transforms the value in Right using the given function that returns another Either, if this is a Right.<T> T
Applies either the leftMapper function to the value if this is a Left, or the rightMapper function if this is a Right.get()
Returns the value if this is a Right, otherwise throws an exception.default R
Returns the Right value if this is a Right, otherwise returns the result of the given Supplier.boolean
isLeft()
Checks if this is an instance of Left.boolean
isRight()
Checks if this is an instance of Right.static <L,
R> Either <L, R> left
(L value) Creates an instance of Left.Transforms the value in Right using the given function if this is a Right, otherwise returns the current Left.Returns this Either if it is a Right, otherwise returns the result of the given Supplier.static <L,
R> Either <L, R> right
(R value) Creates an instance of Right.swap()
Swaps the Left and Right types.Converts this Either to an Optional.
-
Method Details
-
fold
Applies either the leftMapper function to the value if this is a Left, or the rightMapper function if this is a Right.- Type Parameters:
T
- the type of the result- Parameters:
leftMapper
- the function to apply if this is a LeftrightMapper
- the function to apply if this is a Right- Returns:
- the result of applying the appropriate function
-
isLeft
boolean isLeft()Checks if this is an instance of Left.- Returns:
- true if this is a Left, false otherwise
-
isRight
boolean isRight()Checks if this is an instance of Right.- Returns:
- true if this is a Right, false otherwise
-
left
Creates an instance of Left.- Type Parameters:
L
- the type of the Left valueR
- the type of the Right value- Parameters:
value
- the value to be wrapped in Left- Returns:
- a Left containing the given value
-
right
Creates an instance of Right.- Type Parameters:
L
- the type of the Left valueR
- the type of the Right value- Parameters:
value
- the value to be wrapped in Right- Returns:
- a Right containing the given value
-
map
Transforms the value in Right using the given function if this is a Right, otherwise returns the current Left.- Type Parameters:
U
- the type of the new Right value- Parameters:
mapper
- the function to apply to the Right value- Returns:
- a new Either instance
-
flatMap
Transforms the value in Right using the given function that returns another Either, if this is a Right. Otherwise, returns the current Left.- Type Parameters:
U
- the type of the new Right value- Parameters:
mapper
- the function to apply to the Right value- Returns:
- a new Either instance
-
swap
-
getOrElse
-
orElse
-
combine
default Either<L,R> combine(Either<L, R> other, BiFunction<? super R, ? super R, ? extends R> combiner) Combines two Either instances using the given combiner function if both are Right. Otherwise, returns the first Right instance encountered, or the last Left if both are Left.- Parameters:
other
- another Either instance to combine withcombiner
- a BiFunction to combine the Right values- Returns:
- a new Either instance resulting from the combination
-
toOptional
-
get
R get()Returns the value if this is a Right, otherwise throws an exception.- Returns:
- the Right value
- Throws:
NoSuchElementException
- if this is a Left
-
either
Converts a computation that might raise an exception of type `E` into an `Either` value. The computation is represented by a function that takes a Raise object as input.- Type Parameters:
E
- The type of the exception that can be raised by the computation.A
- The type of the value returned by the computation if it succeeds.- Parameters:
block
- The function that encapsulates the computation.- Returns:
- An `Either` value representing the result of the computation.
-