20
20
import java .util .Collection ;
21
21
import java .util .Collections ;
22
22
import java .util .List ;
23
- import java .util .NoSuchElementException ;
24
23
import java .util .Objects ;
25
24
import java .util .Optional ;
26
25
import java .util .concurrent .Callable ;
@@ -147,7 +146,6 @@ public static <T> Promise<List<T>> all(boolean cancelRemaining, List<CompletionS
147
146
* if all promises completed exceptionally, then resulting promise is resolved faulty as well.
148
147
* <p>The resolved result of this promise contains a value of the first resolved result of the {@link CompletionStage}-s passed as an
149
148
* argument.
150
- * <p>When <code>promises</code> argument is empty returns faulty-resolved {@link Promise} with {@link NoSuchElementException} fault.
151
149
* <p>When resulting promise is resolved successfully, all remaining incomplete {@link CompletionStage}-s are cancelled.
152
150
*
153
151
* @param <T>
@@ -170,7 +168,6 @@ public static <T> Promise<T> any(List<CompletionStage<? extends T>> promises) {
170
168
* if all promises completed exceptionally, then resulting promise is resolved faulty as well.
171
169
* <p>The resolved result of this promise contains a value of the first resolved result of the {@link CompletionStage}-s passed as an
172
170
* argument.
173
- * <p>When <code>promises</code> argument is empty returns faulty-resolved {@link Promise} with {@link NoSuchElementException} fault.
174
171
* <p>When resulting promise is resolved successfully <em>and</em> <code>cancelRemaining</code> parameter is <code>true</code>,
175
172
* all remaining incomplete {@link CompletionStage}-s are cancelled.
176
173
*
@@ -192,9 +189,7 @@ public static <T> Promise<T> any(boolean cancelRemaining, List<CompletionStage<?
192
189
int size = null == promises ? 0 : promises .size ();
193
190
switch (size ) {
194
191
case 0 :
195
- @ SuppressWarnings ("unchecked" )
196
- Promise <T > emptyResult = (Promise <T >)EMPTY_AGGREGATE_FAILURE ;
197
- return emptyResult ;
192
+ return insufficientNumberOfArguments (1 , 0 );
198
193
case 1 :
199
194
@ SuppressWarnings ("unchecked" )
200
195
CompletionStage <T > singleResult = (CompletionStage <T >) promises .get (0 );
@@ -213,7 +208,6 @@ public static <T> Promise<T> any(boolean cancelRemaining, List<CompletionStage<?
213
208
* (unlike non-Strict variant, where exceptions are ignored if result is available at all).
214
209
* <p>The resolved result of this promise contains a value of the first resolved result of the {@link CompletionStage}-s passed as an
215
210
* argument.
216
- * <p>When <code>promises</code> argument is empty returns faulty-resolved {@link Promise} with {@link NoSuchElementException} fault.
217
211
* <p>When resulting promise is resolved either successfully or faulty, all remaining incomplete {@link CompletionStage}-s are cancelled.
218
212
* <p>Unlike other methods to combine promises (any, all, atLeast, atLeastStrict), the {@link Promise} returns from this method reports
219
213
* 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
239
233
* (unlike non-Strict variant, where exceptions are ignored if result is available at all).
240
234
* <p>The resolved result of this promise contains a value of the first resolved result of the {@link CompletionStage}-s passed as an
241
235
* argument.
242
- * <p>When <code>promises</code> argument is empty returns faulty-resolved {@link Promise} with {@link NoSuchElementException} fault.
243
236
* <p>When resulting promise is resolved either successfully or faulty <em>and</em> <code>cancelRemaining</code> parameter is <code>true</code>,
244
237
* all remaining incomplete {@link CompletionStage}-s are cancelled.
245
238
* <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
262
255
int size = null == promises ? 0 : promises .size ();
263
256
switch (size ) {
264
257
case 0 :
265
- @ SuppressWarnings ("unchecked" )
266
- Promise <T > emptyResult = (Promise <T >)EMPTY_AGGREGATE_FAILURE ;
267
- return emptyResult ;
258
+ return insufficientNumberOfArguments (1 , 0 );
268
259
case 1 :
269
260
@ SuppressWarnings ("unchecked" )
270
261
CompletionStage <T > singleResult = (CompletionStage <T >) promises .get (0 );
@@ -430,9 +421,7 @@ public static <T> Promise<List<T>> atLeast(int minResultsCount, int maxErrorsCou
430
421
431
422
int size = null == promises ? 0 : promises .size ();
432
423
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 );
436
425
} else if (minResultsCount == 0 ) {
437
426
return success (Collections .emptyList ());
438
427
} else if (size == 1 ) {
@@ -614,6 +603,14 @@ private static <E extends Throwable> MultitargetException wrapMultitargetExcepti
614
603
}
615
604
}
616
605
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
+
617
614
private static class ObjectRef <T > {
618
615
private final T reference ;
619
616
@@ -627,5 +624,4 @@ T dereference() {
627
624
}
628
625
629
626
private static final Object IGNORE = new Object ();
630
- private static final Promise <Object > EMPTY_AGGREGATE_FAILURE = failure (new NoSuchElementException ());
631
627
}
0 commit comments