
Package ca.uhn.fhir.util.monad
Class Either3<L,M,R>
java.lang.Object
ca.uhn.fhir.util.monad.Either3<L,M,R>
- Type Parameters:
L
- the left value typeM
- the middle value typeR
- the right value type
Represents a value of one of three possible types (a disjoint union). An instance of Either3 is either an instance of
Left, Middle, 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, middle, 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 Either to a new Either using the provided function.<T> T
fold
(Function<? super L, ? extends T> foldLeft, Function<? super M, ? extends T> foldMiddle, Function<? super R, ? extends T> foldRight) Maps the left, middle, or right value to a new value using the provided functions.void
Executes the provided consumer if the right value is present.Alias forrightOrThrow()
.int
hashCode()
boolean
isLeft()
Returns true if the value is the left type.boolean
isMiddle()
Returns true if value is the middle type.boolean
isRight()
Returns true if the value is the right type.Returns the left value.Maps the right value to a new value using the provided function.Returns the middle value.optional()
Returns an Optional containing the right value if present, otherwise an empty Optional.Returns the right value if present, otherwise return the provided default value.Returns the right value if present, otherwise return the result of the provided supplier.Executes the provided consumer if the right value is present, returning the Either3 unchanged.Returns the right value.rotate()
Rotates the types of the Either3 to the right.stream()
Returns a stream of the right value if present, otherwise an empty stream.swap()
Swaps the position of the left and right types.<U> U
Transforms the Either to a new value using the provided function.
-
Method Details
-
swap
Swaps the position of the left and right types. The middle type is unchanged. The value is unchanged.- Returns:
- a new Either3 with the left and right types swapped
-
rotate
Rotates the types of the Either3 to the right. The right type becomes the left type, the left type becomes the middle type, and the middle type becomes the right type. The values are unchanged.- Returns:
- new Either3 with the types rotated
-
leftOrThrow
Returns the left value. Throws an exception if the left value is not present. It's generally preferred to pass functions to the Either usingfold(Function, Function, Function)
- Returns:
- the left value
- Throws:
IllegalStateException
- if the left value is not present
-
middleOrThrow
Returns the middle value. Throws an exception if the middle value is not present. It's generally preferred to pass functions to the Either usingfold(Function, Function, Function)
- Returns:
- the middle value
- Throws:
IllegalStateException
- if the middle value is not present
-
rightOrThrow
Returns the right value. Throws an exception if the right value is not present. It's generally preferred to pass functions to the Either usingfold(Function, 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, 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 present, otherwise return the provided default value.- Parameters:
defaultValue
- the value to return if the right value is not present- Returns:
- the right value if present, otherwise the default value
-
orElseGet
Returns the right value if present, otherwise return 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 present, otherwise the result of the supplier
-
isLeft
Returns true if the value is the left type. Otherwise, returns false.- Returns:
- true if left is present
-
isMiddle
Returns true if value is the middle type. Otherwise, returns false.- Returns:
- true if middle is present
-
isRight
Returns true if the value is the right type. Otherwise, returns false.- Returns:
- true if right is present
-
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 Either3 unchanged.- Parameters:
forRight
- the consumer to execute if the right value is present- Returns:
- the Either3 unchanged
-
map
Maps the right value to a new value using the provided function. If the right value is not present, returns the left or middle value unchanged.- Type Parameters:
T
- the type of the new value- Parameters:
mapRight
- the function to map the right value to a new value- Returns:
- a new Either3 with the right value mapped to a new value, or the left or middle value
-
flatMap
public <T> Either3<L,M, flatMapT> (Function<? super R, ? extends Either3<L, M, ? extends T>> flatMapRight) Maps the Either to a new Either using the provided function. If the right value is present, the function is applied to the right value. If the right value is not present, the left or middle value is returned.- Type Parameters:
T
- the type of the new right value- Parameters:
flatMapRight
- the function to map the Either to a new Either- Returns:
- a new Either3 with the right value mapped to a new Either, or the left or middle value
-
fold
public <T> T fold(Function<? super L, ? extends T> foldLeft, Function<? super M, ? extends T> foldMiddle, Function<? super R, ? extends T> foldRight) Maps the left, middle, 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 middle value is present, the foldMiddle function is applied to the middle 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 presentfoldMiddle
- the function to map the middle 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
public <U> U transform(Function<? super Either3<? super L, ? super M, ? super R>, ? extends U> 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
-
narrow
-
propagate
-
equals
-
hashCode
-