01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.net.protocol.delivery;
06:
07: import com.tc.util.Assert;
08:
09: /**
10: *
11: */
12: public abstract class AbstractStateMachine {
13: private State current;
14: private boolean started = false;
15: private boolean paused = true;
16:
17: public abstract void execute(OOOProtocolMessage msg);
18:
19: public final synchronized boolean isStarted() {
20: return started;
21: }
22:
23: public final synchronized void start() {
24: Assert.eval(!started);
25: started = true;
26: paused = true;
27: switchToState(initialState());
28: }
29:
30: public final synchronized void pause() {
31: Assert.eval("started: " + started + ", paused: " + paused,
32: started && !paused);
33: basicPause();
34: this .paused = true;
35: }
36:
37: protected void basicPause() {
38: // Override me
39: }
40:
41: protected void basicResume() {
42: // Override me
43: }
44:
45: public final synchronized void resume() {
46: Assert.eval("started: " + started + ", paused: " + paused,
47: started && paused);
48: this .paused = false;
49: basicResume();
50: }
51:
52: public final synchronized boolean isPaused() {
53: return this .paused;
54: }
55:
56: protected synchronized void switchToState(State state) {
57: Assert.eval(state != null && isStarted());
58: this .current = state;
59: state.enter();
60: }
61:
62: public synchronized final State getCurrentState() {
63: return current;
64: }
65:
66: protected abstract State initialState();
67:
68: public abstract void reset();
69: }
|