-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFind.java
86 lines (73 loc) · 1.88 KB
/
Find.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class Find extends Query implements Comparator<JSONObject> {
private String findData;
private JSONArray data, res;// res=result
private JSONObject input;
private String key;
public Find(String fd, JSONObject object) {
findData = fd;
input = object;
key = ((ArrayList<String>) object.keySet()).get(0);
data = FileControl.readFromFile(super.dName, super.tName);
find();
getData();
// if statement to check the condition given as the
// argument in the constructor and call the further methods given
}
private void find() {
if (data.get(0).containsKey(key) != true)
return;
if (findData == "gt")
greater();
else if (findData == "lt")
less();
else if (findData == "eq")
equal();
// extend functionality if time permits
}
public JSONArray sortBy(String key, boolean asc) {
Collections.sort(res, new MapComparator(key));
return res;
}
private class MapComparator {
String key;
public MapComparator(String k) {
key = k;
}
public int compare(JSONObject a, JSONOBject b) {
// valA and valB could be any simple type, such as number, string,
// whatever
String valA = a.get(key);
String valB = b.get(key);
return valA.compareTo(valB);
}
}
public JSONArray getData() {
return res;
}
private void greater() {
for (JSONObject i : data) {
if (i.get(key).compareTo(input.get(key)) > 0) {
res.add(i);
}
}
}
private void less() {
for (JSONObject i : data) {
if (i.get(key).compareTo(input.get(key)) < 0) {
res.add(i);
}
}
}
private void equal() {
for (JSONObject i : data) {
if (i.get(key).compareTo(input.get(key)) == 0) {
res.add(i);
}
}
}
}