Class Either<L,R>
- java.lang.Object
-
- org.apache.druid.java.util.common.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
L
error()
Returns the error object.static <L,R>
Either<L,R>error(L error)
int
hashCode()
boolean
isError()
boolean
isValue()
<T> Either<L,T>
map(Function<R,T> fn)
Applies a function to this value, if present.String
toString()
static <L,R>
Either<L,R>value(R value)
R
valueOrThrow()
If this Either represents a value, returns it.
-
-
-
Method Detail
-
error
public static <L,R> Either<L,R> error(L error)
-
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 aThrowable
, 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, useisError()
anderror()
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
-
-