Package org.catools.common.executor
Class CRetry
- java.lang.Object
-
- org.catools.common.executor.CRetry
-
public class CRetry extends java.lang.Object
It happens that we need to retry an action many times depend on some criteria. To have the retry logic concentrated and avoid potential dead loop or make the code noisy we move the logic here.
-
-
Constructor Summary
Constructors Constructor Description CRetry()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <R> R
retry(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if any exception throws by result of the function get.static <R> R
retry(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if any exception throws by result of the function get.static <R> R
retry(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if any exception throws by result of the function get.static <R> R
retryIf(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIf, int retryCount, int interval)
Retry the function get if the predicateretryIf
returns true.static <R> R
retryIf(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIf, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the predicateretryIf
returns true.static <R> R
retryIf(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIf, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the predicateretryIf
returns true.static <R extends java.util.Collection>
RretryIfEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is an empty collection.static <R extends java.util.Collection>
RretryIfEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is an empty collection.static <R extends java.util.Collection>
RretryIfEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is an empty collection.static <R> R
retryIfFalse(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is Boolean.false or null.static <R> R
retryIfFalse(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is Boolean.false or null.static <R> R
retryIfFalse(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is Boolean.false or null.static <R> R
retryIfNot(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIfNot, int retryCount, int interval)
Retry the function if the predicateretryIfNot
returns false.static <R> R
retryIfNot(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIfNot, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function if the predicateretryIfNot
returns false.static <R> R
retryIfNot(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIfNot, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function if the predicateretryIfNot
returns false.static <R extends java.util.Collection>
RretryIfNotEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is not an empty collection.static <R extends java.util.Collection>
RretryIfNotEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is not an empty collection.static <R extends java.util.Collection>
RretryIfNotEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is not an empty collection.static <R> R
retryIfNotNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is not an null collection.static <R> R
retryIfNotNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is not an null collection.static <R> R
retryIfNotNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is not an null collection.static <R> R
retryIfNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is null.static <R> R
retryIfNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is null.static <R> R
retryIfNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is null.static <R> R
retryIfTrue(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is Boolean.true.static <R> R
retryIfTrue(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is Boolean.true.static <R> R
retryIfTrue(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is Boolean.true.
-
-
-
Method Detail
-
retryIf
public static <R> R retryIf(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIf, int retryCount, int interval)
Retry the function get if the predicateretryIf
returns true. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryIf
- predicate to be testretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get or null if all retry failed
-
retryIf
public static <R> R retryIf(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIf, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the predicateretryIf
returns true. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryIf
- predicate to be testretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIf
public static <R> R retryIf(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIf, int retryCount, int interval, @Nullable java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the predicateretryIf
returns true. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryIf
- predicate to be testretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during invocation at the end or not- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfNot
public static <R> R retryIfNot(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIfNot, int retryCount, int interval)
Retry the function if the predicateretryIfNot
returns false. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryIfNot
- predicate to be testretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get or null if all retry failed
-
retryIfNot
public static <R> R retryIfNot(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIfNot, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function if the predicateretryIfNot
returns false. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryIfNot
- predicate to be testretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfNot
public static <R> R retryIfNot(java.util.function.Function<java.lang.Integer,R> m, java.util.function.Predicate<R> retryIfNot, int retryCount, int interval, @Nullable java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function if the predicateretryIfNot
returns false. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryIfNot
- predicate to be testretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during invocation at the end or not- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfFalse
public static <R> R retryIfFalse(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is Boolean.false or null. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfFalse
public static <R> R retryIfFalse(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is Boolean.false or null. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfFalse
public static <R> R retryIfFalse(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is Boolean.false or null. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during get at the end- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfTrue
public static <R> R retryIfTrue(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is Boolean.true. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get or null if all retry failed
-
retryIfTrue
public static <R> R retryIfTrue(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is Boolean.true. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfTrue
public static <R> R retryIfTrue(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is Boolean.true. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during get at the end- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfEmpty
public static <R extends java.util.Collection> R retryIfEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is an empty collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get or null if all retry failed
-
retryIfEmpty
public static <R extends java.util.Collection> R retryIfEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is an empty collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfEmpty
public static <R extends java.util.Collection> R retryIfEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is an empty collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during get at the end- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfNotEmpty
public static <R extends java.util.Collection> R retryIfNotEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is not an empty collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get or null if all retry failed
-
retryIfNotEmpty
public static <R extends java.util.Collection> R retryIfNotEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is not an empty collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfNotEmpty
public static <R extends java.util.Collection> R retryIfNotEmpty(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is not an empty collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during get at the end- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfNull
public static <R> R retryIfNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is null. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get or null if all retry failed
-
retryIfNull
public static <R> R retryIfNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is null. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfNull
public static <R> R retryIfNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is null. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during get at the end- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfNotNull
public static <R> R retryIfNotNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if the result of get is not an null collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get or null if all retry failed
-
retryIfNotNull
public static <R> R retryIfNotNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if the result of get is not an null collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retryIfNotNull
public static <R> R retryIfNotNull(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if the result of get is not an null collection. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during get at the end- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retry
public static <R> R retry(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval)
Retry the function get if any exception throws by result of the function get. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in milliseconds- Returns:
- result of
m
method get or null if all retry failed
-
retry
public static <R> R retry(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse)
Retry the function get if any exception throws by result of the function get. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is null- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
retry
public static <R> R retry(java.util.function.Function<java.lang.Integer,R> m, int retryCount, int interval, java.util.function.Supplier<R> orElse, boolean throwLastException)
Retry the function get if any exception throws by result of the function get. Please note that we throw runtime exception which wrap the original exception so we do not have to add throwable to all method deceleration.- Type Parameters:
R
- type of returned object- Parameters:
m
- function to be calledretryCount
- maximum number of retryinterval
- interval between retries in millisecondsorElse
- supplier to generate alternative result if retryIf result was always true, returns null if the supplier is nullthrowLastException
- whether we should throw exception which has been throws during get at the end- Returns:
- result of
m
method get. if all retry failed then return orElse invocation result or null if orElse is null.
-
-