-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.java
146 lines (121 loc) · 4.92 KB
/
Message.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package chat.haver.server;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import java.util.List;
/**
* All Messages have a constructor with the signature:
* public CLASS(JSONObject jsonObject).
* Fields must be primitives ONLY if they are to be stringifiable and override toString.
*/
public abstract class Message {
public interface JSONKey {}
public enum Key implements JSONKey {
TYPE("type");
public final String key;
Key(final String key) {
this.key = key;
}
@Override
public String toString() {return key;}
}
public enum Type {
LOCATION(0),
POST(1),
ROOM_INFO(2),
CLIENT_INFO(3);
public final int type;
Type(final int type) {
this.type = type;
}
@Override
public String toString() {return Integer.toString(type);}
}
public enum Request {
LOCATION("{\"" + Key.TYPE + "\": " + Type.LOCATION + '}'),
ROOM_INFO("{\"" + Key.TYPE + "\": " + Type.ROOM_INFO + '}');
public final String request;
Request(final String request) {
this.request = request;
}
@Override
public String toString() {return request;}
}
public static Type typeFromJson(final JSONObject message) {
if (message.containsKey(Key.TYPE.key) && message.get(Key.TYPE.key) instanceof Number) {
int typeNumber = intFromJson(message, Key.TYPE);
for (Type type : Type.values()) {
if (typeNumber == type.type) return type;
}
}
return null;
}
public static JSONObject jsonFromString(final String jsonString) {
try {
return (JSONObject) Router.PARSER.parse(jsonString);
} catch(ParseException e) {
Logger.warning("Parse Exception: " + e.getMessage());
return null;
}
}
/**
*
* NB: Does not enforce unique names. This should be enforced when storing the List (see: {@link Post#setTo(List) Post:setTo(List}).
* @param names
* @return True if the List is empty or all elements are valid names. False if List is longer than room max size.
*/
public static boolean validListOfNames(final List names) {
// Correctly allows for an empty array
if(names.size() == 0) {return true;} // Empty Lists are safe
if(names.size() > Client.NAMES.length) {return false;} // Cannot be larger than room max size
// ONLY check contents below here
for(Object name : names.stream().distinct().toArray()) { // To remove DOS attack chance.
if (!(name instanceof String)) {return false;}
if (!Client.validName((String) (name))) {return false;}
}
return true;
}
/**
* Helper method that encapsulates the casting of Numbers from JSONObjects.
*
* @haver.precondition jsonObject must contain key of type Number.
* @param jsonObject The entire JSONObject containing the key/value pair to extract and cast.
* @param key The key that maps to a Number in the jsonObject.
* @return The specified value as a double.
*/
protected static double doubleFromJson(final JSONObject jsonObject, final JSONKey key) {
return ((Number) jsonObject.get(key.toString())).doubleValue();
}
/**
* Helper method that encapsulates the casting of Numbers from JSONObjects.
*
* @haver.precondition jsonObject must contain key of type Number.
* @param jsonObject The entire JSONObject containing the key/value pair to extract and cast.
* @param key The key that maps to a Number in the jsonObject.
* @return The specified value as a int.
*/
protected static int intFromJson(final JSONObject jsonObject, final JSONKey key) {
return ((Number) jsonObject.get(key.toString())).intValue();
}
/**
* Helper method that encapsulates the casting of Strings from JSONObjects.
*
* @haver.precondition jsonObject must contain key of type String.
* @param jsonObject The entire JSONObject containing the key/value pair to extract and cast.
* @param key The key that maps to a String in the jsonObject.
* @return The specified value as a String.
*/
protected static String stringFromJson(final JSONObject jsonObject, final JSONKey key) {
return (String) jsonObject.get(key.toString());
}
/**
* Helper method that encapsulates the casting of Lists from JSONObjects.
*
* @haver.precondition jsonObject must contain key of type List.
* @param jsonObject The entire JSONObject containing the key/value pair to extract and cast.
* @param key The key that maps to a List in the jsonObject.
* @return The specified value as a List.
*/
protected static List listFromJson(final JSONObject jsonObject, final JSONKey key) {
return (List) jsonObject.get(key.toString());
}
}