-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNgordnetQueryHandler.java
60 lines (48 loc) · 1.7 KB
/
NgordnetQueryHandler.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
package ngordnet.browser;
import com.google.gson.Gson;
import spark.QueryParamsMap;
import spark.Request;
import spark.Response;
import spark.Route;
import java.util.Arrays;
import java.util.List;
public abstract class NgordnetQueryHandler implements Route {
public abstract String handle(ngordnet.browser.NgordnetQuery q);
private static final Gson gson = new Gson();
private static List<String> commaSeparatedStringToList(String s) {
String[] requestedWords = s.split(",");
for (int i = 0; i < requestedWords.length; i += 1) {
requestedWords[i] = requestedWords[i].trim();
}
return Arrays.asList(requestedWords);
}
private static ngordnet.browser.NgordnetQuery readQueryMap(QueryParamsMap qm) {
List<String> words = commaSeparatedStringToList(qm.get("words").value());
int startYear;
int endYear;
int k;
try {
startYear = Integer.parseInt(qm.get("startYear").value());
} catch (RuntimeException e) {
startYear = 1900;
}
try {
endYear = Integer.parseInt(qm.get("endYear").value());
} catch (RuntimeException e) {
endYear = 2020;
}
try {
k = Integer.parseInt(qm.get("k").value());
} catch (RuntimeException e) {
k = 0;
}
return new ngordnet.browser.NgordnetQuery(words, startYear, endYear, k);
}
@Override
public String handle(Request request, Response response) throws Exception {
QueryParamsMap qm = request.queryMap();
NgordnetQuery nq = readQueryMap(qm);
String queryResult = handle(nq);
return gson.toJson(queryResult);
}
}