StopWatchController2.


// StopWatchController2.java

package StopWatch;

 import FSM.*;

 public class StopWatchController2 implements IFSMController
{
  public static final short S_STOP  = 0;
  public static final short S_START = 1;
  public static final short S_PAUSE = 2;

  public static final short E_START = 0;
  public static final short E_STOP  = 1;
  public static final short E_DSPLY = 2;
  public static final short E_PAUSE = 3;
  public static final short E_RESUM = 4;

  public StopWatchController2(StopWatch stopWatch)
  {
    _fsm = new FSM(S_STOP, S_PAUSE, E_RESUM, this, stopWatch);
  }

  public void start()
  {
    _fsm.dispatch(E_START);
  }

  public void stop()
  {
    _fsm.dispatch(E_STOP);
  }

  public long display()
  {
    Object o = _fsm.dispatch(E_DSPLY);
    return ((Long)o).longValue();
  }

  public synchronized void pause()
  {
    _fsm.dispatch(E_PAUSE);
  }

  public synchronized void resume()
  {
    _fsm.dispatch(E_RESUM);
  }

  public StateTransition[] getTransitions()
  {
    StateTransition[] s = {
      new StateTransition(S_STOP,  E_START, S_START, "start"),
      new StateTransition(S_STOP,  E_DSPLY, S_STOP,  "display1"),
      new StateTransition(S_START, E_STOP,  S_STOP,  "stop"),
      new StateTransition(S_START, E_DSPLY, S_START, "display2"),
      new StateTransition(S_START, E_PAUSE, S_PAUSE, "pause"),
      new StateTransition(S_PAUSE, E_STOP,  S_STOP,  "nop"),
      new StateTransition(S_PAUSE, E_DSPLY, S_PAUSE, "display1"),
      new StateTransition(S_PAUSE, E_RESUM, S_START, "resume")
    };

    return s;
  }

  public void unknownEventHandler(StateTransition s)
  {
    System.out.println("FSM unknown event : " + s);
  }

  public void invalidTransitionHandler(StateTransition s)
  {
    System.out.println("FSM invalid transition : " + s);
  }

  private FSM _fsm;

 }