From e54ee1f580bbb750d28e4c4befe8bb159144edc5 Mon Sep 17 00:00:00 2001 From: tomaytotomato <971697+tomaytotomato@users.noreply.github.com> Date: Fri, 9 Aug 2024 16:02:51 +0100 Subject: [PATCH] Refactored implementation for findByLocalisedName() Updated README.md documentation with more details on each method and example --- README.md | 35 +++++++++---------- .../com/tomaytotomato/LocationService.java | 15 ++++---- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 9a45cff..06121a7 100644 --- a/README.md +++ b/README.md @@ -47,25 +47,22 @@ public class Main { ``` -## What can location4j do? - -Here is an overview of the core functionality that location4j offers - -| Feature | Supported | Object | -|------------------------------|-----------|----------| -| Search (free text) | ✅ | Location | -| Find All Countries | ✅ | Country | -| Find Country by Id | ✅ | Country | -| Find Country by ISO2 code | ✅ | Country | -| Find Country by ISO3 code | ✅ | Country | -| Find Country by name | ✅ | Country | -| Find Country by Native name | ✅ | Country | -| Find Countries by State name | ✅ | Country | -| Find States by State name | ✅ | State | -| Find State by State Id | ✅ | State | -| Find States by State code | ✅ | State | -| Find City by City Id | ✅ | City | -| Find Cities by City name | ✅ | City | +| Feature | Supported | Object | Example | +|--------------------------------|-----------|----------|-------------------------------------------------------------------------| +| Search (free text) | ✅ | Location | `search("Canada, Alberta")` -> `[Location("Alberta", "Canada")]` | +| Find All Countries | ✅ | Country | `findAllCountries()` -> `[Country("Belgium"), Country("Canada"), ...]` | +| Find Country by Id | ✅ | Country | `findCountryById(1)` -> `Optional[Country("Afghanistan")]` | +| Find Country by ISO2 code | ✅ | Country | `findCountryByISO2Code("CA")` -> `Optional[Country("Canada")]` | +| Find Country by ISO3 code | ✅ | Country | `findCountryByISO3Code("CAN")` -> `Optional[Country("Canada")]` | +| Find Country by Name | ✅ | Country | `findCountryByName("Canada")` -> `Optional[Country("Canada")]` | +| Find Country by Localised name | ✅ | Country | `findCountryByLocalisedName("Belgique")` -> `Optional[Country("Belgium")]` | +| Find Countries by State name | ✅ | Country | `findAllCountriesByStateName("Texas")` -> `[Country("USA")]` | +| Find States by State name | ✅ | State | `findAllStatesByStateName("Texas")` -> `[State("Texas", "USA")]` | +| Find State by State Id | ✅ | State | `findStateById(5)` -> `Optional[State("California", "USA")]` | +| Find States by State code | ✅ | State | `findAllStatesByStateCode("CA")` -> `[State("California", "USA")]` | +| Find City by City Id | ✅ | City | `findCityById(10)` -> `Optional[City("Los Angeles", "California")]` | +| Find Cities by City name | ✅ | City | `findAllCitiesByCityName("San Francisco")` -> `[City("San Francisco", "California")]` | + 🟢 location4j can parse free text strings with or without punctuation or capitalisation e.g. > San Francisco, CA, USA diff --git a/library/src/main/java/com/tomaytotomato/LocationService.java b/library/src/main/java/com/tomaytotomato/LocationService.java index 57b39d3..73c19e5 100644 --- a/library/src/main/java/com/tomaytotomato/LocationService.java +++ b/library/src/main/java/com/tomaytotomato/LocationService.java @@ -31,7 +31,7 @@ public class LocationService implements FindCountry, FindState, FindCity { */ private final Map countryNameToCountryMap = new HashMap<>(); private final Map countryIdToCountryMap = new HashMap<>(); - private final Map countryNativeNameToCountry = new HashMap<>(); + private final Map localisedNameToCountryMap = new HashMap<>(); private final Map iso2CodeToCountryMap = new HashMap<>(); private final Map iso3CodeToCountryMap = new HashMap<>(); private final Map stateIdToStateMap = new HashMap<>(); @@ -73,11 +73,10 @@ private void buildDataStructures() { countries.forEach(country -> { countryIdToCountryMap.put(country.getId(), country); countryNameToCountryMap.put(keyMaker(country.getName()), country); - if (Objects.isNull(country.getNativeName()) || country.getNativeName().isEmpty()) { - logger.warning("Country has null native name, skipping mapping: " + country.getName()); - } else { - countryNativeNameToCountry.put(keyMaker(country.getNativeName()), country); - } + + localisedNameToCountryMap.put(keyMaker(country.getNativeName()), country); + country.getTranslations().values().stream().map(this::keyMaker) + .forEach(translatedName -> localisedNameToCountryMap.put(translatedName, country)); iso2CodeToCountryMap.put(keyMaker(country.getIso2()), country); iso3CodeToCountryMap.put(keyMaker(country.getIso3()), country); @@ -123,10 +122,10 @@ public Optional findCountryByName(String countryName) { @Override public Optional findCountryByLocalisedName(String localisedName) { if (Objects.isNull(localisedName) || localisedName.isEmpty()) { - throw new IllegalArgumentException("Country Native Name cannot be null or empty"); + throw new IllegalArgumentException("Country Localised Name cannot be null or empty"); } localisedName = textNormaliser.normalise(localisedName); - return Optional.ofNullable(countryNativeNameToCountry.get(localisedName)); + return Optional.ofNullable(localisedNameToCountryMap.get(localisedName)); } @Override