/*
 *  The MIT License
 *
 *   Copyright (c) 2016, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

package org.easybatch.core.job;

import org.easybatch.core.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import static java.lang.Runtime.getRuntime;
import static java.util.concurrent.Executors.newFixedThreadPool;

/**
 * Main class to execute {@link Job}s.
 *
 * <strong>Job executors must be explicitly shutdown using {@link JobExecutor#shutdown()}</strong>
 *
 * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 */
public class JobExecutor {

    private ExecutorService executorService;

    /**
     * Create a job executor. The number of workers will be set to the number of available processors.
     */
    public JobExecutor() {
        executorService = newFixedThreadPool(getRuntime().availableProcessors());
    }

    /**
     * Create a job executor.
     *
     * @param nbWorkers number of worker threads
     */
    public JobExecutor(int nbWorkers) {
        executorService = newFixedThreadPool(nbWorkers);
    }

    /**
     * Create a job executor.
     *
     * @param executorService to use to execute jobs
     */
    public JobExecutor(ExecutorService executorService) {
        Utils.checkNotNull(executorService, "executor service");
        this.executorService = executorService;
    }

    /**
     * Execute a job synchronously.
     *
     * @param job to execute
     * @return the job report
     */
    public JobReport execute(Job job) {
        try {
            return executorService.submit(job).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException("Unable to execute job " + job.getName(), e);
        }
    }

    /**
     * Submit a job for asynchronous execution.
     *
     * @param job to execute
     * @return a future of the job report
     */
    public Future<JobReport> submit(Job job) {
        return executorService.submit(job);
    }

    /**
     * Submit jobs for execution.
     *
     * @param jobs to execute
     * @return the list of job reports in the same order of submission
     */
    public List<Future<JobReport>> submitAll(Job... jobs) {
        List<Job> jobList = new ArrayList<>();
        Collections.addAll(jobList, jobs);
        try {
            return executorService.invokeAll(jobList);
        } catch (InterruptedException e) {
            throw new RuntimeException("Unable to execute jobs", e);
        }
    }

    /**
     * Shutdown the job executor.
     */
    public void shutdown() {
        executorService.shutdown();
    }

}

@@ -1304,41 +1304,8 @@
 rray
-List;%0Aimport java.util.Collection
 s;%0Ai
@@ -3442,89 +3442,274 @@
     
-List%3CJob%3E jobList = new ArrayList%3C%3E();%0A        Collections.add
+return submitAll(Arrays.asList(jobs));%0A    %7D%0A%0A    /**%0A     * Submit jobs for execution.%0A     *%0A     * @param jobs to execute%0A     * @return the list of job reports in the same order of submission%0A     */%0A    public List%3CFuture%3CJobReport%3E%3E submit
 All(
-job
 List
-,
+%3CJob%3E
  jobs)
-;
+ %7B
 %0A   
@@ -3767,20 +3767,17 @@
 eAll(job
-List
+s
 );%0A     

add method to submit a list of jobs to the JobExecutor