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