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 (and one that we can control) 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 what 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 notAvailable().
-
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 the result and the result value was notnull.booleanReturnstrueif the operation that produced thisResultsuccessfully computed the result, but the result value wasnull.booleanReturnstruea result is available and is recursively non-empty as per thedeepNotEmpty()test.booleanisEmpty()Returnstrueif no result is available or if the result value is empty as per theempty()test.booleanReturnstrueif the operation that produced thisResultcould not compute a proper result.static <T> Result<T> Returns a specialResultinstance signifying the absence of a result.static <T> Result<T> of(T value) Returns aResultcontaining the specified value (possiblynull).Returns thisResultif 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 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 signifying the absence of a result.- Type Parameters:
T- the type of the result value- Returns:
- a special
Resultobject signifying the absence of a result
-
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 the result, but the result value wasnull.- Returns:
trueif a result could be computed, but it turned out to benull
-
isAvailableAndNotNull
public boolean isAvailableAndNotNull()Returnstrueif the operation that produced thisResultsuccessfully computed the 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
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 thisResultif it contains a proper result value (possiblynull), else the providedResult.- Parameters:
alternative- theResultto return if thisResultisResult.notAvailable(). Must not benull, and must not beResult.notAvailable().- Returns:
- this instance or the provided instance
- Throws:
IllegalArgumentException- if the specifiedResultisResult.notAvailable()
-
isEmpty
public boolean isEmpty()Returnstrueif no result is available or if the result value is empty as per theempty()test. -
isDeepNotEmpty
public boolean isDeepNotEmpty()Returnstruea result is available and is recursively non-empty as per thedeepNotEmpty()test.- Specified by:
isDeepNotEmptyin interfaceEmptyable- Returns:
trueif a result is available and is deep-not-empty
-
equals
Returnstrueif the specified object is aResultthat either is thisResultor 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.
-