Class RetryResult<T>
- java.lang.Object
-
- org.kiwiproject.retry.RetryResult<T>
-
- Type Parameters:
T- the result type
public class RetryResult<T> extends Object
Defines a result of one or more attempts to get some type of object. The list of errors should reflect the order in which errors occurred, e.g. the first error should be the error for the first attempt, second error should be the error for the second attempt, etc.A unique identifier is automatically assigned to new
RetryResultinstances for easy identification, e.g. when logging retry exceptions.Note that there is nothing precluding a
RetryResultfrom representing a failure but not actually have any errors. This could happen, for example, if retry code executes aSupplierand that supplier is implemented to catch all exceptions and returnnullto force a retry. We generally recommend to implement Suppliers to throw exceptions when errors occur. They will need to be subclasses ofRuntimeExceptionsinceSupplierdoesn't permit checked exceptions, plus we generally prefer unchecked exceptions.
-
-
Constructor Summary
Constructors Constructor Description RetryResult(int numAttemptsMade, int maxAttempts, T object, List<Exception> errors)Create new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanfailed()The result failed if this result does not have an object.ExceptiongetLastError()Assumes there is at least one error, and returns the last one that was thrown regardless of the number of attempts.Optional<Exception>getLastErrorIfPresent()Returns anOptionalthat contains the last error if this result has any errors, otherwise empty.intgetNumErrors()The number of failed attempts, which can be zero up to the maximum number of attempts.TgetObject()Assumes there is an object an returns it, otherwise throws an exception.Optional<T>getObjectIfPresent()Returns anOptionalthat contains the object if this result was successful, otherwise it will be empty.Set<String>getUniqueErrorTypes()Return a set containing the unique error types in this result.booleanhasAnyErrors()Did any attempts fail with an exception? The overall result can still be successful if an object is obtained before the maximum number of attempts is reached.booleanhasMoreThanOneAttempt()Whether more than one attempt was made to retrieve the object.booleanhasObject()Does this result have an object that was successfully retrieved before the maximum number of attempts?booleansucceeded()The result succeeded if this result has an object.
-
-
-
Constructor Detail
-
RetryResult
public RetryResult(int numAttemptsMade, int maxAttempts, @Nullable T object, @Nullable List<Exception> errors)Create new instance.- Parameters:
numAttemptsMade- the number of attempts (must be less than or equal to max attempts)maxAttempts- the maximum number of attemptsobject- the result, ornullerrors- a list containing any errors that occurred on attempts, ornullor empty list if no errors occurred
-
-
Method Detail
-
succeeded
public boolean succeeded()
The result succeeded if this result has an object.- Returns:
- true if the result object is not null, otherwise false
-
failed
public boolean failed()
The result failed if this result does not have an object.- Returns:
- true if the result object is null, otherwise false
-
hasObject
public boolean hasObject()
Does this result have an object that was successfully retrieved before the maximum number of attempts?- Returns:
- true if the result object is not null, otherwise false
-
getObject
public T getObject()
Assumes there is an object an returns it, otherwise throws an exception.You should check if an object exists before calling this. Or, consider using
getObjectIfPresent().- Returns:
- the result object
- Throws:
IllegalStateException- if this result does not have an object
-
getObjectIfPresent
public Optional<T> getObjectIfPresent()
Returns anOptionalthat contains the object if this result was successful, otherwise it will be empty.- Returns:
- an Optional that may contain a result, or be empty
-
hasMoreThanOneAttempt
public boolean hasMoreThanOneAttempt()
Whether more than one attempt was made to retrieve the object.- Returns:
- true if the number of attempts is more than one
-
hasAnyErrors
public boolean hasAnyErrors()
Did any attempts fail with an exception? The overall result can still be successful if an object is obtained before the maximum number of attempts is reached.- Returns:
- true if there are any errors that occurred (independent of overall success/failure)
-
getNumErrors
public int getNumErrors()
The number of failed attempts, which can be zero up to the maximum number of attempts. A number greater than zero does not represent overall failure, however, since a result could have been obtained even if there were errors.- Returns:
- the number of errors
-
getLastError
public Exception getLastError()
Assumes there is at least one error, and returns the last one that was thrown regardless of the number of attempts.You should check if there are any errors, e.g. using
hasAnyErrors(), before calling this method. Alternatively consider usinggetLastErrorIfPresent().- Returns:
- the most recent exception that occurred
- Throws:
IllegalStateException- if this result does not have any errors
-
getLastErrorIfPresent
public Optional<Exception> getLastErrorIfPresent()
Returns anOptionalthat contains the last error if this result has any errors, otherwise empty.- Returns:
- an Optional that may contain an error, or be empty
-
getUniqueErrorTypes
public Set<String> getUniqueErrorTypes()
Return a set containing the unique error types in this result. There is NO guarantee whatsoever about the order of the unique error types, so don't make any assumptions. That's why the return type isSet.- Returns:
- set of unique error types (class names)
-
-