-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.java
44 lines (37 loc) · 920 Bytes
/
StateMachine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.ArrayList;
class StateMachine
{
ArrayList<Transition> transitions;
State current;
String name;
StateMachine(String name, State start, ArrayList<Transition> transitions)
{
this.name = name;
this.current = start;
this.transitions = transitions;
}
void apply(Condition condition)
{
current = getNextState(condition);
}
State getNextState(Condition condition)
{
for(Transition transition : transitions)
{
boolean currentStateMatches = transition.from.equals(current);
boolean conditionsMatch = false;
for(Condition t : transition.conditions)
{
if(t.equals(condition))
{
conditionsMatch = true;
}
}
if(currentStateMatches && conditionsMatch)
{
return transition.to;
}
}
return null;
}
}