@@ -78,32 +78,33 @@ public RetryState() {
78
78
* which is usually synchronous code.
79
79
*
80
80
* @param attemptException The exception produced by the most recent attempt.
81
- * It is passed to the {@code retryPredicate} and to the {@code exceptionTransformer}.
82
- * @param exceptionTransformer A function that chooses which exception to preserve as a prospective failed result of the associated
83
- * retryable activity and may also transform or mutate the exceptions.
84
- * The choice is between
81
+ * It is passed to the {@code retryPredicate} and to the {@code onAttemptFailureOperator}.
82
+ * @param onAttemptFailureOperator The action that is called once per failed attempt before (in the happens-before order) the
83
+ * {@code retryPredicate}, regardless of whether the {@code retryPredicate} is called.
84
+ * This action is allowed to have side effects.
85
+ * <p>
86
+ * It also has to choose which exception to preserve as a prospective failed result of the associated retryable activity.
87
+ * The {@code onAttemptFailureOperator} may mutate its arguments, choose from the arguments, or return a different exception,
88
+ * but it must return a {@code @}{@link NonNull} value.
89
+ * The choice is between</p>
85
90
* <ul>
86
91
* <li>the previously chosen exception or {@code null} if none has been chosen
87
- * (the first argument of the {@code exceptionTransformer })</li>
88
- * <li>and the exception from the most recent attempt (the second argument of the {@code exceptionTransformer }).</li>
92
+ * (the first argument of the {@code onAttemptFailureOperator })</li>
93
+ * <li>and the exception from the most recent attempt (the second argument of the {@code onAttemptFailureOperator }).</li>
89
94
* </ul>
90
- * The {@code exceptionTransformer} may either choose from its arguments, or return a different exception, a.k.a. transform,
91
- * but it must return a {@code @}{@link NonNull} value.
92
- * The {@code exceptionTransformer} is called once before (in the happens-before order) the {@code retryPredicate},
93
- * regardless of whether the {@code retryPredicate} is called. The result of the {@code exceptionTransformer} does not affect
94
- * what exception is passed to the {@code retryPredicate}.
95
+ * The result of the {@code onAttemptFailureOperator} does not affect the exception passed to the {@code retryPredicate}.
95
96
* @param retryPredicate {@code true} iff another attempt needs to be made. The {@code retryPredicate} is called not more than once
96
97
* per attempt and only if all the following is true:
97
98
* <ul>
98
- * <li>{@code exceptionTransformer } completed normally;</li>
99
+ * <li>{@code onAttemptFailureOperator } completed normally;</li>
99
100
* <li>the most recent attempt is not the {@linkplain #isLastAttempt() last} one.</li>
100
101
* </ul>
101
102
* The {@code retryPredicate} accepts this {@link RetryState} and the exception from the most recent attempt,
102
103
* and may mutate the exception. The {@linkplain RetryState} advances to represent the state of a new attempt
103
104
* after (in the happens-before order) testing the {@code retryPredicate}, and only if the predicate completes normally.
104
105
* @throws RuntimeException Iff any of the following is true:
105
106
* <ul>
106
- * <li>the {@code exceptionTransformer } completed abruptly;</li>
107
+ * <li>the {@code onAttemptFailureOperator } completed abruptly;</li>
107
108
* <li>the most recent attempt is the {@linkplain #isLastAttempt() last} one;</li>
108
109
* <li>the {@code retryPredicate} completed abruptly;</li>
109
110
* <li>the {@code retryPredicate} is {@code false}.</li>
@@ -112,10 +113,10 @@ public RetryState() {
112
113
* i.e., the caller must not do any more attempts.
113
114
* @see #advanceOrThrow(Throwable, BinaryOperator, BiPredicate)
114
115
*/
115
- void advanceOrThrow (final RuntimeException attemptException , final BinaryOperator <Throwable > exceptionTransformer ,
116
+ void advanceOrThrow (final RuntimeException attemptException , final BinaryOperator <Throwable > onAttemptFailureOperator ,
116
117
final BiPredicate <RetryState , Throwable > retryPredicate ) throws RuntimeException {
117
118
try {
118
- doAdvanceOrThrow (attemptException , exceptionTransformer , retryPredicate , true );
119
+ doAdvanceOrThrow (attemptException , onAttemptFailureOperator , retryPredicate , true );
119
120
} catch (RuntimeException | Error unchecked ) {
120
121
throw unchecked ;
121
122
} catch (Throwable checked ) {
@@ -129,18 +130,19 @@ void advanceOrThrow(final RuntimeException attemptException, final BinaryOperato
129
130
*
130
131
* @see #advanceOrThrow(RuntimeException, BinaryOperator, BiPredicate)
131
132
*/
132
- void advanceOrThrow (final Throwable attemptException , final BinaryOperator <Throwable > exceptionTransformer ,
133
+ void advanceOrThrow (final Throwable attemptException , final BinaryOperator <Throwable > onAttemptFailureOperator ,
133
134
final BiPredicate <RetryState , Throwable > retryPredicate ) throws Throwable {
134
- doAdvanceOrThrow (attemptException , exceptionTransformer , retryPredicate , false );
135
+ doAdvanceOrThrow (attemptException , onAttemptFailureOperator , retryPredicate , false );
135
136
}
136
137
137
138
/**
138
139
* @param onlyRuntimeExceptions {@code true} iff the method must expect {@link #exception} and {@code attemptException} to be
139
140
* {@link RuntimeException}s and must not explicitly handle other {@link Throwable} types, of which only {@link Error} is possible
140
141
* as {@link RetryState} does not have any source of {@link Exception}s.
142
+ * @param onAttemptFailureOperator See {@link #advanceOrThrow(RuntimeException, BinaryOperator, BiPredicate)}.
141
143
*/
142
144
private void doAdvanceOrThrow (final Throwable attemptException ,
143
- final BinaryOperator <Throwable > exceptionTransformer ,
145
+ final BinaryOperator <Throwable > onAttemptFailureOperator ,
144
146
final BiPredicate <RetryState , Throwable > retryPredicate ,
145
147
final boolean onlyRuntimeExceptions ) throws Throwable {
146
148
assertTrue (attempt () < attempts );
@@ -149,7 +151,7 @@ private void doAdvanceOrThrow(final Throwable attemptException,
149
151
assertTrue (isRuntime (attemptException ));
150
152
}
151
153
assertTrue (!isFirstAttempt () || exception == null );
152
- Throwable newlyChosenException = transformException (exception , attemptException , onlyRuntimeExceptions , exceptionTransformer );
154
+ Throwable newlyChosenException = callOnAttemptFailureOperator (exception , attemptException , onlyRuntimeExceptions , onAttemptFailureOperator );
153
155
if (isLastAttempt ()) {
154
156
exception = newlyChosenException ;
155
157
throw exception ;
@@ -167,27 +169,31 @@ private void doAdvanceOrThrow(final Throwable attemptException,
167
169
168
170
/**
169
171
* @param onlyRuntimeExceptions See {@link #doAdvanceOrThrow(Throwable, BinaryOperator, BiPredicate, boolean)}.
172
+ * @param onAttemptFailureOperator See {@link #advanceOrThrow(RuntimeException, BinaryOperator, BiPredicate)}.
170
173
*/
171
- private static Throwable transformException (@ Nullable final Throwable previouslyChosenException , final Throwable attemptException ,
172
- final boolean onlyRuntimeExceptions , final BinaryOperator <Throwable > exceptionTransformer ) {
174
+ private static Throwable callOnAttemptFailureOperator (
175
+ @ Nullable final Throwable previouslyChosenException ,
176
+ final Throwable attemptException ,
177
+ final boolean onlyRuntimeExceptions ,
178
+ final BinaryOperator <Throwable > onAttemptFailureOperator ) {
173
179
if (onlyRuntimeExceptions && previouslyChosenException != null ) {
174
180
assertTrue (isRuntime (previouslyChosenException ));
175
181
}
176
182
Throwable result ;
177
183
try {
178
- result = assertNotNull (exceptionTransformer .apply (previouslyChosenException , attemptException ));
184
+ result = assertNotNull (onAttemptFailureOperator .apply (previouslyChosenException , attemptException ));
179
185
if (onlyRuntimeExceptions ) {
180
186
assertTrue (isRuntime (result ));
181
187
}
182
- } catch (Throwable exceptionTransformerException ) {
183
- if (onlyRuntimeExceptions && !isRuntime (exceptionTransformerException )) {
184
- throw exceptionTransformerException ;
188
+ } catch (Throwable onAttemptFailureOperatorException ) {
189
+ if (onlyRuntimeExceptions && !isRuntime (onAttemptFailureOperatorException )) {
190
+ throw onAttemptFailureOperatorException ;
185
191
}
186
192
if (previouslyChosenException != null ) {
187
- exceptionTransformerException .addSuppressed (previouslyChosenException );
193
+ onAttemptFailureOperatorException .addSuppressed (previouslyChosenException );
188
194
}
189
- exceptionTransformerException .addSuppressed (attemptException );
190
- throw exceptionTransformerException ;
195
+ onAttemptFailureOperatorException .addSuppressed (attemptException );
196
+ throw onAttemptFailureOperatorException ;
191
197
}
192
198
return result ;
193
199
}
0 commit comments