Skip to content

Commit 60c23a7

Browse files
author
Gerald Unterrainer
committed
Merge branch 'develop'
2 parents 56ec6a4 + 22e889b commit 60c23a7

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>jre-utils</artifactId>
13-
<version>0.2.3</version>
13+
<version>0.2.4</version>
1414
<name>JreUtils</name>
1515
<packaging>jar</packaging>
1616

src/main/java/info/unterrainer/commons/jreutils/Exceptions.java

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,109 @@
88
public class Exceptions {
99

1010
/**
11-
* Swallows any given Exception (checked or unchecked alike) silently.
11+
* Swallows any given Exception (checked or unchecked alike) silently that is
12+
* thrown when running the given supplier.
1213
*
1314
* @param runnable the {@link Runnable} that could throw the exception to
1415
* swallow
15-
* @param throwables a List of Exception-types
16+
* @param exceptions a List of Exception-types
1617
*/
1718
@SafeVarargs
18-
public static void swallow(final Runnable runnable, final Class<?>... throwables) {
19+
public static void swallow(final Runnable runnable, final Class<?>... exceptions) {
1920
Supplier<Void> supplier = () -> {
2021
runnable.run();
2122
return null;
2223
};
23-
swallowReturning(supplier, throwables);
24+
swallowReturning(supplier, exceptions);
2425
}
2526

2627
/**
27-
* Swallows any given Exception (checked or unchecked alike) silently.
28+
* Swallows any given Exception (checked or unchecked alike) silently that is
29+
* thrown when running the given supplier and returns the return-value
30+
* otherwise.
2831
*
2932
* @param <T> the return-value of the {@link Supplier}
3033
* @param supplier the {@link Supplier} that could throw the exception to
3134
* swallow
32-
* @param throwables a List of Exception-types
35+
* @param exceptions a List of Exception-types
3336
* @return the return-value of the {@link Supplier}
3437
*/
3538
@SafeVarargs
36-
public static <T> T swallowReturning(final Supplier<T> supplier, final Class<?>... throwables) {
39+
public static <T> T swallowReturning(final Supplier<T> supplier, final Class<?>... exceptions) {
3740
try {
3841
return supplier.get();
39-
} catch (Exception throwable) {
40-
boolean swallow = false;
41-
for (Class<?> omit : throwables)
42-
if (omit.isAssignableFrom(throwable.getClass())) {
43-
swallow = true;
44-
break;
45-
}
46-
if (!swallow)
47-
throw throwable;
42+
} catch (Exception e) {
43+
if (!containsException(e, exceptions))
44+
throw e;
4845
return null;
4946
}
5047
}
48+
49+
/**
50+
* If one of the specified exceptions occur while running the given supplier, it
51+
* retries running the supplier [times] times with [backOffInMillis]ms in
52+
* between tries.
53+
* <p>
54+
* If the number of retries are used up and the supplier still throws an
55+
* exception, that exception is re-thrown.
56+
*
57+
* @param runnable the {@link Runnable} that could throw the exception to
58+
* swallow
59+
* @param times the number of times to retry before throwing the
60+
* exception for real
61+
* @param backOffInMillis the time to wait in between retries
62+
* @param exceptions a List of Exception-types to retry upon
63+
*/
64+
@SafeVarargs
65+
public static void retry(final int times, final long backOffInMillis, final Runnable runnable,
66+
final Class<?>... exceptions) {
67+
Supplier<Void> supplier = () -> {
68+
runnable.run();
69+
return null;
70+
};
71+
retryReturning(times, backOffInMillis, supplier, exceptions);
72+
}
73+
74+
/**
75+
* If one of the specified exceptions occur while running the given supplier, it
76+
* retries running the supplier [times] times with [backOffInMillis]ms in
77+
* between tries. It returns the return-value of the given supplier.
78+
* <p>
79+
* If the number of retries are used up and the supplier still throws an
80+
* exception, that exception is re-thrown.
81+
*
82+
* @param <T> the return-value of the {@link Supplier}
83+
* @param supplier the {@link Supplier} that could throw the exception to
84+
* retry upon
85+
* @param times the number of times to retry before throwing the
86+
* exception for real
87+
* @param backOffInMillis the time to wait in between retries
88+
* @param exceptions a List of Exception-types to retry upon
89+
* @return the return-value of the {@link Supplier}
90+
*/
91+
@SafeVarargs
92+
public static <T> T retryReturning(final int times, final long backOffInMillis, final Supplier<T> supplier,
93+
final Class<?>... exceptions) {
94+
T result = null;
95+
for (int i = 0; i < times; i++)
96+
try {
97+
result = supplier.get();
98+
} catch (Exception e) {
99+
if (!containsException(e, exceptions) || i == times - 1)
100+
throw e;
101+
try {
102+
Thread.sleep(backOffInMillis);
103+
} catch (InterruptedException e1) {
104+
Thread.currentThread().interrupt();
105+
}
106+
}
107+
return result;
108+
}
109+
110+
public static boolean containsException(final Exception e, final Class<?>... exceptions) {
111+
for (Class<?> omit : exceptions)
112+
if (omit.isAssignableFrom(e.getClass()))
113+
return true;
114+
return false;
115+
}
51116
}

0 commit comments

Comments
 (0)