Skip to content

Commit 7411255

Browse files
committed
Pass a JSONObject instead of a Map in the constructor. Fields renamed. Use primitive types instead of non-primtives. Save 'json' to return as String.
1 parent 76e2f18 commit 7411255

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed
Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,79 @@
11
package io.ipgeolocation.api;
22

3-
import java.util.Map;
3+
import java.util.Objects;
4+
import org.json.JSONObject;
45

56
public class GeolocationSecurity {
6-
private final Integer threatScore;
7-
private final Boolean isTor;
8-
private final Boolean isProxy;
9-
private final String proxyType;
10-
private final Boolean isAnonymous;
11-
private final Boolean isKnownAttacker;
12-
private final Boolean isBot;
13-
private final Boolean isSpam;
14-
private final Boolean isCloudProvider;
7+
private final int threatScore;
8+
private final boolean tor;
9+
private final boolean proxy;
10+
private final String proxyType;
11+
private final boolean anonymous;
12+
private final boolean knownAttacker;
13+
private final boolean bot;
14+
private final boolean spam;
15+
private final boolean cloudProvider;
16+
private final JSONObject json;
1517

16-
GeolocationSecurity(Map<String, Object> json) {
17-
this.threatScore = (Integer) json.get("threat_score");
18-
this.isTor = (Boolean) json.get("is_tor");
19-
this.isProxy = (Boolean) json.get("is_proxy");
20-
this.proxyType = (String) json.get("proxy_type");
21-
this.isAnonymous = (Boolean) json.get("is_anonymous");
22-
this.isKnownAttacker = (Boolean) json.get("is_known_attacker");
23-
this.isBot = (Boolean) json.get("is_bot");
24-
this.isSpam = (Boolean) json.get("is_spam");
25-
this.isCloudProvider = (Boolean) json.get("is_cloud_provider");
18+
GeolocationSecurity(JSONObject json) {
19+
if (Objects.isNull(json)) {
20+
throw new IllegalArgumentException("'json' must not be null");
2621
}
2722

28-
public Integer getThreatScore() {
29-
return threatScore;
23+
if (json.isEmpty()) {
24+
throw new IllegalArgumentException("'json' must not be empty");
3025
}
3126

32-
public Boolean isTor() {
33-
return isTor;
34-
}
27+
this.threatScore = json.getInt("threat_score");
28+
this.tor = json.getBoolean("is_tor");
29+
this.proxy = json.getBoolean("is_proxy");
30+
this.proxyType = json.getString("proxy_type");
31+
this.anonymous = json.getBoolean("is_anonymous");
32+
this.knownAttacker = json.getBoolean("is_known_attacker");
33+
this.bot = json.getBoolean("is_bot");
34+
this.spam = json.getBoolean("is_spam");
35+
this.cloudProvider = json.getBoolean("is_cloud_provider");
36+
this.json = json;
37+
}
3538

36-
public Boolean isProxy() {
37-
return isProxy;
38-
}
39+
public int getThreatScore() {
40+
return threatScore;
41+
}
3942

40-
public String getProxyType() {
41-
return proxyType;
42-
}
43+
public boolean isTor() {
44+
return tor;
45+
}
4346

44-
public Boolean isAnonymous() {
45-
return isAnonymous;
46-
}
47+
public boolean isProxy() {
48+
return proxy;
49+
}
4750

48-
public Boolean isKnownAttacker() {
49-
return isKnownAttacker;
50-
}
51+
public String getProxyType() {
52+
return proxyType;
53+
}
5154

52-
public Boolean isBot() {
53-
return isBot;
54-
}
55+
public boolean isAnonymous() {
56+
return anonymous;
57+
}
5558

56-
public Boolean isSpam() {
57-
return isSpam;
58-
}
59+
public boolean isKnownAttacker() {
60+
return knownAttacker;
61+
}
5962

60-
public Boolean isCloudProvider() {
61-
return isCloudProvider;
62-
}
63+
public boolean isBot() {
64+
return bot;
65+
}
66+
67+
public boolean isSpam() {
68+
return spam;
69+
}
70+
71+
public boolean isCloudProvider() {
72+
return cloudProvider;
73+
}
6374

75+
@Override
76+
public String toString() {
77+
return json.toString(2);
78+
}
6479
}

0 commit comments

Comments
 (0)