Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ protected Map<String, Object> transform(final CityResponse response) {
switch (property) {
case IP -> data.put("ip", response.getTraits().getIpAddress());
case COUNTRY_IN_EUROPEAN_UNION -> {
if (country.getIsoCode() != null) {
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
data.put("country_in_european_union", country.isInEuropeanUnion());
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
if (isInEuropeanUnion != null) {
data.put("country_in_european_union", isInEuropeanUnion);
}
}
case COUNTRY_ISO_CODE -> {
Expand Down Expand Up @@ -239,12 +239,8 @@ protected Map<String, Object> transform(final CityResponse response) {
}
}
case REGION_ISO_CODE -> {
// ISO 3166-2 code for country subdivisions.
// See iso.org/iso-3166-country-codes.html
String countryIso = country.getIsoCode();
String subdivisionIso = subdivision.getIsoCode();
if (countryIso != null && subdivisionIso != null) {
String regionIsoCode = countryIso + "-" + subdivisionIso;
String regionIsoCode = regionIsoCode(country, subdivision);
if (regionIsoCode != null) {
data.put("region_iso_code", regionIsoCode);
}
}
Expand All @@ -270,7 +266,7 @@ protected Map<String, Object> transform(final CityResponse response) {
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
if (latitude != null && longitude != null) {
Map<String, Object> locationObject = new HashMap<>();
Map<String, Object> locationObject = HashMap.newHashMap(2);
locationObject.put("lat", latitude);
locationObject.put("lon", longitude);
data.put("location", locationObject);
Expand All @@ -288,9 +284,9 @@ protected Map<String, Object> transform(final CityResponse response) {
}
}
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
if (registeredCountry.getIsoCode() != null) {
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
if (isInEuropeanUnion != null) {
data.put("registered_country_in_european_union", isInEuropeanUnion);
}
}
case REGISTERED_COUNTRY_ISO_CODE -> {
Expand Down Expand Up @@ -353,9 +349,9 @@ protected Map<String, Object> transform(final CountryResponse response) {
switch (property) {
case IP -> data.put("ip", response.getTraits().getIpAddress());
case COUNTRY_IN_EUROPEAN_UNION -> {
if (country.getIsoCode() != null) {
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
data.put("country_in_european_union", country.isInEuropeanUnion());
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
if (isInEuropeanUnion != null) {
data.put("country_in_european_union", isInEuropeanUnion);
}
}
case COUNTRY_ISO_CODE -> {
Expand Down Expand Up @@ -383,9 +379,9 @@ protected Map<String, Object> transform(final CountryResponse response) {
}
}
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
if (registeredCountry.getIsoCode() != null) {
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
if (isInEuropeanUnion != null) {
data.put("registered_country_in_european_union", isInEuropeanUnion);
}
}
case REGISTERED_COUNTRY_ISO_CODE -> {
Expand Down Expand Up @@ -480,9 +476,9 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
}
}
case COUNTRY_IN_EUROPEAN_UNION -> {
if (country.getIsoCode() != null) {
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
data.put("country_in_european_union", country.isInEuropeanUnion());
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
if (isInEuropeanUnion != null) {
data.put("country_in_european_union", isInEuropeanUnion);
}
}
case COUNTRY_ISO_CODE -> {
Expand Down Expand Up @@ -510,12 +506,8 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
}
}
case REGION_ISO_CODE -> {
// ISO 3166-2 code for country subdivisions.
// See iso.org/iso-3166-country-codes.html
String countryIso = country.getIsoCode();
String subdivisionIso = subdivision.getIsoCode();
if (countryIso != null && subdivisionIso != null) {
String regionIsoCode = countryIso + "-" + subdivisionIso;
String regionIsoCode = regionIsoCode(country, subdivision);
if (regionIsoCode != null) {
data.put("region_iso_code", regionIsoCode);
}
}
Expand Down Expand Up @@ -547,7 +539,7 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
if (latitude != null && longitude != null) {
Map<String, Object> locationObject = new HashMap<>();
Map<String, Object> locationObject = HashMap.newHashMap(2);
locationObject.put("lat", latitude);
locationObject.put("lon", longitude);
data.put("location", locationObject);
Expand Down Expand Up @@ -639,9 +631,9 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
}
}
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
if (registeredCountry.getIsoCode() != null) {
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
if (isInEuropeanUnion != null) {
data.put("registered_country_in_european_union", isInEuropeanUnion);
}
}
case REGISTERED_COUNTRY_ISO_CODE -> {
Expand Down Expand Up @@ -776,4 +768,23 @@ private RESPONSE lookup(final Reader reader, final String ipAddress) throws IOEx
*/
protected abstract Map<String, Object> transform(RESPONSE response);
}

@Nullable
private static Boolean isInEuropeanUnion(com.maxmind.geoip2.record.Country country) {
// isInEuropeanUnion is a lowercase-b boolean so it cannot be null, but it really only makes sense for us to return a value
// for this if there's actually a real country here, as opposed to an empty null-object country, so we check for an iso code first
return (country.getIsoCode() == null) ? null : country.isInEuropeanUnion();
}

@Nullable
private static String regionIsoCode(final com.maxmind.geoip2.record.Country country, final Subdivision subdivision) {
// ISO 3166-2 code for country subdivisions, see https://www.iso.org/iso-3166-country-codes.html
final String countryIso = country.getIsoCode();
final String subdivisionIso = subdivision.getIsoCode();
if (countryIso != null && subdivisionIso != null) {
return countryIso + "-" + subdivisionIso;
} else {
return null;
}
}
}
Loading