java.lang.Object
org.klojang.check.aux.Result<T>
- Type Parameters:
T
- the type of the result value
- All Implemented Interfaces:
Emptyable
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
.
Another scenario would be iterating over an
array and returning a particular element, if found. If the element can itself
legitimately be null
, it is not clear whether a return value of null
means not present or really null. Using the Result
class, you
would return a Result
containing null
if the element was present but
null
. If the element was not present, you would return
Result.notAvailable()
.
-
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 the result and the result value was notnull
.boolean
Returnstrue
if the operation that produced thisResult
successfully computed the result and the result value wasnull
.boolean
Returnstrue
if a result is available and the result value is recursively non-empty as per thedeepNotEmpty()
test.boolean
isEmpty()
Returnstrue
if no result is available or if the result value is empty as per theempty()
test.boolean
Returnstrue
if the operation that produced thisResult
could not compute a proper result.static <T> Result
<T> Returns a specialResult
instance signifying the absence of a result.static <T> Result
<T> of
(T value) Returns aResult
containing the specified value (possiblynull
).Returns thisResult
if it contains a proper result value (possiblynull
), else the providedResult
.Returns the result value, if available, else the provided default value.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 signifying the absence of a result.- Type Parameters:
T
- the type of the result value- Returns:
- a special
Result
object signifying the absence of a result
-
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 the 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 the 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
Returns the result value, if available, else the provided default value.- Parameters:
defaultValue
- the default value- Returns:
- the result value, if available, else the provided default value
-
or
Returns thisResult
if it contains a proper result value (possiblynull
), else the providedResult
.- Parameters:
alternative
- theResult
to return if thisResult
isResult.notAvailable()
. Must not benull
, and must not beResult.notAvailable()
.- Returns:
- this instance or the provided instance
- Throws:
IllegalArgumentException
- if the specifiedResult
isResult.notAvailable()
-
isEmpty
public boolean isEmpty()Returnstrue
if no result is available or if the result value is empty as per theempty()
test. -
isDeepNotEmpty
public boolean isDeepNotEmpty()Returnstrue
if a result is available and the result value is recursively non-empty as per thedeepNotEmpty()
test.- Specified by:
isDeepNotEmpty
in interfaceEmptyable
- Returns:
true
if a result is available and is deep-not-empty
-
equals
Returnstrue
if the specified object is aResult
that either is thisResult
or contains the same value. -
hashCode
public int hashCode()Returns the hashcode of the value contained in thisResult
, or 0 if no result was available. -
toString
Returns a string representation analogous to the one provided byOptional
.
-