-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogJson.java
77 lines (67 loc) · 2.71 KB
/
LogJson.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
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import com.google.gson.*;
import com.google.gson.reflect.*;
public class LogJson {
private static List<List<String>> entries;
private static Gson gson = new Gson();
private static Map<Class<?>, String> eventToKey = new HashMap<>();
private static Map<String, Class<?>> keyToEvent = new HashMap<>();
static {
LogJson.registerEvent(MoveEvent.class, "Move");
LogJson.registerEvent(DeboardEvent.class, "Deboard");
LogJson.registerEvent(BoardEvent.class, "Board");
}
private LogJson(List<List<String>> entries) { this.entries = entries; }
public LogJson(Log log) {
entries = new LinkedList<>();
for (Event e : log.events()) {
LinkedList<String> elts = new LinkedList<>(e.toStringList());
elts.addFirst(eventToKey.get(e.getClass()));
entries.add(elts);
}
}
public Log toLog() {
System.out.println(keyToEvent);
List<Event> events = new LinkedList<>();
for (List<String> e : entries) {
System.out.println("key: " + e.get(0) + " result: " + keyToEvent.get(e.get(0)));
Class<?> cls = keyToEvent.get(e.get(0));
if (cls == MoveEvent.class) {
Event evt = new MoveEvent(Train.make(e.get(1)), Station.make(e.get(2)), Station.make(e.get(3)));
events.add(evt);
}
else if (cls == BoardEvent.class) {
Event evt = new BoardEvent(Passenger.make(e.get(1)), Train.make(e.get(2)), Station.make(e.get(3)));
events.add(evt);
}
else if (cls == DeboardEvent.class) {
Event evt = new DeboardEvent(Passenger.make(e.get(1)), Train.make(e.get(2)), Station.make(e.get(3)));
events.add(evt);
}
else {
throw new RuntimeException("Don't know what to do with event kind " + cls);
}
}
return new Log(events);
}
public static void registerEvent(Class<?> c, String k) {
eventToKey.put(c, k); keyToEvent.put(k, c);
}
public String toJson() {
return gson.toJson(entries);
}
public static LogJson fromJson(String json) {
Type t_list_string = TypeToken.getParameterized(List.class, String.class).getType();
Type t_list_list_string = TypeToken.getParameterized(List.class, t_list_string).getType();
@SuppressWarnings("unchecked") LogJson lj = new LogJson((List<List<String>>) gson.fromJson(json, t_list_list_string));
return lj;
}
public static LogJson fromJson(Reader r) {
Type t_list_string = TypeToken.getParameterized(List.class, String.class).getType();
Type t_list_list_string = TypeToken.getParameterized(List.class, t_list_string).getType();
@SuppressWarnings("unchecked") LogJson lj = new LogJson((List<List<String>>) gson.fromJson(r, t_list_list_string));
return lj;
}
}