/** * Constructs a base task with a name. * * @param name the name for this task. */ public BaseTask(final String name) { super(Promises.<T>settable()); _name = truncate(name); final State state = State.INIT; _shallowTraceBuilder = new ShallowTraceBuilder(_id); _shallowTraceBuilder.setName(getName()); _shallowTraceBuilder.setResultType(ResultType.UNFINISHED); _stateRef = new AtomicReference<State>(state); }
@Override protected Promise<List<T>> run(final Context context) throws Exception { final SettablePromise<List<T>> result = Promises.settable(); final PromiseListener<?> listener = new PromiseListener<Object>() { @Override public void onResolved(Promise<Object> resolvedPromise) { boolean allEarlyFinish = true; final List<T> taskResult = new ArrayList<T>(); final List<Throwable> errors = new ArrayList<Throwable>(); for (Task<? extends T> task : _tasks) { if (task.isFailed()) { if (allEarlyFinish && ResultType.fromTask(task) != ResultType.EARLY_FINISH) { allEarlyFinish = false; } errors.add(task.getError()); } else { taskResult.add(task.get()); } } if (!errors.isEmpty()) { result.fail( allEarlyFinish ? errors.get(0) : new MultiException("Multiple errors in 'ParTask' task.", errors)); } else { result.done(taskResult); } } }; InternalUtil.after(listener, _tasks.toArray(new Task<?>[_tasks.size()])); for (Task<?> task : _tasks) { context.run(task); } return result; }