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 .math .BigDecimal ;
4
+ import java .util .Objects ;
5
+ import org .json .JSONObject ;
7
6
8
7
public class Geolocation {
9
8
private final String domain ;
@@ -16,12 +15,13 @@ public class Geolocation {
16
15
private final String countryName ;
17
16
private final String countryCapital ;
18
17
private final String stateProvince ;
18
+ private final String stateCode ;
19
19
private final String district ;
20
20
private final String city ;
21
21
private final String zipCode ;
22
- private final String latitude ;
23
- private final String longitude ;
24
- private final Boolean isEU ;
22
+ private final BigDecimal latitude ;
23
+ private final BigDecimal longitude ;
24
+ private final boolean eu ;
25
25
private final String callingCode ;
26
26
private final String countryTLD ;
27
27
private final String languages ;
@@ -30,72 +30,73 @@ public class Geolocation {
30
30
private final String connectionType ;
31
31
private final String organization ;
32
32
private final String asn ;
33
- private final String geonameID ;
33
+ private final long geoNameId ;
34
34
private GeolocationCurrency currency ;
35
35
private GeolocationTimezone timezone ;
36
36
private GeolocationSecurity geolocationSecurity ;
37
37
private UserAgent userAgent ;
38
+ private final JSONObject json ;
38
39
39
- Geolocation (Map < String , Object > json ) {
40
- if (isNull (json )) {
41
- throw new IllegalArgumentException ("'json' must not be null. " );
40
+ Geolocation (JSONObject json ) {
41
+ if (Objects . isNull (json )) {
42
+ throw new IllegalArgumentException ("'json' must not be null" );
42
43
}
43
44
44
45
if (json .isEmpty ()) {
45
- throw new IllegalArgumentException ("'json' must not be empty. " );
46
+ throw new IllegalArgumentException ("'json' must not be empty" );
46
47
}
47
48
48
- this .domain = (String ) json .get ("domain" );
49
- this .ip = (String ) json .get ("ip" );
50
- this .hostname = (String ) json .get ("hostname" );
51
- this .continentCode = (String ) json .get ("continent_code" );
52
- this .continentName = (String ) json .get ("continent_name" );
53
- this .countryCode2 = (String ) json .get ("country_code2" );
54
- this .countryCode3 = (String ) json .get ("country_code3" );
55
- this .countryName = (String ) json .get ("country_name" );
56
- this .countryCapital = (String ) json .get ("country_capital" );
57
- this .stateProvince = (String ) json .get ("state_prov" );
58
- this .district = (String ) json .get ("district" );
59
- this .city = (String ) json .get ("city" );
60
- this .zipCode = (String ) json .get ("zipcode" );
61
- this .latitude = (String ) json .get ("latitude" );
62
- this .longitude = (String ) json .get ("longitude" );
63
- this .isEU = (Boolean ) json .get ("is_eu" );
64
- this .callingCode = (String ) json .get ("calling_code" );
65
- this .countryTLD = (String ) json .get ("country_tld" );
66
- this .languages = (String ) json .get ("languages" );
67
- this .countryFlag = (String ) json .get ("country_flag" );
68
- this .isp = (String ) json .get ("isp" );
69
- this .connectionType = (String ) json .get ("connection_type" );
70
- this .organization = (String ) json .get ("organization" );
71
- this .asn = (String ) json .get ("asn" );
72
- this .geonameID = (String ) json .get ("geoname_id" );
73
- if (json .get ("currency" ) instanceof HashMap ) {
74
- Map <String , Object > currencyJson = (HashMap ) json .get ("currency" );
75
- this .currency = new GeolocationCurrency (currencyJson );
49
+ this .domain = json .optString ("domain" );
50
+ this .ip = json .getString ("ip" );
51
+ this .hostname = json .optString ("hostname" );
52
+ this .continentCode = json .optString ("continent_code" );
53
+ this .continentName = json .optString ("continent_name" );
54
+ this .countryCode2 = json .optString ("country_code2" );
55
+ this .countryCode3 = json .optString ("country_code3" );
56
+ this .countryName = json .optString ("country_name" );
57
+ this .countryCapital = json .optString ("country_capital" );
58
+ this .stateProvince = json .optString ("state_prov" );
59
+ this .stateCode = json .optString ("state_code" );
60
+ this .district = json .optString ("district" );
61
+ this .city = json .optString ("city" );
62
+ this .zipCode = json .optString ("zipcode" );
63
+ this .latitude = new BigDecimal (json .optString ("latitude" , "0.0" ));
64
+ this .longitude = new BigDecimal (json .optString ("longitude" , "0.0" ));
65
+ this .eu = json .optBoolean ("is_eu" );
66
+ this .callingCode = json .optString ("calling_code" );
67
+ this .countryTLD = json .optString ("country_tld" );
68
+ this .languages = json .optString ("languages" );
69
+ this .countryFlag = json .optString ("country_flag" );
70
+ this .isp = json .optString ("isp" );
71
+ this .connectionType = json .optString ("connection_type" );
72
+ this .organization = json .optString ("organization" );
73
+ this .asn = json .optString ("asn" );
74
+ this .geoNameId = Long .parseLong (json .optString ("geoname_id" , "0" ));
75
+
76
+ if (json .has ("currency" )) {
77
+ this .currency = new GeolocationCurrency (json .getJSONObject ("currency" ));
76
78
}
77
79
78
- if (json .get ("time_zone" ) instanceof HashMap ) {
79
- Map <String , Object > timezoneJson = (HashMap ) json .get ("time_zone" );
80
- this .timezone = new GeolocationTimezone (timezoneJson );
80
+ if (json .has ("time_zone" )) {
81
+ this .timezone = new GeolocationTimezone (json .getJSONObject ("time_zone" ));
81
82
}
82
83
83
- if (json .get ("security" ) instanceof HashMap ) {
84
- Map <String , Object > securityJson = (HashMap ) json .get ("security" );
85
- this .geolocationSecurity = new GeolocationSecurity (securityJson );
84
+ if (json .has ("security" )) {
85
+ this .geolocationSecurity = new GeolocationSecurity (json .getJSONObject ("security" ));
86
86
}
87
87
88
- if (json .get ("user_agent" ) instanceof HashMap ) {
89
- Map <String , Object > userAgentJson = (HashMap ) json .get ("user_agent" );
90
- this .userAgent = new UserAgent (userAgentJson );
88
+ if (json .has ("user_agent" )) {
89
+ this .userAgent = new UserAgent (json .getJSONObject ("user_agent" ));
91
90
}
91
+
92
+ this .json = json ;
92
93
}
93
94
94
95
public String getDomain () {
95
96
return domain ;
96
97
}
97
98
98
- public String getIPAddress () {
99
+ public String getIP () {
99
100
return ip ;
100
101
}
101
102
@@ -131,6 +132,10 @@ public String getStateProvince() {
131
132
return stateProvince ;
132
133
}
133
134
135
+ public String getStateCode () {
136
+ return stateCode ;
137
+ }
138
+
134
139
public String getDistrict () {
135
140
return district ;
136
141
}
@@ -143,16 +148,16 @@ public String getZipCode() {
143
148
return zipCode ;
144
149
}
145
150
146
- public String getLatitude () {
151
+ public BigDecimal getLatitude () {
147
152
return latitude ;
148
153
}
149
154
150
- public String getLongitude () {
155
+ public BigDecimal getLongitude () {
151
156
return longitude ;
152
157
}
153
158
154
- public Boolean isEU () {
155
- return isEU ;
159
+ public boolean isEU () {
160
+ return eu ;
156
161
}
157
162
158
163
public String getCallingCode () {
@@ -187,8 +192,8 @@ public String getAsn() {
187
192
return asn ;
188
193
}
189
194
190
- public String getGeonameID () {
191
- return geonameID ;
195
+ public long getGeoNameId () {
196
+ return geoNameId ;
192
197
}
193
198
194
199
public GeolocationCurrency getCurrency () {
@@ -206,4 +211,9 @@ public GeolocationSecurity getGeolocationSecurity() {
206
211
public UserAgent getUserAgent () {
207
212
return userAgent ;
208
213
}
214
+
215
+ @ Override
216
+ public String toString () {
217
+ return json .toString (2 );
218
+ }
209
219
}
0 commit comments