Class Either<L,​R>


  • public class Either<L,​R>
    extends Object
    Encapsulates either an "error" or a "value". Similar to the Either class in Scala or Haskell, except the possibilities are named "error" and "value" instead of "left" and "right".
    • Method Detail

      • error

        public static <L,​R> Either<L,​R> error​(L error)
      • value

        public static <L,​R> Either<L,​R> value​(@Nullable
                                                          R value)
      • isValue

        public boolean isValue()
      • isError

        public boolean isError()
      • error

        public L error()
        Returns the error object.
        Throws:
        IllegalStateException - if this instance is not an error
      • valueOrThrow

        @Nullable
        public R valueOrThrow()
        If this Either represents a value, returns it. If this Either represents an error, throw an error. If the error is a Throwable, it is wrapped in a RuntimeException and thrown. If it is not a throwable, a generic error is thrown containing the string representation of the error object. To retrieve the error as-is, use isError() and error() instead.
      • map

        public <T> Either<L,​T> map​(Function<R,​T> fn)
        Applies a function to this value, if present. If the mapping function throws an exception, it is thrown by this method instead of being packed up into the returned Either. If this Either represents an error, the mapping function is not applied.
        Throws:
        NullPointerException - if the mapping function returns null
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object