return newList;
}
public final static <S, T> List<Future<T>> transformToFuture(final Iterable<S> sourceList, final ParallelRowConvertor<S, T> convertor) {
final Context context = Context.getCurrentThreadContext();
final Configuration conf = Configuration.getConfiguration();
Boolean isInParallelConverting = context.getData(ParallelListConversionMark);
if (isInParallelConverting != null) {// recursive converting
switch (conf.getParallelRecursivePolicyForListRendering()) {
case EXCEPTION:
throw new RuntimeException(
"Parallel list converting is forbidden (by default) to avoid deadlock. You can change this policy by Configuration.setParallelRecursivePolicyForListRendering().");
case CURRENT_THREAD:
List<T> list = transform(sourceList, (RowConvertor<S, T>) convertor);
return transform(list, new RowConvertor<T, Future<T>>() {
@Override
public Future<T> convert(int rowIndex, final T obj) {
return new Future<T>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return true;
}
@Override
public T get() throws InterruptedException, ExecutionException {
return obj;
}
@Override
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return obj;
}
};
}
});
case NEW_THREAD:
ExecutorService executor = ParallelFallbackExecutor;
List<Future<T>> futureList = new LinkedList<>();
int index = 0;
for (S obj : sourceList) {
futureList.add(convertor.invoke(executor, index, obj));
index++;
}
return futureList;
default:
return Collections.emptyList();
}
} else {// not in recursive converting
Context newContext = context.clone();
newContext.setData(ParallelListConversionMark, Boolean.TRUE);
try {
return Context.with(newContext, new Callable<List<Future<T>>>() {
@Override
public List<Future<T>> call() throws Exception {
ExecutorService executor = conf.getListExecutorFactory().getExecutorService();
List<Future<T>> futureList = new LinkedList<>();
int index = 0;
for (S obj : sourceList) {
futureList.add(convertor.invoke(executor, index, obj));
index++;