com.github.edgar615.util.vertx.task.ParTaskImpl Maven / Gradle / Ivy
package com.github.edgar615.util.vertx.task;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Edgar on 2016/7/21.
*
* @author Edgar Date 2016/7/21
*/
class ParTaskImpl extends BaseTask> {
ParTaskImpl(String name, List> futures) {
super(name, Future.>future());
final int size = futures.size();
List copyFuture = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
copyFuture.add(futures.get(i));
}
CompositeFuture compositeFuture = CompositeFuture.all(copyFuture);
compositeFuture.setHandler(ar -> {
if (ar.succeeded()) {
List results = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
results.add((T) ar.result().resultAt(i));
}
complete(results);
} else {
fail(ar.cause());
}
});
}
ParTaskImpl(List> futures) {
this("ParTask", futures);
}
}