
Package ca.uhn.fhir.util.monad
Class Either<L,R>
java.lang.Object
ca.uhn.fhir.util.monad.Either<L,R>
- Type Parameters:
L
- the left typeR
- the right type
Represents a value of one of two possible types (a disjoint union). An instance of Either is either an instance of
Left or Right, only one of which can be present at a time.
This type is right-biased, meaning that operations like map and flatMap will apply to the right value if it is present.
By convention, the preferred type for a given context should be the right value.
By convention, the left value is used for error handling, when used in a context where one of the values is an error or exception.
While this class does provide methods for accessing the left and right values, it is generally recommended to use
the map, flatMap, and fold methods to work with the values. These methods are more idiomatic and less error-prone, and they
are less likely to result in IllegalStateExceptions.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Maps the right value to a new Either using the provided function.<T> T
Maps the left or right value to a new value using the provided functions.void
Executes the provided consumer if the right value is presentAlias forrightOrThrow()
.int
hashCode()
boolean
isLeft()
Returns true if the value is the left type, false if it is right.boolean
isRight()
Returns true if the value is the right type, false if it is left.Returns the left value if it is present, otherwise throws an IllegalStateException.Maps the right value to a new value using the provided function.optional()
Returns an Optional containing the right value if present, otherwise an empty Optional.Returns the right value if it is present, otherwise returns the provided default value.Returns the right value if it is present, otherwise returns the result of the provided supplier.Executes the provided consumer if the right value is present, returning the Either unchanged.Returns the right value if it is present, otherwise throws an IllegalStateException.stream()
Returns a stream of the right value if present, otherwise an empty stream.swap()
Swaps the left and right types.<U> U
Transforms the Either to a new value using the provided function.
-
Method Details
-
swap
Swaps the left and right types. The value is unchanged.- Returns:
- a new Either instance with the left and right types swapped
-
isLeft
Returns true if the value is the left type, false if it is right.- Returns:
- true if left is present
-
isRight
Returns true if the value is the right type, false if it is left.- Returns:
- true is right is present
-
leftOrThrow
Returns the left value if it is present, otherwise throws an IllegalStateException. It's generally preferred to pass functions to the Either usingfold(Function, Function)
- Returns:
- the left value
- Throws:
IllegalStateException
- if the left value is not present
-
rightOrThrow
Returns the right value if it is present, otherwise throws an IllegalStateException. It's generally preferred to pass functions to the Either usingfold(Function, Function)
Alternatively, useorElse(Object)
ororElseGet(Supplier)
- Returns:
- the right value
- Throws:
IllegalStateException
- if the right value is not present
-
getOrThrow
Alias forrightOrThrow()
. It's generally preferred to pass functions to the Either usingfold(Function, Function)
Alternatively, useorElse(Object)
ororElseGet(Supplier)
- Returns:
- the right value
- Throws:
IllegalStateException
- if the right value is not present
-
orElse
Returns the right value if it is present, otherwise returns the provided default value.- Parameters:
defaultValue
- the value to return if the right value is not present- Returns:
- the right value if it is present, otherwise the default value
-
orElseGet
Returns the right value if it is present, otherwise returns the result of the provided supplier.- Parameters:
defaultSupplier
- the supplier to provide a default value if the right value is not present- Returns:
- the right value if it is present, otherwise the result of the supplier
-
forEach
Executes the provided consumer if the right value is present- Parameters:
forRight
- the consumer to execute if the right value is present
-
peek
Executes the provided consumer if the right value is present, returning the Either unchanged.- Parameters:
forRight
- the consumer to execute if the right value is present- Returns:
- the Either unchanged
-
map
Maps the right value to a new value using the provided function. If the right value is not present, returns the left value unchanged.- Type Parameters:
T
- the new right type- Parameters:
mapRight
- the function to map the right value to a new value- Returns:
- the Either with the right value mapped to a new value, or the left value unchanged
-
flatMap
Maps the right value to a new Either using the provided function. If the right value is not present, returns the left value unchanged.- Type Parameters:
T
- the new right type- Parameters:
flatMapRight
- the function to map the right value to a new Either- Returns:
- a new Either instance with the right value mapped to a new Either, or the left value unchanged
-
fold
public <T> T fold(Function<? super L, ? extends T> foldLeft, Function<? super R, ? extends T> foldRight) Maps the left or right value to a new value using the provided functions. The function is sometimes known as "reduce". If the right value is present, the foldRight function is applied to the right value. If the left value is present, the foldLeft function is applied to the left value.- Type Parameters:
T
- the type of the new value- Parameters:
foldLeft
- the function to map the left value to a new value, if presentfoldRight
- the function to map the right value to a new value, if present- Returns:
- the new value
-
transform
Transforms the Either to a new value using the provided function. The function is applied to the entire Either, regardless of which value is present. This is in contrast to the map, flatMap, and fold functions, which only apply when the right value is present.- Type Parameters:
U
- the type of the new value- Parameters:
transform
- the function to transform the Either to a new value- Returns:
- the new value
-
stream
Returns a stream of the right value if present, otherwise an empty stream. Mainly useful for converting to a stream for further processing with standard Java APIs like Stream.map, Stream.filter, etc.- Returns:
- the stream of the right value if present
-
optional
Returns an Optional containing the right value if present, otherwise an empty Optional. Mainly useful for converting to an Optional for further processing with standard Java APIs, like Optional.map, Optional.filter, etc.- Returns:
- an Optional containing the right value if present
-
equals
-
hashCode
-