-
Notifications
You must be signed in to change notification settings - Fork 2
/
Handmadestate.java
96 lines (84 loc) · 2.72 KB
/
Handmadestate.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// State pattern: handmade implementation using example of traditional phone
package behavioral.state.handmade;
import org.javatuples.Pair;
import java.io.*;
import java.text.*;
import java.util.*;
enum State
{
OFF_HOOK, // starting state
ON_HOOK, // terminal state
CONNECTING,
CONNECTED,
ON_HOLD
}
enum Trigger
{
CALL_DIALED,
HUNG_UP,
CALL_CONNECTED,
PLACED_ON_HOLD,
TAKEN_OFF_HOLD,
LEFT_MESSAGE,
STOP_USING_PHONE
}
class HandmadeStateDemo
{
// State machine here
private static Map<State, List<Pair<Trigger, State>>>
rules = new HashMap<>();
static
{
rules.put(State.OFF_HOOK, List.of (
new Pair<>(Trigger.CALL_DIALED, State.CONNECTED),
new Pair<>(Trigger.STOP_USING_PHONE, State.ON_HOOK)
));
rules.put(State.CONNECTING, List.of (
new Pair<>(Trigger.HUNG_UP, State.OFF_HOOK),
new Pair<>(Trigger.CALL_CONNECTED, State.CONNECTED)
));
rules.put(State.CONNECTED, List.of (
new Pair<>(Trigger.LEFT_MESSAGE, State.OFF_HOOK),
new Pair<>(Trigger.HUNG_UP, State.OFF_HOOK),
new Pair<>(Trigger.PLACED_ON_HOLD, State.ON_HOLD)
));
rules.put(State.ON_HOLD, List.of(
new Pair<>(Trigger.TAKEN_OFF_HOLD, State.CONNECTED),
new Pair<>(Trigger.HUNG_UP, State.OFF_HOOK)
));
}
private static State currentState = State.OFF_HOOK; // initial state
private static State exitState = State.ON_HOOK; // final state
public static void main(String[] args)
{
BufferedReader console = new BufferedReader(
new InputStreamReader(System.in)
);
while(true)
{
System.out.println("The phone is currently " + currentState);
System.out.println("Select a trigger:");
for (var i = 0; i < rules.get(currentState).size(); ++i)
{
Trigger trigger = rules.get(currentState).get(i).getValue0();
System.out.println("" + i + ". " + trigger);
}
boolean parseOk;
int choice = 0;
do{
try {
System.out.println("Please enter your choice:");
choice = Integer.parseInt(console.readLine());
parseOk = choice >=0 &&
choice < rules.get(currentState).size();
} catch (Exception e)
{
parseOk = false;
}
} while (!parseOk);
currentState = rules.get(currentState).get(choice).getValue1();
if (currentState == exitState) break;
}
System.out.println("And we are done!");
}
}