@@ -1144,93 +1144,6 @@ public static <T, U> CompletableFuture<List<U>> forEach(@Nonnull final Iterable<
11441144 }, executor ).thenApply (ignored -> Arrays .asList ((U [])resultArray ));
11451145 }
11461146
1147- /**
1148- * Method that provides the functionality of a for loop, however, in an asynchronous way. The result of this method
1149- * is a {@link CompletableFuture} that represents the result of the last iteration of the loop body.
1150- * @param startI an integer analogous to the starting value of a loop variable in a for loop
1151- * @param startU an object of some type {@code U} that represents some initial state that is passed to the loop's
1152- * initial state
1153- * @param conditionPredicate a predicate on the loop variable that must be true before the next iteration is
1154- * entered; analogous to the condition in a for loop
1155- * @param stepFunction a unary operator used for modifying the loop variable after each iteration
1156- * @param body a bi-function to be called for each iteration; this function is initially invoked using
1157- * {@code startI} and {@code startU}; the result of the body is then passed into the next iterator's body
1158- * together with a new value for the loop variable. In this way callers can access state inside an iteration
1159- * that was computed in a previous iteration.
1160- * @param executor the executor
1161- * @param <U> the type of the result of the body {@link BiFunction}
1162- * @return a {@link CompletableFuture} containing the result of the last iteration's body invocation.
1163- */
1164- @ Nonnull
1165- public static <U > CompletableFuture <U > forLoop (final int startI , @ Nullable final U startU ,
1166- @ Nonnull final IntPredicate conditionPredicate ,
1167- @ Nonnull final IntUnaryOperator stepFunction ,
1168- @ Nonnull final BiFunction <Integer , U , CompletableFuture <U >> body ,
1169- @ Nonnull final Executor executor ) {
1170- final AtomicInteger loopVariableAtomic = new AtomicInteger (startI );
1171- final AtomicReference <U > lastResultAtomic = new AtomicReference <>(startU );
1172- return whileTrue (() -> {
1173- final int loopVariable = loopVariableAtomic .get ();
1174- if (!conditionPredicate .test (loopVariable )) {
1175- return AsyncUtil .READY_FALSE ;
1176- }
1177- return body .apply (loopVariable , lastResultAtomic .get ())
1178- .thenApply (result -> {
1179- loopVariableAtomic .set (stepFunction .applyAsInt (loopVariable ));
1180- lastResultAtomic .set (result );
1181- return true ;
1182- });
1183- }, executor ).thenApply (ignored -> lastResultAtomic .get ());
1184- }
1185-
1186- /**
1187- * Method to iterate over some items, for each of which a body is executed asynchronously. The result of each such
1188- * executed is then collected in a list and returned as a {@link CompletableFuture} over that list.
1189- * @param items the items to iterate over
1190- * @param body a function to be called for each item
1191- * @param parallelism the maximum degree of parallelism this method should use
1192- * @param executor the executor
1193- * @param <T> the type of item
1194- * @param <U> the type of the result
1195- * @return a {@link CompletableFuture} containing a list of results collected from the individual body invocations
1196- */
1197- @ Nonnull
1198- @ SuppressWarnings ("unchecked" )
1199- public static <T , U > CompletableFuture <List <U >> forEach (@ Nonnull final Iterable <T > items ,
1200- @ Nonnull final Function <T , CompletableFuture <U >> body ,
1201- final int parallelism ,
1202- @ Nonnull final Executor executor ) {
1203- // this deque is only modified by once upon creation
1204- final ArrayDeque <T > toBeProcessed = new ArrayDeque <>();
1205- for (final T item : items ) {
1206- toBeProcessed .addLast (item );
1207- }
1208-
1209- final List <CompletableFuture <Void >> working = Lists .newArrayList ();
1210- final AtomicInteger indexAtomic = new AtomicInteger (0 );
1211- final Object [] resultArray = new Object [toBeProcessed .size ()];
1212-
1213- return whileTrue (() -> {
1214- working .removeIf (CompletableFuture ::isDone );
1215-
1216- while (working .size () <= parallelism ) {
1217- final T currentItem = toBeProcessed .pollFirst ();
1218- if (currentItem == null ) {
1219- break ;
1220- }
1221-
1222- final int index = indexAtomic .getAndIncrement ();
1223- working .add (body .apply (currentItem )
1224- .thenAccept (result -> resultArray [index ] = result ));
1225- }
1226-
1227- if (working .isEmpty ()) {
1228- return AsyncUtil .READY_FALSE ;
1229- }
1230- return whenAny (working ).thenApply (ignored -> true );
1231- }, executor ).thenApply (ignored -> Arrays .asList ((U [])resultArray ));
1232- }
1233-
12341147 /**
12351148 * A {@code Boolean} function that is always true.
12361149 * @param <T> the type of the (ignored) argument to the function
0 commit comments