StopWatchController.


// StopWatchController.java

package StopWatch;

import FSM.*;

public class StopWatchController implements IFSMController
{

  public static final short S_STOP  = 0;
  public static final short S_START = 1;

  public static final short E_START = 0;
  public static final short E_STOP  = 1;
  public static final short E_DSPLY = 2;

  public StopWatchController(StopWatch stopWatch)
  {
    _fsm = new FSM(S_STOP, S_START, E_DSPLY, 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 StateTransition[] getTransitions()
  {
    StateTransition[]  stateTransitions = {
      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"),
    };

    return stateTransitions;
  }

  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;

}