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