Skip to content

Commit

Permalink
Replace BiFunction with BinaryOperator (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
stIncMale committed Apr 30, 2024
1 parent f4bc1cb commit d937915
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.mongodb.lang.Nullable;

import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;

import static com.mongodb.assertions.Assertions.assertFalse;
Expand Down Expand Up @@ -110,9 +110,9 @@ public RetryState() {
* </ul>
* The exception thrown represents the failed result of the associated retryable activity,
* i.e., the caller must not do any more attempts.
* @see #advanceOrThrow(Throwable, BiFunction, BiPredicate)
* @see #advanceOrThrow(Throwable, BinaryOperator, BiPredicate)
*/
void advanceOrThrow(final RuntimeException attemptException, final BiFunction<Throwable, Throwable, Throwable> exceptionTransformer,
void advanceOrThrow(final RuntimeException attemptException, final BinaryOperator<Throwable> exceptionTransformer,
final BiPredicate<RetryState, Throwable> retryPredicate) throws RuntimeException {
try {
doAdvanceOrThrow(attemptException, exceptionTransformer, retryPredicate, true);
Expand All @@ -127,9 +127,9 @@ void advanceOrThrow(final RuntimeException attemptException, final BiFunction<Th
* This method is intended to be used by code that generally handles all {@link Throwable} types explicitly,
* which is usually asynchronous code.
*
* @see #advanceOrThrow(RuntimeException, BiFunction, BiPredicate)
* @see #advanceOrThrow(RuntimeException, BinaryOperator, BiPredicate)
*/
void advanceOrThrow(final Throwable attemptException, final BiFunction<Throwable, Throwable, Throwable> exceptionTransformer,
void advanceOrThrow(final Throwable attemptException, final BinaryOperator<Throwable> exceptionTransformer,
final BiPredicate<RetryState, Throwable> retryPredicate) throws Throwable {
doAdvanceOrThrow(attemptException, exceptionTransformer, retryPredicate, false);
}
Expand All @@ -140,7 +140,7 @@ void advanceOrThrow(final Throwable attemptException, final BiFunction<Throwable
* as {@link RetryState} does not have any source of {@link Exception}s.
*/
private void doAdvanceOrThrow(final Throwable attemptException,
final BiFunction<Throwable, Throwable, Throwable> exceptionTransformer,
final BinaryOperator<Throwable> exceptionTransformer,
final BiPredicate<RetryState, Throwable> retryPredicate,
final boolean onlyRuntimeExceptions) throws Throwable {
assertTrue(attempt() < attempts);
Expand All @@ -166,10 +166,10 @@ private void doAdvanceOrThrow(final Throwable attemptException,
}

/**
* @param onlyRuntimeExceptions See {@link #doAdvanceOrThrow(Throwable, BiFunction, BiPredicate, boolean)}.
* @param onlyRuntimeExceptions See {@link #doAdvanceOrThrow(Throwable, BinaryOperator, BiPredicate, boolean)}.
*/
private static Throwable transformException(@Nullable final Throwable previouslyChosenException, final Throwable attemptException,
final boolean onlyRuntimeExceptions, final BiFunction<Throwable, Throwable, Throwable> exceptionTransformer) {
final boolean onlyRuntimeExceptions, final BinaryOperator<Throwable> exceptionTransformer) {
if (onlyRuntimeExceptions && previouslyChosenException != null) {
assertTrue(isRuntime(previouslyChosenException));
}
Expand All @@ -194,7 +194,7 @@ private static Throwable transformException(@Nullable final Throwable previously

/**
* @param readOnlyRetryState Must not be mutated by this method.
* @param onlyRuntimeExceptions See {@link #doAdvanceOrThrow(Throwable, BiFunction, BiPredicate, boolean)}.
* @param onlyRuntimeExceptions See {@link #doAdvanceOrThrow(Throwable, BinaryOperator, BiPredicate, boolean)}.
*/
private boolean shouldRetry(final RetryState readOnlyRetryState, final Throwable attemptException, final Throwable newlyChosenException,
final boolean onlyRuntimeExceptions, final BiPredicate<RetryState, Throwable> retryPredicate) {
Expand Down Expand Up @@ -227,7 +227,7 @@ private static boolean isRuntime(@Nullable final Throwable exception) {
* by the caller to complete the ongoing attempt.
* <p>
* If this method is called from
* {@linkplain RetryingSyncSupplier#RetryingSyncSupplier(RetryState, BiFunction, BiPredicate, Supplier)
* {@linkplain RetryingSyncSupplier#RetryingSyncSupplier(RetryState, BinaryOperator, BiPredicate, Supplier)
* retry predicate / failed result transformer}, the behavior is unspecified.
*
* @param predicate {@code true} iff retrying needs to be broken.
Expand Down Expand Up @@ -265,7 +265,7 @@ public void breakAndThrowIfRetryAnd(final Supplier<Boolean> predicate) throws Ru
* but instead of throwing an exception, it relays it to the {@code callback}.
* <p>
* If this method is called from
* {@linkplain RetryingAsyncCallbackSupplier#RetryingAsyncCallbackSupplier(RetryState, BiFunction, BiPredicate, com.mongodb.internal.async.function.AsyncCallbackSupplier)
* {@linkplain RetryingAsyncCallbackSupplier#RetryingAsyncCallbackSupplier(RetryState, BinaryOperator, BiPredicate, AsyncCallbackSupplier)
* retry predicate / failed result transformer}, the behavior is unspecified.
*
* @return {@code true} iff the {@code callback} was completed, which happens in the same situations in which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.mongodb.lang.NonNull;
import com.mongodb.lang.Nullable;

import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;

/**
Expand All @@ -41,7 +41,7 @@
public final class RetryingAsyncCallbackSupplier<R> implements AsyncCallbackSupplier<R> {
private final RetryState state;
private final BiPredicate<RetryState, Throwable> retryPredicate;
private final BiFunction<Throwable, Throwable, Throwable> failedResultTransformer;
private final BinaryOperator<Throwable> failedResultTransformer;
private final AsyncCallbackSupplier<R> asyncFunction;

/**
Expand Down Expand Up @@ -75,7 +75,7 @@ public final class RetryingAsyncCallbackSupplier<R> implements AsyncCallbackSupp
*/
public RetryingAsyncCallbackSupplier(
final RetryState state,
final BiFunction<Throwable, Throwable, Throwable> failedResultTransformer,
final BinaryOperator<Throwable> failedResultTransformer,
final BiPredicate<RetryState, Throwable> retryPredicate,
final AsyncCallbackSupplier<R> asyncFunction) {
this.state = state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import com.mongodb.annotations.NotThreadSafe;

import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;

/**
Expand All @@ -37,11 +37,11 @@
public final class RetryingSyncSupplier<R> implements Supplier<R> {
private final RetryState state;
private final BiPredicate<RetryState, Throwable> retryPredicate;
private final BiFunction<Throwable, Throwable, Throwable> failedResultTransformer;
private final BinaryOperator<Throwable> failedResultTransformer;
private final Supplier<R> syncFunction;

/**
* See {@link RetryingAsyncCallbackSupplier#RetryingAsyncCallbackSupplier(RetryState, BiFunction, BiPredicate, AsyncCallbackSupplier)}
* See {@link RetryingAsyncCallbackSupplier#RetryingAsyncCallbackSupplier(RetryState, BinaryOperator, BiPredicate, AsyncCallbackSupplier)}
* for the documentation of the parameters.
*
* @param failedResultTransformer Even though the {@code failedResultTransformer} accepts {@link Throwable},
Expand All @@ -51,7 +51,7 @@ public final class RetryingSyncSupplier<R> implements Supplier<R> {
*/
public RetryingSyncSupplier(
final RetryState state,
final BiFunction<Throwable, Throwable, Throwable> failedResultTransformer,
final BinaryOperator<Throwable> failedResultTransformer,
final BiPredicate<RetryState, Throwable> retryPredicate,
final Supplier<R> syncFunction) {
this.state = state;
Expand Down

0 comments on commit d937915

Please sign in to comment.