Skip to content

Commit

Permalink
Refactored implementation for findByLocalisedName()
Browse files Browse the repository at this point in the history
Updated README.md documentation with more details on each method and example
  • Loading branch information
tomaytotomato committed Aug 9, 2024
1 parent d5b3597 commit e54ee1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
35 changes: 16 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions library/src/main/java/com/tomaytotomato/LocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class LocationService implements FindCountry, FindState, FindCity {
*/
private final Map<String, Country> countryNameToCountryMap = new HashMap<>();
private final Map<Integer, Country> countryIdToCountryMap = new HashMap<>();
private final Map<String, Country> countryNativeNameToCountry = new HashMap<>();
private final Map<String, Country> localisedNameToCountryMap = new HashMap<>();
private final Map<String, Country> iso2CodeToCountryMap = new HashMap<>();
private final Map<String, Country> iso3CodeToCountryMap = new HashMap<>();
private final Map<Integer, State> stateIdToStateMap = new HashMap<>();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -123,10 +122,10 @@ public Optional<Country> findCountryByName(String countryName) {
@Override
public Optional<Country> 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
Expand Down

0 comments on commit e54ee1f

Please sign in to comment.