Package net.orbyfied.coldlib.util
Class Result<V>
java.lang.Object
net.orbyfied.coldlib.util.Result<V>
- Type Parameters:
V- The value type on success.
Construct to hold the result of an
operation which could have thrown
an error.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn exception used byResultwhen unwrapping the result to signal the absence of a value.static classFor absent errors. -
Method Summary
Modifier and TypeMethodDescriptionerror()Returns the captured error if present, or null if absent.static <V> Result<V>Create a new result signaling that the operation failed with the given error, and no value is present.booleanCheck if a SUCCESSFUL value is present.booleanCheck if an error occurred.Get the value if present or the provided default/fallback value if absent.orNull()Get the value if present or null if absent.Get the captured value if successful, or rethrow the captured error if failed.Rethrow the error if present, or exit and return this instance back if absent.static <V> Result<V>success(V value) Create a new success result, with a nullable value.unwrap()Get and return the value if present, or throw with a description if absent.
-
Method Details
-
success
Create a new success result, with a nullable value. The returned instance will always have a value set and marked as set.- Type Parameters:
V- The value type.- Parameters:
value- The value (nullable).- Returns:
- The result instance.
-
failed
Create a new result signaling that the operation failed with the given error, and no value is present. The returned instance will never have a value set or marked as set.- Type Parameters:
V- The value type (for compatibility).- Parameters:
throwable- The cause.- Returns:
- The failed result instance.
-
isPresent
public boolean isPresent()Check if a SUCCESSFUL value is present. You can assign a value and a thrown error, which means in some rare cases, entirely based on the source of this result, it can still return false while containing a value.- Returns:
- If a value was successfully assigned and is present.
-
isSuccess
public boolean isSuccess()Check if an error occurred. This just checks to see if a throwable was captured by this result.- Returns:
- If a throwable was captured.
-
error
Returns the captured error if present, or null if absent. The field being set toNO_VALUEis considered absent, so null will be returned.- Returns:
- The error or null if absent.
-
orNull
Get the value if present or null if absent. This ignores if an and what error was thrown.- Returns:
- The value or null if absent.
-
orElse
Get the value if present or the provided default/fallback value if absent. This usesisPresent()to check presence.- Parameters:
def- The fallback value.- Returns:
- The value.
-
rethrowFailed
Rethrow the error if present, or exit and return this instance back if absent. This usesThrowables.sneakyThrow(Throwable)to rethrow the error.- Returns:
- This.
-
orRethrow
Get the captured value if successful, or rethrow the captured error if failed. This usesThrowables.sneakyThrow(Throwable)to rethrow the error.- Returns:
- The value.
-
unwrap
Get and return the value if present, or throw with a description if absent. If the value is simply absent, with no cause (error()), a blankResult.AbsentValueExceptionwill be thrown. If there is a cause, an error captured, it will be set as the cause for theResult.AbsentValueExceptionthrown.- Returns:
- The value.
- Throws:
Result.AbsentValueException- If no value is present.
-