|
8 | 8 | public class Exceptions {
|
9 | 9 |
|
10 | 10 | /**
|
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. |
12 | 13 | *
|
13 | 14 | * @param runnable the {@link Runnable} that could throw the exception to
|
14 | 15 | * swallow
|
15 |
| - * @param throwables a List of Exception-types |
| 16 | + * @param exceptions a List of Exception-types |
16 | 17 | */
|
17 | 18 | @SafeVarargs
|
18 |
| - public static void swallow(final Runnable runnable, final Class<?>... throwables) { |
| 19 | + public static void swallow(final Runnable runnable, final Class<?>... exceptions) { |
19 | 20 | Supplier<Void> supplier = () -> {
|
20 | 21 | runnable.run();
|
21 | 22 | return null;
|
22 | 23 | };
|
23 |
| - swallowReturning(supplier, throwables); |
| 24 | + swallowReturning(supplier, exceptions); |
24 | 25 | }
|
25 | 26 |
|
26 | 27 | /**
|
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. |
28 | 31 | *
|
29 | 32 | * @param <T> the return-value of the {@link Supplier}
|
30 | 33 | * @param supplier the {@link Supplier} that could throw the exception to
|
31 | 34 | * swallow
|
32 |
| - * @param throwables a List of Exception-types |
| 35 | + * @param exceptions a List of Exception-types |
33 | 36 | * @return the return-value of the {@link Supplier}
|
34 | 37 | */
|
35 | 38 | @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) { |
37 | 40 | try {
|
38 | 41 | 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; |
48 | 45 | return null;
|
49 | 46 | }
|
50 | 47 | }
|
| 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 | + } |
51 | 116 | }
|
0 commit comments