@@ -456,13 +456,7 @@ public static List<Object> awaitAll(Object... awaitables) {
456456 if (awaitables == null || awaitables .length == 0 ) {
457457 return new ArrayList <>();
458458 }
459- for (int i = 0 ; i < awaitables .length ; i ++) {
460- if (awaitables [i ] == null ) {
461- throw new IllegalArgumentException ("awaitAll: element at index " + i + " is null" );
462- }
463- }
464- CompletableFuture <?>[] futures =
465- Arrays .stream (awaitables ).map (AsyncSupport ::toCompletableFuture ).toArray (CompletableFuture []::new );
459+ CompletableFuture <?>[] futures = toCompletableFutures (awaitables , "awaitAll" );
466460 try {
467461 CompletableFuture .allOf (futures ).join ();
468462 } catch (CompletionException e ) {
@@ -490,13 +484,7 @@ public static Object awaitAny(Object... awaitables) {
490484 if (awaitables == null || awaitables .length == 0 ) {
491485 throw new IllegalArgumentException ("awaitAny requires at least one awaitable" );
492486 }
493- for (int i = 0 ; i < awaitables .length ; i ++) {
494- if (awaitables [i ] == null ) {
495- throw new IllegalArgumentException ("awaitAny: element at index " + i + " is null" );
496- }
497- }
498- CompletableFuture <?>[] futures =
499- Arrays .stream (awaitables ).map (AsyncSupport ::toCompletableFuture ).toArray (CompletableFuture []::new );
487+ CompletableFuture <?>[] futures = toCompletableFutures (awaitables , "awaitAny" );
500488 try {
501489 return CompletableFuture .anyOf (futures ).join ();
502490 } catch (CompletionException e ) {
@@ -521,13 +509,7 @@ public static List<AwaitResult<Object>> awaitAllSettled(Object... awaitables) {
521509 if (awaitables == null || awaitables .length == 0 ) {
522510 return new ArrayList <>();
523511 }
524- for (int i = 0 ; i < awaitables .length ; i ++) {
525- if (awaitables [i ] == null ) {
526- throw new IllegalArgumentException ("awaitAllSettled: element at index " + i + " is null" );
527- }
528- }
529- CompletableFuture <?>[] futures =
530- Arrays .stream (awaitables ).map (AsyncSupport ::toCompletableFuture ).toArray (CompletableFuture []::new );
512+ CompletableFuture <?>[] futures = toCompletableFutures (awaitables , "awaitAllSettled" );
531513 CompletableFuture .allOf (
532514 Arrays .stream (futures )
533515 .map (f -> f .handle ((v , t ) -> null ))
@@ -545,6 +527,29 @@ private static CompletableFuture<?> toCompletableFuture(Object source) {
545527 return Awaitable .from (source ).toCompletableFuture ();
546528 }
547529
530+ /**
531+ * Validates that no element is {@code null} and converts all elements to
532+ * {@link CompletableFuture}s in one pass. Used by combinator methods
533+ * ({@code awaitAll}, {@code awaitAny}, {@code awaitAllSettled}, and their
534+ * non-blocking {@code Async} variants) to eliminate duplicate
535+ * null-checking and conversion loops.
536+ *
537+ * @param sources the source objects to validate and convert
538+ * @param callerName the method name for error messages
539+ * @return an array of completable futures corresponding to the sources
540+ * @throws IllegalArgumentException if any element is {@code null}
541+ */
542+ private static CompletableFuture <?>[] toCompletableFutures (Object [] sources , String callerName ) {
543+ CompletableFuture <?>[] futures = new CompletableFuture <?>[sources .length ];
544+ for (int i = 0 ; i < sources .length ; i ++) {
545+ if (sources [i ] == null ) {
546+ throw new IllegalArgumentException (callerName + ": element at index " + i + " is null" );
547+ }
548+ futures [i ] = toCompletableFuture (sources [i ]);
549+ }
550+ return futures ;
551+ }
552+
548553 // ---- non-blocking combinators (return Awaitable) --------------------
549554
550555 /**
@@ -556,13 +561,7 @@ public static Awaitable<List<Object>> allAsync(Object... sources) {
556561 if (sources == null || sources .length == 0 ) {
557562 return Awaitable .of (new ArrayList <>());
558563 }
559- CompletableFuture <?>[] futures = new CompletableFuture [sources .length ];
560- for (int i = 0 ; i < sources .length ; i ++) {
561- if (sources [i ] == null ) {
562- throw new IllegalArgumentException ("Awaitable.all: element at index " + i + " is null" );
563- }
564- futures [i ] = toCompletableFuture (sources [i ]);
565- }
564+ CompletableFuture <?>[] futures = toCompletableFutures (sources , "Awaitable.all" );
566565 CompletableFuture <List <Object >> combined = CompletableFuture .allOf (futures )
567566 .thenApply (v -> {
568567 List <Object > results = new ArrayList <>(futures .length );
@@ -584,12 +583,7 @@ public static <T> Awaitable<T> anyAsync(Object... sources) {
584583 if (sources == null || sources .length == 0 ) {
585584 throw new IllegalArgumentException ("Awaitable.any requires at least one source" );
586585 }
587- for (int i = 0 ; i < sources .length ; i ++) {
588- if (sources [i ] == null ) {
589- throw new IllegalArgumentException ("Awaitable.any: element at index " + i + " is null" );
590- }
591- }
592- CompletableFuture <?>[] futures = Arrays .stream (sources ).map (AsyncSupport ::toCompletableFuture ).toArray (CompletableFuture []::new );
586+ CompletableFuture <?>[] futures = toCompletableFutures (sources , "Awaitable.any" );
593587 return GroovyPromise .of ((CompletableFuture <T >) CompletableFuture .anyOf (futures ));
594588 }
595589
@@ -602,12 +596,7 @@ public static Awaitable<List<AwaitResult<Object>>> allSettledAsync(Object... sou
602596 if (sources == null || sources .length == 0 ) {
603597 return Awaitable .of (new ArrayList <>());
604598 }
605- for (int i = 0 ; i < sources .length ; i ++) {
606- if (sources [i ] == null ) {
607- throw new IllegalArgumentException ("Awaitable.allSettled: element at index " + i + " is null" );
608- }
609- }
610- CompletableFuture <?>[] futures = Arrays .stream (sources ).map (AsyncSupport ::toCompletableFuture ).toArray (CompletableFuture []::new );
599+ CompletableFuture <?>[] futures = toCompletableFutures (sources , "Awaitable.allSettled" );
611600 // Wait for all to settle (handle converts failures to non-exceptional completions)
612601 CompletableFuture <List <AwaitResult <Object >>> combined = CompletableFuture .allOf (
613602 Arrays .stream (futures )
0 commit comments