/*
 * Copyright 2016 PantherCode
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.panthercode.arctic.core.processing.modules.impl;

import org.panthercode.arctic.core.helper.identity.IdentityInfo;
import org.panthercode.arctic.core.helper.version.VersionInfo;
import org.panthercode.arctic.core.processing.ProcessState;
import org.panthercode.arctic.core.processing.ProcessException;
import org.panthercode.arctic.core.processing.modules.Module;
import org.panthercode.arctic.core.settings.Context;

//TODO: implement reset()

/**
 * The Chunk class runs all elements parallelised.
 */
@IdentityInfo(name = "Standard Chunk", group = "Chunk Module")
@VersionInfo(major = 1)
public class Chunk extends Bundle {

    /**
     * Standard Constructor
     */
    public Chunk() {
        super();
    }

    /**
     * Constructor
     *
     * @param context context the module is associated with.
     */
    public Chunk(Context context) {
        super(context);
    }

    /**
     * Copy Constructor
     *
     * @param chunk object to copy
     */
    public Chunk(Chunk chunk)
            throws UnsupportedOperationException, NullPointerException {
        super(chunk);
    }

    /**
     * Starts the process.
     *
     * @throws Exception Is eventually thrown by actual module or if an error occurred while running process.
     */
    public synchronized boolean start()
            throws ProcessException {
        // Todo: implement
        return false;
    }

    /**
     * Stops the actual process. It's not guaranteed that process stops immediately.
     * Calls the <tt>stop()</tt> method of each module.
     *
     * @throws Exception Is eventually thrown by actual module or if an error occurred while stopping process.
     */
    public synchronized boolean stop()
            throws ProcessException {
        for (Module module : this.modules()) {
            //Todo ignore exceptions, rethrow it after stopping all other modules
            module.stop();
        }

        return this.canChangeState(ProcessState.STOPPED);
    }

    @Override
    public boolean reset() throws ProcessException {
        //Todo: implement
        return false;
    }

    /**
     * @return
     * @throws UnsupportedOperationException
     */
    @Override
    public Chunk copy()
            throws UnsupportedOperationException {
        return new Chunk(this);
    }

    /**
     * Returns a hash code value of this object.
     *
     * @return Returns a hash code value of this object.
     */
    @Override
    public int hashCode() {
        return super.hashCode();
    }

    /**
     * Checks if this object is equals to another one.
     *
     * @param obj other object for comparison
     * @return Returns <code>true</code> if both objects are equal; Otherwise <tt>false</tt>.
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == this) {
            return true;
        }

        if (!(obj instanceof Chunk)) {
            return false;
        }

        Chunk chunk = (Chunk) obj;

        return super.equals(chunk);
    }
}
@@ -834,21 +834,25 @@
 .Process
-State
+Exception
 ;%0Aimport
@@ -894,33 +894,29 @@
 sing.Process
-Exception
+State
 ;%0Aimport org
@@ -1021,16 +1021,128 @@
 ntext;%0A%0A
+import java.util.Iterator;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A%0A
 //TODO: 
@@ -1343,16 +1343,78 @@
 ndle %7B%0A%0A
+    //TODO: check parameter%0A    private int threadCount = 0;%0A%0A
     /**%0A
@@ -1466,34 +1466,65 @@
 c Chunk(
-) %7B%0A        super(
+int threadCount) %7B%0A        this(threadCount, null
 );%0A    %7D
@@ -1645,16 +1645,33 @@
 c Chunk(
+int threadCount, 
 Context 
@@ -1704,16 +1704,57 @@
 ontext);
+%0A%0A        this.threadCount = threadCount;
 %0A    %7D%0A%0A
@@ -2217,26 +2217,633 @@
     
-// Todo: implement
+if (this.modules().isEmpty()) %7B%0A            return true;%0A        %7D%0A%0A        //TODO: change state%0A%0A        this.reset();%0A%0A        //ExecuterService muste be class member%0A        ExecutorService executor = Executors.newFixedThreadPool(this.threadCount);%0A%0A        Iterator%3CModule%3E iterator = this.modules().iterator();%0A%0A        while (iterator.hasNext()) %7B%0A            final Module current = iterator.next();%0A%0A            executor.execute(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    current.start();%0A                %7D%0A            %7D);%0A        %7D%0A%0A        //set Process state%0A
 %0A   

modify implementation