/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.huihoo.jfox.service;

/**
 * 
 * @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, service is in initializing, doCreate()
   */
  public final static State INITIALIZING = new State(1,"Initializing");

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

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

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

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

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

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

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

  /**
   * INTERRUPPTED, service 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;
  }

}
