1 4 package com.tc.net.protocol.delivery; 5 6 import com.tc.util.Assert; 7 8 11 public abstract class AbstractStateMachine { 12 private State current; 13 private boolean started = false; 14 private boolean paused = true; 15 16 public abstract void execute(OOOProtocolMessage msg); 17 18 public final synchronized boolean isStarted() { 19 return started; 20 } 21 22 public final synchronized void start() { 23 Assert.eval(!started); 24 started = true; 25 paused = true; 26 switchToState(initialState()); 27 } 28 29 public final synchronized void pause() { 30 Assert.eval("started: " + started + ", paused: " + paused, started && ! paused); 31 basicPause(); 32 this.paused = true; 33 } 34 35 protected void basicPause() { 36 } 38 39 protected void basicResume() { 40 } 42 43 public final synchronized void resume() { 44 Assert.eval("started: " + started + ", paused: " + paused, started && paused); 45 this.paused = false; 46 basicResume(); 47 } 48 49 public final synchronized boolean isPaused() { 50 return this.paused; 51 } 52 53 protected final synchronized void switchToState(State state) { 54 Assert.eval(state != null && isStarted()); 55 this.current = state; 56 state.enter(); 57 } 58 59 public synchronized final State getCurrentState() { 60 return current; 61 } 62 63 protected abstract State initialState(); 64 } | Popular Tags |