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 TypeMethodDescriptionboolean
Returnstrue
if the specified object is aResult
that either is thisResult
or contains the same value.get()
Returns the result.int
hashCode()
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.boolean
Returnstrue
if the operation that produced thisResult
successfully computed the result.boolean
Returnstrue
if the operation that produced thisResult
successfully computed a result and the result value was notnull
.boolean
Returnstrue
if the operation that produced thisResult
successfully computed a result and the result value wasnull
.boolean
Returnstrue
if the operation that produced thisResult
could not compute a result.boolean
Returnstrue
if the operation that produced thisResult
could not compute a result or the result wasnull
.static <T> Result
<T> Returns a specialResult
instance indicating the absence of a result.static <T> Result
<T> Returns aResult
containingnull
.static <T> Result
<T> of
(T value) Returns aResult
containing the specified value (possiblynull
).Returns the result value, if available, else the provided default value.Returns thisResult
if available, else the providedResult
.toString()
Returns a string representation analogous to the one provided byOptional
.
-
Method Details
-
of
Returns aResult
containing the specified value (possiblynull
).- Type Parameters:
T
- The type of the result value- Parameters:
value
- The value- Returns:
- a
Result
containing the specified value
-
notAvailable
Returns a specialResult
instance indicating the absence of a result.- Type Parameters:
T
- the type of the result value- Returns:
- a special
Result
object indicating the absence of a result
-
nullResult
Returns aResult
containingnull
.- Type Parameters:
T
- the type of the result value- Returns:
- a
Result
containingnull
-
get
Returns the result. You should have established first that a result value is available or aNoSuchElementException
will be thrown.- Returns:
- the value
- Throws:
NoSuchElementException
- if no result is available
-
isAvailable
public boolean isAvailable()Returnstrue
if the operation that produced thisResult
successfully computed the result. If so, the result value can be retrieved via theget()
method. If not, callingget()
method will result in aNoSuchElementException
.- Returns:
true
if a result could be computed
-
isAvailableAndNull
public boolean isAvailableAndNull()Returnstrue
if the operation that produced thisResult
successfully computed a result and the result value wasnull
.- Returns:
true
if a result could be computed, and it turned out to benull
-
isAvailableAndNotNull
public boolean isAvailableAndNotNull()Returnstrue
if the operation that produced thisResult
successfully computed a result and the result value was notnull
.- Returns:
true
if a result could be computed and it was a non-null
result
-
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 thisResult
if available, else the providedResult
.- Parameters:
supplier
- the value to return if thisResult
isnotAvailable()
.- Returns:
- this instance's value if available; else the value provided by the specified
Supplier
; - Throws:
IllegalArgumentException
- if the specifiedResult
isResult.notAvailable()
-
equals
-
hashCode
-
toString
-