public class Result<T>
extends Object
- Author:
- Peter Blakeley : @pblakez pb@blakez.org
-
Constructor Summary
Constructors
protected
Protected constructor to create a Result instance.
-
Method Summary
Gets the list of errors associated with a failed operation.
Creates a failed Result with a list of errors.
Creates a failed Result with a list of errors and potentially a partial value.
Creates a failed Result with a single error and potentially a partial value.
Creates a failed Result with a single error.
boolean
Checks if the operation was successful.
Gets the primary error from the list of errors.
Creates a successful Result.
Gets the value associated with the result.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
Result
Protected constructor to create a Result instance.
- Parameters:
success
- Indicates if the operation was successful.
value
- An Optional containing the result value (present on success, potentially present on failure).
errors
- A list of errors (empty on success, non-empty on failure).
-
Method Details
-
success
Creates a successful Result.
- Type Parameters:
T
- The type of the value.
- Parameters:
value
- An Optional containing the successful result value.
- Returns:
- A new successful Result instance.
-
fail
Creates a failed Result with a list of errors.
- Type Parameters:
T
- The type of the value (will be empty).
- Parameters:
errors
- A list of errors indicating the failure reason.
- Returns:
- A new failed Result instance.
-
fail
Creates a failed Result with a single error.
- Type Parameters:
T
- The type of the value (will be empty).
- Parameters:
error
- The error indicating the failure reason.
- Returns:
- A new failed Result instance.
-
fail
Creates a failed Result with a list of errors and potentially a partial value.
- Type Parameters:
T
- The type of the value.
- Parameters:
value
- An Optional containing a partial or default value associated with the failure.
errors
- A list of errors indicating the failure reason.
- Returns:
- A new failed Result instance.
-
fail
Creates a failed Result with a single error and potentially a partial value.
- Type Parameters:
T
- The type of the value.
- Parameters:
value
- An Optional containing a partial or default value associated with the failure.
error
- The error indicating the failure reason.
- Returns:
- A new failed Result instance.
-
value
Gets the value associated with the result.
- Returns:
- An Optional containing the value if present.
-
isSuccess
public boolean isSuccess()
Checks if the operation was successful.
- Returns:
- true if the operation succeeded, false otherwise.
-
errors
Gets the list of errors associated with a failed operation.
- Returns:
- An unmodifiable list of errors (empty if successful).
-
primaryError
public Error primaryError()
Gets the primary error from the list of errors.
If the list is empty (which shouldn't happen for a failed result), returns an unknown error.
- Returns:
- The first Error in the list, or a default unknown error.