Skip to content

Commit fdc390e

Browse files
committed
Pass a JSONObject instead of a Map in the constructor. Save 'json' to return as String.
1 parent 9c535ac commit fdc390e

File tree

1 file changed

+62
-64
lines changed

1 file changed

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

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
6-
import static java.util.Objects.isNull;
3+
import java.util.Objects;
4+
import org.json.JSONObject;
75

86
public class UserAgent {
7+
private final String userAgentString;
8+
private final String name;
9+
private final String type;
10+
private final String version;
11+
private final String versionMajor;
12+
public UserAgentDevice device;
13+
private UserAgentEngine engine;
14+
private UserAgentOperatingSystem operatingSystem;
15+
private final JSONObject json;
16+
17+
UserAgent(JSONObject json) {
18+
if (Objects.isNull(json)) {
19+
throw new IllegalArgumentException("'json' must not be null");
20+
}
921

10-
private String userAgentString;
11-
private String name;
12-
private String type;
13-
private String version;
14-
private String versionMajor;
15-
public UserAgentDevice device;
16-
private UserAgentEngine engine;
17-
private UserAgentOperatingSystem operatingSystem;
18-
19-
public UserAgent() {
22+
if (json.isEmpty()) {
23+
throw new IllegalArgumentException("'json' must not be empty");
2024
}
2125

22-
public UserAgent(Map<String, Object> json) {
23-
if (isNull(json)) {
24-
throw new IllegalArgumentException("'json' must not be null.");
25-
}
26-
27-
if (json.isEmpty()) {
28-
throw new IllegalArgumentException("'json' must not be empty.");
29-
}
30-
31-
this.userAgentString = (String) json.get("userAgentString");
32-
this.name = (String) json.get("name");
33-
this.type = (String) json.get("type");
34-
this.version = (String) json.get("version");
35-
this.versionMajor = (String) json.get("versionMajor");
36-
if (json.get("device") instanceof HashMap) {
37-
Map<String, Object> deviceJson = (HashMap) json.get("device");
38-
this.device = new UserAgentDevice(deviceJson);
39-
}
40-
41-
if (json.get("engine") instanceof HashMap) {
42-
Map<String, Object> engineJson = (HashMap) json.get("engine");
43-
this.engine = new UserAgentEngine(engineJson);
44-
}
45-
46-
if (json.get("operatingSystem") instanceof HashMap) {
47-
Map<String, Object> operatingSystemJson = (HashMap) json.get("operatingSystem");
48-
this.operatingSystem = new UserAgentOperatingSystem(operatingSystemJson);
49-
}
26+
this.userAgentString = json.getString("userAgentString");
27+
this.name = json.getString("name");
28+
this.type = json.getString("type");
29+
this.version = json.getString("version");
30+
this.versionMajor = json.getString("versionMajor");
5031

32+
if (json.has("device")) {
33+
this.device = new UserAgentDevice(json.getJSONObject("device"));
5134
}
5235

53-
public String getUserAgentString() {
54-
return userAgentString;
36+
if (json.has("engine")) {
37+
this.engine = new UserAgentEngine(json.getJSONObject("engine"));
5538
}
5639

57-
public String getName() {
58-
return name;
40+
if (json.has("operatingSystem")) {
41+
this.operatingSystem = new UserAgentOperatingSystem(json.getJSONObject("operatingSystem"));
5942
}
6043

61-
public String getType() {
62-
return type;
63-
}
44+
this.json = json;
45+
}
6446

65-
public String getVersion() {
66-
return version;
67-
}
47+
public String getUserAgentString() {
48+
return userAgentString;
49+
}
6850

69-
public String getVersionMajor() {
70-
return versionMajor;
71-
}
51+
public String getName() {
52+
return name;
53+
}
7254

73-
public UserAgentDevice getDevice() {
74-
return device;
75-
}
55+
public String getType() {
56+
return type;
57+
}
7658

77-
public UserAgentEngine getEngine() {
78-
return engine;
79-
}
59+
public String getVersion() {
60+
return version;
61+
}
8062

81-
public UserAgentOperatingSystem getOperatingSystem() {
82-
return operatingSystem;
83-
}
63+
public String getVersionMajor() {
64+
return versionMajor;
65+
}
66+
67+
public UserAgentDevice getDevice() {
68+
return device;
69+
}
70+
71+
public UserAgentEngine getEngine() {
72+
return engine;
73+
}
74+
75+
public UserAgentOperatingSystem getOperatingSystem() {
76+
return operatingSystem;
77+
}
8478

79+
@Override
80+
public String toString() {
81+
return json.toString(2);
82+
}
8583
}

0 commit comments

Comments
 (0)