KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > delivery > AbstractStateMachine


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.protocol.delivery;
5
6 import com.tc.util.Assert;
7
8 /**
9  *
10  */

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     // Override me
37
}
38
39   protected void basicResume() {
40     // Override me
41
}
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