Skip to content

Commit

Permalink
feat($𝛌): support predicator's retryFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Oct 3, 2021
1 parent 194cdb8 commit b1c4a06
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionFunction;
import com.jmsoftware.maf.springcloudstarter.function.functionalinterface.ThrowExceptionRunnable;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* <h1>Retry</h1>
* <p>
Expand All @@ -19,18 +23,18 @@ private Retry() {
/**
* Retry function.
*
* @param runnable the runnable
* @param time the time
* @param runnable the runnable
* @param retryTime the retry time
* @see com.jmsoftware.maf.springcloudstarter.FunctionalInterfaceTests#tesRetryFunction()
*/
public static void retryFunction(ThrowExceptionRunnable runnable, int time) {
public static void retryFunction(ThrowExceptionRunnable runnable, int retryTime) {
while (true) {
try {
runnable.run();
return;
} catch (Exception e) {
time--;
if (time <= 0) {
retryTime--;
if (retryTime <= 0) {
throw new RuntimeException(e);
}
}
Expand All @@ -40,26 +44,54 @@ public static void retryFunction(ThrowExceptionRunnable runnable, int time) {
/**
* Retry function r.
*
* @param <T> the type parameter
* @param <R> the type parameter
* @param function the function
* @param t the t
* @param time the time
* @param <T> the type parameter
* @param <R> the type parameter
* @param function the function
* @param t the t
* @param retryTime the retry time
* @return the r
*/
public static <T, R> R retryFunction(ThrowExceptionFunction<T, R> function, T t, int time) {
public static <T, R> R retryFunction(ThrowExceptionFunction<T, R> function, T t, int retryTime) {
while (true) {
try {
return function.apply(t);
} catch (Exception e) {
time--;
if (time <= 0) {
retryTime--;
if (retryTime <= 0) {
throw new RuntimeException(e);
}
}
}
}

/**
* Retry function r.
*
* @param <T> the type parameter
* @param <R> the type parameter
* @param function the function
* @param t the t
* @param predicator the predicator
* @param retryTime the retry time
* @return the r
* @throws IllegalStateException the illegal state exception
* @see com.jmsoftware.maf.springcloudstarter.FunctionalInterfaceTests#tesRetryFunction()
*/
public static <T, R> R retryFunction(Function<T, R> function, T t, Predicate<R> predicator, int retryTime
) throws IllegalStateException {
while (retryTime > 0) {
try {
final var r = function.apply(t);
if (predicator.test(r)) {
return r;
}
} finally {
retryTime--;
}
}
throw new IllegalStateException("retryTime reached hits the limit and the result is not correct");
}

/**
* Retry function r.
*
Expand All @@ -84,4 +116,34 @@ public static <T, U, R> R retryFunction(ThrowExceptionBiFunction<T, U, R> functi
}
}
}

/**
* Retry function r.
*
* @param <T> the type parameter
* @param <U> the type parameter
* @param <R> the type parameter
* @param function the function
* @param t the t
* @param u the u
* @param predicator the predicator
* @param retryTime the retry time
* @return the r
* @throws IllegalStateException the illegal state exception
*/
public static <T, U, R> R retryFunction(BiFunction<T, U, R> function, T t, U u, Predicate<R> predicator,
int retryTime
) throws IllegalStateException {
while (retryTime > 0) {
try {
final var r = function.apply(t, u);
if (predicator.test(r)) {
return r;
}
} finally {
retryTime--;
}
}
throw new IllegalStateException("retryTime reached hits the limit and the result is not correct");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,13 @@ void tesRetryFunction() {
throw new IllegalStateException("Oops! Somehow, the code throws an exception");
};
Assertions.assertThrows(RuntimeException.class, () -> retryFunction(compute, 5));

final Function<Integer, Integer> compute2 = value -> 1 + value;
final var result = retryFunction(compute2, -1, integer -> integer.equals(0), 3);
Assertions.assertEquals(0, result);
Assertions.assertThrows(
IllegalStateException.class,
() -> retryFunction(compute2, 1, integer -> integer.equals(0), 3)
);
}
}

0 comments on commit b1c4a06

Please sign in to comment.