11package 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 .math .BigDecimal ;
4+ import java .util .Objects ;
5+ import org .json .JSONObject ;
76
87public class Geolocation {
98 private final String domain ;
@@ -16,12 +15,13 @@ public class Geolocation {
1615 private final String countryName ;
1716 private final String countryCapital ;
1817 private final String stateProvince ;
18+ private final String stateCode ;
1919 private final String district ;
2020 private final String city ;
2121 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 ;
2525 private final String callingCode ;
2626 private final String countryTLD ;
2727 private final String languages ;
@@ -30,72 +30,73 @@ public class Geolocation {
3030 private final String connectionType ;
3131 private final String organization ;
3232 private final String asn ;
33- private final String geonameID ;
33+ private final long geoNameId ;
3434 private GeolocationCurrency currency ;
3535 private GeolocationTimezone timezone ;
3636 private GeolocationSecurity geolocationSecurity ;
3737 private UserAgent userAgent ;
38+ private final JSONObject json ;
3839
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" );
4243 }
4344
4445 if (json .isEmpty ()) {
45- throw new IllegalArgumentException ("'json' must not be empty. " );
46+ throw new IllegalArgumentException ("'json' must not be empty" );
4647 }
4748
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" ));
7678 }
7779
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" ));
8182 }
8283
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" ));
8686 }
8787
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" ));
9190 }
91+
92+ this .json = json ;
9293 }
9394
9495 public String getDomain () {
9596 return domain ;
9697 }
9798
98- public String getIPAddress () {
99+ public String getIP () {
99100 return ip ;
100101 }
101102
@@ -131,6 +132,10 @@ public String getStateProvince() {
131132 return stateProvince ;
132133 }
133134
135+ public String getStateCode () {
136+ return stateCode ;
137+ }
138+
134139 public String getDistrict () {
135140 return district ;
136141 }
@@ -143,16 +148,16 @@ public String getZipCode() {
143148 return zipCode ;
144149 }
145150
146- public String getLatitude () {
151+ public BigDecimal getLatitude () {
147152 return latitude ;
148153 }
149154
150- public String getLongitude () {
155+ public BigDecimal getLongitude () {
151156 return longitude ;
152157 }
153158
154- public Boolean isEU () {
155- return isEU ;
159+ public boolean isEU () {
160+ return eu ;
156161 }
157162
158163 public String getCallingCode () {
@@ -187,8 +192,8 @@ public String getAsn() {
187192 return asn ;
188193 }
189194
190- public String getGeonameID () {
191- return geonameID ;
195+ public long getGeoNameId () {
196+ return geoNameId ;
192197 }
193198
194199 public GeolocationCurrency getCurrency () {
@@ -206,4 +211,9 @@ public GeolocationSecurity getGeolocationSecurity() {
206211 public UserAgent getUserAgent () {
207212 return userAgent ;
208213 }
214+
215+ @ Override
216+ public String toString () {
217+ return json .toString (2 );
218+ }
209219}
0 commit comments