Skip to content

Commit

Permalink
refactor: improve NPE message for this/other methods
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 30, 2024
1 parent 1a202b1 commit 78caa97
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2370,7 +2370,7 @@ public static <T, U1, U2, U3, U4, U5> CompletableFuture<Tuple5<U1, U2, U3, U4, U
public static <T, U, V> CompletableFuture<V> thenCombineFastFail(
CompletableFuture<? extends T> cfThis, CompletionStage<? extends U> other,
BiFunction<? super T, ? super U, ? extends V> fn) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(fn, "fn is null");

final Object[] result = new Object[css.length];
Expand Down Expand Up @@ -2411,7 +2411,7 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(
public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(
CompletableFuture<? extends T> cfThis, CompletionStage<? extends U> other,
BiFunction<? super T, ? super U, ? extends V> fn, Executor executor) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(fn, "fn is null");
requireNonNull(executor, "executor is null");

Expand All @@ -2422,6 +2422,12 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(
.thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1]), executor);
}

@SuppressWarnings("unchecked")
private static <T> CompletionStage<? extends T>[] requireThisAndOtherNonNull(
CompletionStage<? extends T> cfThis, CompletionStage<? extends T> other) {
return new CompletionStage[]{requireNonNull(cfThis, "cfThis is null"), requireNonNull(other, "other is null")};
}

/**
* Returns a new CompletableFuture that, when tow given stage both complete normally,
* is executed with the two results as arguments to the supplied action.
Expand All @@ -2436,7 +2442,7 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(
public static <T, U> CompletableFuture<Void> thenAcceptBothFastFail(
CompletableFuture<? extends T> cfThis, CompletionStage<? extends U> other,
BiConsumer<? super T, ? super U> action) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");

final Object[] result = new Object[css.length];
Expand Down Expand Up @@ -2477,7 +2483,7 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(
public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(
CompletableFuture<? extends T> cfThis, CompletionStage<? extends U> other,
BiConsumer<? super T, ? super U> action, Executor executor) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

Expand All @@ -2498,7 +2504,7 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(
*/
public static CompletableFuture<Void> runAfterBothFastFail(
CompletableFuture<?> cfThis, CompletionStage<?> other, Runnable action) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");

return allFastFailOf(css).thenRun(action);
Expand Down Expand Up @@ -2531,7 +2537,7 @@ public static CompletableFuture<Void> runAfterBothFastFailAsync(
*/
public static CompletableFuture<Void> runAfterBothFastFailAsync(
CompletableFuture<?> cfThis, CompletionStage<?> other, Runnable action, Executor executor) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

Expand All @@ -2557,7 +2563,7 @@ public static CompletableFuture<Void> runAfterBothFastFailAsync(
*/
public static <T, U> CompletableFuture<U> applyToEitherSuccess(
CompletableFuture<? extends T> cfThis, CompletionStage<? extends T> other, Function<? super T, ? extends U> fn) {
final CompletionStage<? extends T>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<? extends T>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(fn, "fn is null");

return anySuccessOf(css).thenApply(fn);
Expand Down Expand Up @@ -2589,7 +2595,7 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(
public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(
CompletableFuture<? extends T> cfThis, CompletionStage<? extends T> other,
Function<? super T, ? extends U> fn, Executor executor) {
final CompletionStage<? extends T>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<? extends T>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(fn, "fn is null");
requireNonNull(executor, "executor is null");

Expand All @@ -2605,7 +2611,7 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(
*/
public static <T> CompletableFuture<Void> acceptEitherSuccess(
CompletableFuture<? extends T> cfThis, CompletionStage<? extends T> other, Consumer<? super T> action) {
final CompletionStage<? extends T>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<? extends T>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");

return anySuccessOf(css).thenAccept(action);
Expand Down Expand Up @@ -2635,7 +2641,7 @@ public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(
public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(
CompletableFuture<? extends T> cfThis, CompletionStage<? extends T> other,
Consumer<? super T> action, Executor executor) {
final CompletionStage<? extends T>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<? extends T>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

Expand All @@ -2653,7 +2659,7 @@ public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(
*/
public static CompletableFuture<Void> runAfterEitherSuccess(
CompletableFuture<?> cfThis, CompletionStage<?> other, Runnable action) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");

return anySuccessOf(css).thenRun(action);
Expand Down Expand Up @@ -2686,7 +2692,7 @@ public static CompletableFuture<Void> runAfterEitherSuccessAsync(
*/
public static CompletableFuture<Void> runAfterEitherSuccessAsync(
CompletableFuture<?> cfThis, CompletionStage<?> other, Runnable action, Executor executor) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cfThis, other);
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

Expand Down

0 comments on commit 78caa97

Please sign in to comment.