1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.statemachine;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.mina.statemachine.event.Event;
27 import org.apache.mina.statemachine.transition.SelfTransition;
28 import org.apache.mina.statemachine.transition.Transition;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class State {
48 private final String id;
49
50 private final State parent;
51
52 private List<TransitionHolder> transitionHolders = new ArrayList<TransitionHolder>();
53
54 private List<Transition> transitions = Collections.emptyList();
55
56 private List<SelfTransition> onEntries = new ArrayList<SelfTransition>();
57
58 private List<SelfTransition> onExits = new ArrayList<SelfTransition>();
59
60
61
62
63
64
65 public State(String id) {
66 this(id, null);
67 }
68
69
70
71
72
73
74
75 public State(String id, State parent) {
76 this.id = id;
77 this.parent = parent;
78 }
79
80
81
82
83 public String getId() {
84 return id;
85 }
86
87
88
89
90
91 public State getParent() {
92 return parent;
93 }
94
95
96
97
98
99 public List<Transition> getTransitions() {
100 return Collections.unmodifiableList(transitions);
101 }
102
103
104
105
106 public List<SelfTransition> getOnEntrySelfTransitions() {
107 return Collections.unmodifiableList(onEntries);
108 }
109
110
111
112
113 public List<SelfTransition> getOnExitSelfTransitions() {
114 return Collections.unmodifiableList(onExits);
115 }
116
117
118
119
120
121
122
123 State addOnEntrySelfTransaction(SelfTransition onEntrySelfTransaction) {
124 if (onEntrySelfTransaction == null) {
125 throw new IllegalArgumentException("transition");
126 }
127 onEntries.add(onEntrySelfTransaction);
128 return this;
129 }
130
131
132
133
134
135
136
137 State addOnExitSelfTransaction(SelfTransition onExitSelfTransaction) {
138 if (onExitSelfTransaction == null) {
139 throw new IllegalArgumentException("transition");
140 }
141 onExits.add(onExitSelfTransaction);
142 return this;
143 }
144
145 private void updateTransitions() {
146 transitions = new ArrayList<Transition>(transitionHolders.size());
147 for (TransitionHolder holder : transitionHolders) {
148 transitions.add(holder.transition);
149 }
150 }
151
152
153
154
155
156
157
158
159 public State addTransition(Transition transition) {
160 return addTransition(transition, 0);
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174 public State addTransition(Transition transition, int weight) {
175 if (transition == null) {
176 throw new IllegalArgumentException("transition");
177 }
178
179 transitionHolders.add(new TransitionHolder(transition, weight));
180 Collections.sort(transitionHolders);
181 updateTransitions();
182 return this;
183 }
184
185
186
187
188 @Override
189 public boolean equals(Object o) {
190 if (o == this) {
191 return true;
192 }
193
194 if (!(o instanceof State)) {
195 return false;
196 }
197
198 return id.equals(((State) o).id);
199 }
200
201
202
203
204 @Override
205 public int hashCode() {
206 int h = 37;
207
208 return h * 17 + id.hashCode();
209 }
210
211
212
213
214 @Override
215 public String toString() {
216 StringBuilder sb = new StringBuilder();
217
218 sb.append("State[");
219 sb.append("id=").append(id);
220 sb.append("]");
221
222 return sb.toString();
223 }
224
225 private static class TransitionHolder implements Comparable<TransitionHolder> {
226 private Transition transition;
227
228 private int weight;
229
230 TransitionHolder(Transition transition, int weight) {
231 this.transition = transition;
232 this.weight = weight;
233 }
234
235 public int compareTo(TransitionHolder o) {
236 return (weight > o.weight) ? 1 : (weight < o.weight ? -1 : 0);
237 }
238 }
239 }