|
1 | 1 | package io.ipgeolocation.api; |
2 | 2 |
|
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; |
7 | 5 |
|
8 | 6 | 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 | + } |
9 | 21 |
|
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"); |
20 | 24 | } |
21 | 25 |
|
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"); |
50 | 31 |
|
| 32 | + if (json.has("device")) { |
| 33 | + this.device = new UserAgentDevice(json.getJSONObject("device")); |
51 | 34 | } |
52 | 35 |
|
53 | | - public String getUserAgentString() { |
54 | | - return userAgentString; |
| 36 | + if (json.has("engine")) { |
| 37 | + this.engine = new UserAgentEngine(json.getJSONObject("engine")); |
55 | 38 | } |
56 | 39 |
|
57 | | - public String getName() { |
58 | | - return name; |
| 40 | + if (json.has("operatingSystem")) { |
| 41 | + this.operatingSystem = new UserAgentOperatingSystem(json.getJSONObject("operatingSystem")); |
59 | 42 | } |
60 | 43 |
|
61 | | - public String getType() { |
62 | | - return type; |
63 | | - } |
| 44 | + this.json = json; |
| 45 | + } |
64 | 46 |
|
65 | | - public String getVersion() { |
66 | | - return version; |
67 | | - } |
| 47 | + public String getUserAgentString() { |
| 48 | + return userAgentString; |
| 49 | + } |
68 | 50 |
|
69 | | - public String getVersionMajor() { |
70 | | - return versionMajor; |
71 | | - } |
| 51 | + public String getName() { |
| 52 | + return name; |
| 53 | + } |
72 | 54 |
|
73 | | - public UserAgentDevice getDevice() { |
74 | | - return device; |
75 | | - } |
| 55 | + public String getType() { |
| 56 | + return type; |
| 57 | + } |
76 | 58 |
|
77 | | - public UserAgentEngine getEngine() { |
78 | | - return engine; |
79 | | - } |
| 59 | + public String getVersion() { |
| 60 | + return version; |
| 61 | + } |
80 | 62 |
|
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 | + } |
84 | 78 |
|
| 79 | + @Override |
| 80 | + public String toString() { |
| 81 | + return json.toString(2); |
| 82 | + } |
85 | 83 | } |
0 commit comments