2020import java .util .Collection ;
2121import java .util .Collections ;
2222import java .util .List ;
23- import java .util .NoSuchElementException ;
2423import java .util .Objects ;
2524import java .util .Optional ;
2625import java .util .concurrent .Callable ;
@@ -147,7 +146,6 @@ public static <T> Promise<List<T>> all(boolean cancelRemaining, List<CompletionS
147146 * if all promises completed exceptionally, then resulting promise is resolved faulty as well.
148147 * <p>The resolved result of this promise contains a value of the first resolved result of the {@link CompletionStage}-s passed as an
149148 * argument.
150- * <p>When <code>promises</code> argument is empty returns faulty-resolved {@link Promise} with {@link NoSuchElementException} fault.
151149 * <p>When resulting promise is resolved successfully, all remaining incomplete {@link CompletionStage}-s are cancelled.
152150 *
153151 * @param <T>
@@ -170,7 +168,6 @@ public static <T> Promise<T> any(List<CompletionStage<? extends T>> promises) {
170168 * if all promises completed exceptionally, then resulting promise is resolved faulty as well.
171169 * <p>The resolved result of this promise contains a value of the first resolved result of the {@link CompletionStage}-s passed as an
172170 * argument.
173- * <p>When <code>promises</code> argument is empty returns faulty-resolved {@link Promise} with {@link NoSuchElementException} fault.
174171 * <p>When resulting promise is resolved successfully <em>and</em> <code>cancelRemaining</code> parameter is <code>true</code>,
175172 * all remaining incomplete {@link CompletionStage}-s are cancelled.
176173 *
@@ -192,9 +189,7 @@ public static <T> Promise<T> any(boolean cancelRemaining, List<CompletionStage<?
192189 int size = null == promises ? 0 : promises .size ();
193190 switch (size ) {
194191 case 0 :
195- @ SuppressWarnings ("unchecked" )
196- Promise <T > emptyResult = (Promise <T >)EMPTY_AGGREGATE_FAILURE ;
197- return emptyResult ;
192+ return insufficientNumberOfArguments (1 , 0 );
198193 case 1 :
199194 @ SuppressWarnings ("unchecked" )
200195 CompletionStage <T > singleResult = (CompletionStage <T >) promises .get (0 );
@@ -213,7 +208,6 @@ public static <T> Promise<T> any(boolean cancelRemaining, List<CompletionStage<?
213208 * (unlike non-Strict variant, where exceptions are ignored if result is available at all).
214209 * <p>The resolved result of this promise contains a value of the first resolved result of the {@link CompletionStage}-s passed as an
215210 * argument.
216- * <p>When <code>promises</code> argument is empty returns faulty-resolved {@link Promise} with {@link NoSuchElementException} fault.
217211 * <p>When resulting promise is resolved either successfully or faulty, all remaining incomplete {@link CompletionStage}-s are cancelled.
218212 * <p>Unlike other methods to combine promises (any, all, atLeast, atLeastStrict), the {@link Promise} returns from this method reports
219213 * exact exception. All other methods wrap it to {@link MultitargetException}.
@@ -239,7 +233,6 @@ public static <T> Promise<T> anyStrict(List<CompletionStage<? extends T>> promis
239233 * (unlike non-Strict variant, where exceptions are ignored if result is available at all).
240234 * <p>The resolved result of this promise contains a value of the first resolved result of the {@link CompletionStage}-s passed as an
241235 * argument.
242- * <p>When <code>promises</code> argument is empty returns faulty-resolved {@link Promise} with {@link NoSuchElementException} fault.
243236 * <p>When resulting promise is resolved either successfully or faulty <em>and</em> <code>cancelRemaining</code> parameter is <code>true</code>,
244237 * all remaining incomplete {@link CompletionStage}-s are cancelled.
245238 * <p>Unlike other methods to combine promises (any, all, atLeast, atLeastStrict), the {@link Promise} returns from this method reports
@@ -262,9 +255,7 @@ public static <T> Promise<T> anyStrict(boolean cancelRemaining, List<CompletionS
262255 int size = null == promises ? 0 : promises .size ();
263256 switch (size ) {
264257 case 0 :
265- @ SuppressWarnings ("unchecked" )
266- Promise <T > emptyResult = (Promise <T >)EMPTY_AGGREGATE_FAILURE ;
267- return emptyResult ;
258+ return insufficientNumberOfArguments (1 , 0 );
268259 case 1 :
269260 @ SuppressWarnings ("unchecked" )
270261 CompletionStage <T > singleResult = (CompletionStage <T >) promises .get (0 );
@@ -430,9 +421,7 @@ public static <T> Promise<List<T>> atLeast(int minResultsCount, int maxErrorsCou
430421
431422 int size = null == promises ? 0 : promises .size ();
432423 if (minResultsCount > size ) {
433- throw new IllegalArgumentException (
434- "The number of futures supplied is less than a number of futures to await"
435- );
424+ return insufficientNumberOfArguments (minResultsCount , size );
436425 } else if (minResultsCount == 0 ) {
437426 return success (Collections .emptyList ());
438427 } else if (size == 1 ) {
@@ -614,6 +603,14 @@ private static <E extends Throwable> MultitargetException wrapMultitargetExcepti
614603 }
615604 }
616605
606+ private static <T > Promise <T > insufficientNumberOfArguments (int minResultCount , int size ) {
607+ String message = String .format (
608+ "The number of futures supplied (%d) is less than a number of futures to await (%d)" , size , minResultCount
609+ );
610+ //return failure(new NoSuchElementException(message));
611+ throw new IllegalArgumentException (message );
612+ }
613+
617614 private static class ObjectRef <T > {
618615 private final T reference ;
619616
@@ -627,5 +624,4 @@ T dereference() {
627624 }
628625
629626 private static final Object IGNORE = new Object ();
630- private static final Promise <Object > EMPTY_AGGREGATE_FAILURE = failure (new NoSuchElementException ());
631627}
0 commit comments