/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.system;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public final class State {

    public final static State ORIGINAL = new State(0, "Original");

    /**
     * INITIALIZING, system is in initializing, doCreate()
     */
    public final static State INITIALIZING = new State(1, "Initializing");

    /**
     * INITIALIZED, system is initialized
     */
    public final static State INITIALIZED = new State(2, "Initialized");

    /**
     * STOPPING, system is in starting
     */
    public final static State STARTING = new State(3, "Starting");

    /**
     * STARTED, system has started
     */
    public final static State STARTED = new State(4, "Started");

    /**
     * STOPPING, system is in stopping
     */
    public final static State STOPPING = new State(5, "Stopping");

    /**
     * STOPPED, system stopped by user
     */
    public final static State STOPPED = new State(6, "Stopped");

    /**
     * DESTROYING, system is in destroying
     */
    public final static State DESTROYING = new State(7, "Destroying");

    /**
     * DESTROYED, system stopped and it's resource destoryed, example: it's connection closed
     */
    public final static State DESTROYED = new State(8, "Destroyed");

    /**
     * INTERRUPPTED, system interrupted because of some error or exception
     */
    public final static State INTERRUPTED = new State(9, "Interrupted");


    public int id = 0;
    public String name = "";

    public State(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int hashCode() {
        return (id + "").hashCode() + name.hashCode();
    }

    public String toString() {
        return id + " [" + name + "]";
    }

    public boolean equals(Object obj) {
        if(obj instanceof State) {
            State state = (State) obj;
            return state.id == id && state.name.equals(name);
        }
        return false;

    }

    public static boolean canInit(State state) {
        if(state == State.ORIGINAL || state == State.DESTROYED) {
            return true;
        }
        return false;
    }

    public static boolean canStart(State state) {
        if(state == State.STOPPED || state == State.INITIALIZED) {
            return true;
        }
        return false;
    }

    public static boolean canStop(State state) {
        if(state == State.STARTED) {
            return true;
        }
        return false;
    }

    public static boolean canDestroy(State state) {
        if(state == State.INITIALIZED || state == State.STOPPED || state == State.INTERRUPTED) {
            return true;
        }
        return false;
    }

}
