/*
 * Copyright (c) 2013 Noveo Group
 *
 * 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.
 *
 * Except as contained in this notice, the name(s) of the above copyright holders
 * shall not be used in advertising or otherwise to promote the sale, use or
 * other dealings in this Software without prior written authorization.
 *
 * 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 com.noveogroup.android.task;

public interface TaskHandler {

    /**
     * Represents a task's processing state. A given task may only be in one
     * state at a time.
     */
    public enum State {

        /**
         * The task has been created, but hasn't been started yet.
         */
        CREATED,

        /**
         * The task is running now.
         */
        STARTED,

        /**
         * The task has been canceled before method {@link Task#run(TaskEnvironment)}
         * was entered.
         */
        CANCELED,

        /**
         * The task was running but failed due to an exception was thrown
         * from {@link Task#run(TaskEnvironment)}.
         * <p/>
         * Task is considered as failed even if {@link InterruptedException}
         * was thrown.
         */
        FAILED,

        /**
         * The task has been started and finished successfully.
         */
        SUCCEED;

        /**
         * Returns {@code boolean} indicating whether the task's state is
         * alive {@code true} or {@code false}.
         *
         * @return a {@code boolean} indicating if state is {@link #CREATED}
         * or {@link #STARTED}.
         */
        public boolean isAlive() {
            return this == CREATED || this == STARTED;
        }

        /**
         * Returns {@code boolean} indicating whether the task's state is
         * destroyed {@code true} or {@code false}.
         *
         * @return a {@code boolean} indicating if state is {@link #CANCELED},
         * {@link #FAILED} or {@link #SUCCEED}.
         */
        public boolean isDestroyed() {
            return this == CANCELED || this == FAILED || this == SUCCEED;
        }

        /**
         * Returns {@code boolean} indicating whether the task's state is
         * finished {@code true} or {@code false}.
         *
         * @return a {@code boolean} indicating if state is {@link #FAILED}
         * or {@link #SUCCEED}.
         */
        public boolean isFinished() {
            return this == FAILED || this == SUCCEED;
        }

    }

    public TaskExecutor executor();

    public TaskSet owner();

    public Object lock();

    public Task task();

    public Pack args();

    public State getState();

    public Throwable getThrowable();

    public boolean isInterrupted();

    public void interrupt();

    public void join() throws InterruptedException;

    public boolean join(long timeout) throws InterruptedException;

}

@@ -1382,16 +1382,95 @@
 .task;%0A%0A
+import java.util.Collections;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 public i
@@ -2363,16 +2363,743 @@
 CCEED;%0A%0A
+        public static final Set%3CState%3E ALIVE_SET;%0A        public static final Set%3CState%3E DESTROYED_SET;%0A        public static final Set%3CState%3E FINISHED_SET;%0A%0A        static %7B%0A            Set%3CState%3E set;%0A%0A            set = new HashSet%3CState%3E();%0A            set.add(CREATED);%0A            set.add(STARTED);%0A            ALIVE_SET = Collections.unmodifiableSet(set);%0A%0A            set = new HashSet%3CState%3E();%0A            set.add(CANCELED);%0A            set.add(FAILED);%0A            set.add(SUCCEED);%0A            DESTROYED_SET = Collections.unmodifiableSet(set);%0A%0A            set = new HashSet%3CState%3E();%0A            set.add(FAILED);%0A            set.add(SUCCEED);%0A            FINISHED_SET = Collections.unmodifiableSet(set);%0A        %7D%0A%0A
         
@@ -3414,42 +3414,32 @@
 urn 
-this == CREATED %7C%7C this == STARTED
+ALIVE_SET.contains(this)
 ;%0A  
@@ -3797,61 +3797,36 @@
 urn 
-this == CANCELED %7C%7C this == FAILED %7C%7C this == SUCCEED
+DESTROYED_SET.contains(this)
 ;%0A  
@@ -4163,41 +4163,35 @@
 urn 
-this == FAILED %7C%7C this == SUCCEED
+FINISHED_SET.contains(this)
 ;%0A  

introduce state sets