Class Result<T>
java.lang.Object
org.klojang.check.extra.Result<T>
- Type Parameters:
T- the type of the result value
A simple value container that explicitly allows the value to be
null. This class is meant to be
used as the return value of methods that would otherwise return null both as the legitimate
outcome of a computation and as a signal that the computation yielded no result. The
HashMap class is a well-known example. If its get() method returns
null, it is not clear whether the requested key was absent, or whether it was present, but
associated with value null. If you wanted to create a Map implementation where this
distinction is clear, you could use the Result class and return notAvailable() if
the requested key was absent, and Result.of(null) (or nullResult()) if present but
null.-
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif the specified object is aResultthat either is thisResultor contains the same value.get()Returns the result.inthashCode()Returns the hashcode of the value contained in thisResult, or 0 if no result was available.<X extends Throwable>
voidifAvailable(FallibleConsumer<T, X> consumer) If available, passes the result to the specified consumer; else does nothing.booleanReturnstrueif the operation that produced thisResultsuccessfully computed the result.booleanReturnstrueif the operation that produced thisResultsuccessfully computed a result and the result value was notnull.booleanReturnstrueif the operation that produced thisResultsuccessfully computed a result and the result value wasnull.booleanReturnstrueif the operation that produced thisResultcould not compute a result.booleanReturnstrueif the operation that produced thisResultcould not compute a result or the result wasnull.static <T> Result<T> Returns a specialResultinstance indicating the absence of a result.static <T> Result<T> Returns aResultcontainingnull.static <T> Result<T> of(T value) Returns aResultcontaining the specified value (possiblynull).Returns the result value, if available, else the provided default value.Returns thisResultif available, else the providedResult.toString()Returns a string representation analogous to the one provided byOptional.
-
Method Details
-
of
Returns aResultcontaining the specified value (possiblynull).- Type Parameters:
T- The type of the result value- Parameters:
value- The value- Returns:
- a
Resultcontaining the specified value
-
notAvailable
Returns a specialResultinstance indicating the absence of a result.- Type Parameters:
T- the type of the result value- Returns:
- a special
Resultobject indicating the absence of a result
-
nullResult
Returns aResultcontainingnull.- Type Parameters:
T- the type of the result value- Returns:
- a
Resultcontainingnull
-
get
Returns the result. You should have established first that a result value is available or aNoSuchElementExceptionwill be thrown.- Returns:
- the value
- Throws:
NoSuchElementException- if no result is available
-
isAvailable
public boolean isAvailable()Returnstrueif the operation that produced thisResultsuccessfully computed the result. If so, the result value can be retrieved via theget()method. If not, callingget()method will result in aNoSuchElementException.- Returns:
trueif a result could be computed
-
isAvailableAndNull
public boolean isAvailableAndNull()Returnstrueif the operation that produced thisResultsuccessfully computed a result and the result value wasnull.- Returns:
trueif a result could be computed, and it turned out to benull
-
isAvailableAndNotNull
public boolean isAvailableAndNotNull()Returnstrueif the operation that produced thisResultsuccessfully computed a result and the result value was notnull.- Returns:
trueif a result could be computed and it was a non-nullresult
-
ifAvailable
If available, passes the result to the specified consumer; else does nothing.- Type Parameters:
X- the type of the exception thrown by the consumer- Parameters:
consumer- the consumer of the result- Throws:
X- if the consumer experiences an error
-
orElse
-
orElseGet
Returns thisResultif available, else the providedResult.- Parameters:
supplier- the value to return if thisResultisnotAvailable().- Returns:
- this instance's value if available; else the value provided by the specified
Supplier; - Throws:
IllegalArgumentException- if the specifiedResultisResult.notAvailable()
-
equals
-
hashCode
-
toString
-