Class Either3<L,M,R>

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

public class Either3<L,M,R> extends Object
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 Type
    Method
    Description
    boolean
     
    <T> Either3<L,M,T>
    flatMap(Function<? super R,? extends Either3<L,M,? extends T>> flatMapRight)
    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
    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.
    boolean
    Returns true if value is the middle type.
    boolean
    Returns true if the value is the right type.
    Returns the left value.
    <T> Either3<L,M,T>
    map(Function<? super R,? extends T> mapRight)
    Maps the right value to a new value using the provided function.
    Returns the middle value.
    protected <T> Either3<L,M,T>
    narrow(Either3<L,M,? extends T> wide)
     
    Returns an Optional containing the right value if present, otherwise an empty Optional.
    orElse(R defaultValue)
    Returns the right value if present, otherwise return the provided default value.
    orElseGet(Supplier<R> defaultSupplier)
    Returns the right value if present, otherwise return the result of the provided supplier.
    peek(Consumer<? super R> forRight)
    Executes the provided consumer if the right value is present, returning the Either3 unchanged.
    protected <T> Either3<L,M,T>
     
    Returns the right value.
    Rotates the types of the Either3 to the right.
    Returns a stream of the right value if present, otherwise an empty stream.
    Swaps the position of the left and right types.
    <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.

    Methods inherited from class java.lang.Object

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

    • swap

      public Either3<R,M,L> 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

      public Either3<R,L,M> 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

      public L 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 using fold(Function, Function, Function)
      Returns:
      the left value
      Throws:
      IllegalStateException - if the left value is not present
    • middleOrThrow

      public M 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 using fold(Function, Function, Function)
      Returns:
      the middle value
      Throws:
      IllegalStateException - if the middle value is not present
    • rightOrThrow

      public R 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 using fold(Function, 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, 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 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

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

      public boolean isLeft()
      Returns true if the value is the left type. Otherwise, returns false.
      Returns:
      true if left is present
    • isMiddle

      public boolean isMiddle()
      Returns true if value is the middle type. Otherwise, returns false.
      Returns:
      true if middle is present
    • isRight

      public boolean isRight()
      Returns true if the value is the right type. Otherwise, returns false.
      Returns:
      true if right is present
    • 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 Either3<L,M,R> peek(Consumer<? super R> forRight)
      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

      public <T> Either3<L,M,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 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,T> flatMap(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 present
      foldMiddle - the function to map the middle 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 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

      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
    • narrow

      protected <T> Either3<L,M,T> narrow(Either3<L,M,? extends T> wide)
    • propagate

      protected <T> Either3<L,M,T> propagate()
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object