Class Either<L,R>
java.lang.Object
com.dudko.tools.safejavastreams.core.Either<L,R>
- Type Parameters:
L
- type of the Left (typically an error or failure reason)R
- type of the Right (typically a success value)
- Direct Known Subclasses:
Either.Left
,Either.Right
Functional-style container type that represents a value of one of two possible types.
Either is often used to indicate a computation that may result in a value (Right) or an error (Left).
This is a functional alternative to throwing exceptions and is similar to Try
, Result
, etc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static final class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionApplies a function that returns another Either to Right value.abstract <T> T
Reduces this Either into a single value by applying one of the provided functions.abstract R
get()
Converts this Either to a Stream.abstract L
getLeft()
Gets the Left value if present.Converts this Either to a Stream.getOrElseGet
(Function<? super L, ? extends R> mapper) Converts this Either to a Stream.abstract R
getRight()
Gets the Right value if present.Executes action if this is Left.Executes action if this is Right.abstract boolean
isLeft()
Checks whether the value is a Left.abstract boolean
isRight()
Checks whether the value is a Right.static <L,
R> Either<L, R> left
(L value) Creates a Left instance.Maps Right value to another type if present.Allows observing the value (for logging, debugging, etc.).Converts Left into Right using a recovery function.Converts Left into another Either using a recovery function.static <L,
R> Either<L, R> right
(R value) Creates a Right instance.Converts Right value to Optional, or empty if Left.
-
Constructor Details
-
Either
public Either()
-
-
Method Details
-
isRight
public abstract boolean isRight()Checks whether the value is a Right.- Returns:
- true if this is a Right instance
-
isLeft
public abstract boolean isLeft()Checks whether the value is a Left.- Returns:
- true if this is a Left instance
-
getLeft
Gets the Left value if present.- Returns:
- Left value
- Throws:
NoSuchElementException
- if this is not a Left
-
getRight
Gets the Right value if present.- Returns:
- Right value
- Throws:
NoSuchElementException
- if this is not a Right
-
map
Maps Right value to another type if present. If Left, the result remains Left.- Type Parameters:
T
- target type- Parameters:
mapper
- function to map Right to T- Returns:
- new Either
-
flatMap
Applies a function that returns another Either to Right value. If Left, the result remains unchanged.- Type Parameters:
T
- target type- Parameters:
mapper
- function that returns Either- Returns:
- new Either
-
ifRight
Executes action if this is Right.- Parameters:
action
- consumer to run- Returns:
- same Either
-
ifLeft
Executes action if this is Left.- Parameters:
action
- consumer to run- Returns:
- same Either
-
fold
public abstract <T> T fold(Function<? super L, ? extends T> leftMapper, Function<? super R, ? extends T> rightMapper) Reduces this Either into a single value by applying one of the provided functions.- Type Parameters:
T
- result type- Parameters:
leftMapper
- function to apply to LeftrightMapper
- function to apply to Right- Returns:
- reduced value
-
recover
Converts Left into Right using a recovery function.- Parameters:
recovery
- mapping function- Returns:
- recovered Either
-
recoverWith
Converts Left into another Either using a recovery function.- Parameters:
recovery
- function to convert Left into Either- Returns:
- new Either
-
peek
Allows observing the value (for logging, debugging, etc.).- Parameters:
inspector
- consumer that sees this Either- Returns:
- same Either
-
toOptional
Converts Right value to Optional, or empty if Left.- Returns:
- Optional with Right or empty
-
get
Converts this Either to a Stream.- Returns:
- Stream with this Either
-
getOrElse
Converts this Either to a Stream.- Returns:
- Stream with this Either
-
getOrElseGet
Converts this Either to a Stream.- Returns:
- Stream with this Either
-
right
Creates a Right instance.- Type Parameters:
L
- Left typeR
- Right type- Parameters:
value
- right value- Returns:
- Right Either
-
left
Creates a Left instance.- Type Parameters:
L
- Left typeR
- Right type- Parameters:
value
- left value- Returns:
- Left Either
-