|
1 | 1 | package io.ipgeolocation.api; |
2 | 2 |
|
3 | 3 | import java.math.BigDecimal; |
4 | | -import java.util.Map; |
| 4 | +import java.util.Objects; |
| 5 | +import org.json.JSONObject; |
5 | 6 |
|
6 | 7 | public class TimezoneGeo { |
7 | | - private final String countryCode2; |
8 | | - private final String countryCode3; |
9 | | - private final String countryName; |
10 | | - private final String stateProvince; |
11 | | - private final String district; |
12 | | - private final String city; |
13 | | - private final String zipCode; |
14 | | - private BigDecimal latitude; |
15 | | - private BigDecimal longitude; |
16 | | - |
17 | | - TimezoneGeo(Map<String, Object> json) { |
18 | | - this.countryCode2 = (String) json.get("country_code2"); |
19 | | - this.countryCode3 = (String) json.get("country_code3"); |
20 | | - this.countryName = (String) json.get("country_name"); |
21 | | - this.stateProvince = (String) json.get("state_prov"); |
22 | | - this.district = (String) json.get("district"); |
23 | | - this.city = (String) json.get("city"); |
24 | | - this.zipCode = (String) json.get("zipcode"); |
25 | | - |
26 | | - if (json.get("latitude") instanceof String) { |
27 | | - this.latitude = new BigDecimal((String) json.get("latitude")); |
28 | | - } else if (json.get("latitude") instanceof BigDecimal) { |
29 | | - this.latitude = (BigDecimal) json.get("latitude"); |
30 | | - } |
31 | | - |
32 | | - if (json.get("longitude") instanceof String) { |
33 | | - this.longitude = new BigDecimal((String) json.get("longitude")); |
34 | | - } else if (json.get("longitude") instanceof BigDecimal) { |
35 | | - this.longitude = (BigDecimal) json.get("longitude"); |
36 | | - } |
37 | | - } |
| 8 | + private final String countryCode2; |
| 9 | + private final String countryCode3; |
| 10 | + private final String countryName; |
| 11 | + private final String stateProvince; |
| 12 | + private final String district; |
| 13 | + private final String city; |
| 14 | + private final String locality; |
| 15 | + private final String zipCode; |
| 16 | + private final BigDecimal latitude; |
| 17 | + private final BigDecimal longitude; |
| 18 | + private final JSONObject json; |
38 | 19 |
|
39 | | - public String getCountryCode2() { |
40 | | - return countryCode2; |
| 20 | + TimezoneGeo(JSONObject json) { |
| 21 | + if (Objects.isNull(json)) { |
| 22 | + throw new IllegalArgumentException("'json' must not be null"); |
41 | 23 | } |
42 | 24 |
|
43 | | - public String getCountryCode3() { |
44 | | - return countryCode3; |
| 25 | + if (json.isEmpty()) { |
| 26 | + throw new IllegalArgumentException("'json' must not be empty"); |
45 | 27 | } |
46 | 28 |
|
47 | | - public String getCountryName() { |
48 | | - return countryName; |
49 | | - } |
| 29 | + this.countryCode2 = json.optString("country_code2"); |
| 30 | + this.countryCode3 = json.optString("country_code3"); |
| 31 | + this.countryName = json.optString("country_name", json.optString("country")); |
| 32 | + this.stateProvince = json.optString("state_prov", json.optString("state")); |
| 33 | + this.district = json.optString("district"); |
| 34 | + this.city = json.getString("city"); |
| 35 | + this.locality = json.optString("locality"); |
| 36 | + this.zipCode = json.optString("zipcode"); |
| 37 | + this.latitude = json.getBigDecimal("latitude"); |
| 38 | + this.longitude = json.getBigDecimal("longitude"); |
| 39 | + this.json = json; |
| 40 | + } |
50 | 41 |
|
51 | | - public String getStateProvince() { |
52 | | - return stateProvince; |
53 | | - } |
| 42 | + public String getCountryCode2() { |
| 43 | + return countryCode2; |
| 44 | + } |
54 | 45 |
|
55 | | - public String getDistrict() { |
56 | | - return district; |
57 | | - } |
| 46 | + public String getCountryCode3() { |
| 47 | + return countryCode3; |
| 48 | + } |
58 | 49 |
|
59 | | - public String getCity() { |
60 | | - return city; |
61 | | - } |
| 50 | + public String getCountryName() { |
| 51 | + return countryName; |
| 52 | + } |
62 | 53 |
|
63 | | - public String getZipCode() { |
64 | | - return zipCode; |
65 | | - } |
| 54 | + public String getStateProvince() { |
| 55 | + return stateProvince; |
| 56 | + } |
66 | 57 |
|
67 | | - public BigDecimal getLatitude() { |
68 | | - return latitude; |
69 | | - } |
| 58 | + public String getDistrict() { |
| 59 | + return district; |
| 60 | + } |
70 | 61 |
|
71 | | - public BigDecimal getLongitude() { |
72 | | - return longitude; |
73 | | - } |
| 62 | + public String getCity() { |
| 63 | + return city; |
| 64 | + } |
| 65 | + |
| 66 | + public String getLocality() { |
| 67 | + return locality; |
| 68 | + } |
| 69 | + |
| 70 | + public String getZipCode() { |
| 71 | + return zipCode; |
| 72 | + } |
| 73 | + |
| 74 | + public BigDecimal getLatitude() { |
| 75 | + return latitude; |
| 76 | + } |
| 77 | + |
| 78 | + public BigDecimal getLongitude() { |
| 79 | + return longitude; |
| 80 | + } |
74 | 81 |
|
| 82 | + @Override |
| 83 | + public String toString() { |
| 84 | + return json.toString(2); |
| 85 | + } |
75 | 86 | } |
0 commit comments