Class Either<L,R>

java.lang.Object
ca.uhn.fhir.util.monad.Either<L,R>
Type Parameters:
L - the left type
R - the right type

public class Either<L,R> extends Object
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 Type
    Method
    Description
    boolean
     
    <T> Either<L,T>
    flatMap(Function<? super R,? extends Either<L,? extends T>> flatMapRight)
    Maps the right value to a new Either using the provided function.
    <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.
    void
    forEach(Consumer<? super R> forRight)
    Executes the provided consumer if the right value is present
    Alias for rightOrThrow().
    int
     
    boolean
    Returns true if the value is the left type, false if it is right.
    boolean
    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.
    <T> Either<L,T>
    map(Function<? super R,? extends T> mapRight)
    Maps the right value to a new value using the provided function.
    Returns an Optional containing the right value if present, otherwise an empty Optional.
    orElse(R defaultValue)
    Returns the right value if it is present, otherwise returns the provided default value.
    orElseGet(Supplier<R> defaultSupplier)
    Returns the right value if it is present, otherwise returns the result of the provided supplier.
    peek(Consumer<? super R> forRight)
    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.
    Returns a stream of the right value if present, otherwise an empty stream.
    Swaps the left and right types.
    <U> U
    transform(Function<? super Either<? super L,? super R>,? extends U> transform)
    Transforms the Either to a new value using the provided function.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • swap

      public Either<R,L> swap()
      Swaps the left and right types. The value is unchanged.
      Returns:
      a new Either instance with the left and right types swapped
    • isLeft

      public boolean isLeft()
      Returns true if the value is the left type, false if it is right.
      Returns:
      true if left is present
    • isRight

      public boolean isRight()
      Returns true if the value is the right type, false if it is left.
      Returns:
      true is right is present
    • leftOrThrow

      public L leftOrThrow()
      Returns the left value if it is present, otherwise throws an IllegalStateException. It's generally preferred to pass functions to the Either using fold(Function, Function)
      Returns:
      the left value
      Throws:
      IllegalStateException - if the left value is not present
    • rightOrThrow

      public R rightOrThrow()
      Returns the right value if it is present, otherwise throws an IllegalStateException. It's generally preferred to pass functions to the Either using fold(Function, Function) Alternatively, use orElse(Object) or orElseGet(Supplier)
      Returns:
      the right value
      Throws:
      IllegalStateException - if the right value is not present
    • getOrThrow

      public R getOrThrow()
      Alias for rightOrThrow(). It's generally preferred to pass functions to the Either using fold(Function, Function) Alternatively, use orElse(Object) or orElseGet(Supplier)
      Returns:
      the right value
      Throws:
      IllegalStateException - if the right value is not present
    • orElse

      public R orElse(R defaultValue)
      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

      public R orElseGet(Supplier<R> defaultSupplier)
      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

      public void forEach(Consumer<? super R> forRight)
      Executes the provided consumer if the right value is present
      Parameters:
      forRight - the consumer to execute if the right value is present
    • peek

      public Either<L,R> peek(Consumer<? super R> forRight)
      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

      public <T> Either<L,T> map(Function<? super R,? extends T> mapRight)
      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

      public <T> Either<L,T> flatMap(Function<? super R,? extends Either<L,? extends T>> flatMapRight)
      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 present
      foldRight - the function to map the right value to a new value, if present
      Returns:
      the new value
    • transform

      public <U> U transform(Function<? super Either<? super L,? 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

      public Stream<R> 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

      public Optional<R> 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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object