Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Unterrainer committed Aug 27, 2021
2 parents 56ec6a4 + 22e889b commit 60c23a7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<modelVersion>4.0.0</modelVersion>
<artifactId>jre-utils</artifactId>
<version>0.2.3</version>
<version>0.2.4</version>
<name>JreUtils</name>
<packaging>jar</packaging>

Expand Down
97 changes: 81 additions & 16 deletions src/main/java/info/unterrainer/commons/jreutils/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,109 @@
public class Exceptions {

/**
* Swallows any given Exception (checked or unchecked alike) silently.
* Swallows any given Exception (checked or unchecked alike) silently that is
* thrown when running the given supplier.
*
* @param runnable the {@link Runnable} that could throw the exception to
* swallow
* @param throwables a List of Exception-types
* @param exceptions a List of Exception-types
*/
@SafeVarargs
public static void swallow(final Runnable runnable, final Class<?>... throwables) {
public static void swallow(final Runnable runnable, final Class<?>... exceptions) {
Supplier<Void> supplier = () -> {
runnable.run();
return null;
};
swallowReturning(supplier, throwables);
swallowReturning(supplier, exceptions);
}

/**
* Swallows any given Exception (checked or unchecked alike) silently.
* Swallows any given Exception (checked or unchecked alike) silently that is
* thrown when running the given supplier and returns the return-value
* otherwise.
*
* @param <T> the return-value of the {@link Supplier}
* @param supplier the {@link Supplier} that could throw the exception to
* swallow
* @param throwables a List of Exception-types
* @param exceptions a List of Exception-types
* @return the return-value of the {@link Supplier}
*/
@SafeVarargs
public static <T> T swallowReturning(final Supplier<T> supplier, final Class<?>... throwables) {
public static <T> T swallowReturning(final Supplier<T> supplier, final Class<?>... exceptions) {
try {
return supplier.get();
} catch (Exception throwable) {
boolean swallow = false;
for (Class<?> omit : throwables)
if (omit.isAssignableFrom(throwable.getClass())) {
swallow = true;
break;
}
if (!swallow)
throw throwable;
} catch (Exception e) {
if (!containsException(e, exceptions))
throw e;
return null;
}
}

/**
* If one of the specified exceptions occur while running the given supplier, it
* retries running the supplier [times] times with [backOffInMillis]ms in
* between tries.
* <p>
* If the number of retries are used up and the supplier still throws an
* exception, that exception is re-thrown.
*
* @param runnable the {@link Runnable} that could throw the exception to
* swallow
* @param times the number of times to retry before throwing the
* exception for real
* @param backOffInMillis the time to wait in between retries
* @param exceptions a List of Exception-types to retry upon
*/
@SafeVarargs
public static void retry(final int times, final long backOffInMillis, final Runnable runnable,
final Class<?>... exceptions) {
Supplier<Void> supplier = () -> {
runnable.run();
return null;
};
retryReturning(times, backOffInMillis, supplier, exceptions);
}

/**
* If one of the specified exceptions occur while running the given supplier, it
* retries running the supplier [times] times with [backOffInMillis]ms in
* between tries. It returns the return-value of the given supplier.
* <p>
* If the number of retries are used up and the supplier still throws an
* exception, that exception is re-thrown.
*
* @param <T> the return-value of the {@link Supplier}
* @param supplier the {@link Supplier} that could throw the exception to
* retry upon
* @param times the number of times to retry before throwing the
* exception for real
* @param backOffInMillis the time to wait in between retries
* @param exceptions a List of Exception-types to retry upon
* @return the return-value of the {@link Supplier}
*/
@SafeVarargs
public static <T> T retryReturning(final int times, final long backOffInMillis, final Supplier<T> supplier,
final Class<?>... exceptions) {
T result = null;
for (int i = 0; i < times; i++)
try {
result = supplier.get();
} catch (Exception e) {
if (!containsException(e, exceptions) || i == times - 1)
throw e;
try {
Thread.sleep(backOffInMillis);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
}
}
return result;
}

public static boolean containsException(final Exception e, final Class<?>... exceptions) {
for (Class<?> omit : exceptions)
if (omit.isAssignableFrom(e.getClass()))
return true;
return false;
}
}

0 comments on commit 60c23a7

Please sign in to comment.