From 936408c6f5e5907a5453ea30edfbd98714757b5f Mon Sep 17 00:00:00 2001 From: tomaytotomato <971697+tomaytotomato@users.noreply.github.com> Date: Sun, 28 Jul 2024 18:30:04 +0100 Subject: [PATCH] Updating JavaDoc, refactor --- .../com/tomaytotomato/LocationService.java | 16 +++++- .../tomaytotomato/SearchLocationService.java | 53 +++++++++++-------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/tomaytotomato/LocationService.java b/src/main/java/com/tomaytotomato/LocationService.java index d73f7c5..e0748d1 100644 --- a/src/main/java/com/tomaytotomato/LocationService.java +++ b/src/main/java/com/tomaytotomato/LocationService.java @@ -5,6 +5,7 @@ import com.tomaytotomato.model.City; import com.tomaytotomato.model.Country; import com.tomaytotomato.model.State; +import com.tomaytotomato.text.normaliser.DefaultTextNormaliser; import com.tomaytotomato.text.normaliser.TextNormaliser; import com.tomaytotomato.usecase.FindCity; import com.tomaytotomato.usecase.FindCountry; @@ -17,11 +18,14 @@ import java.util.Optional; import java.util.logging.Logger; +/** + * This class provides simple lookups for Country, State and City information. + */ public class LocationService implements FindCountry, FindState, FindCity { private final Logger logger = Logger.getLogger(this.getClass().getPackage().getName() + this.getClass().getName()); - private List countries; + private final List countries; /** * One-to-one mappings (1:1) */ @@ -41,6 +45,16 @@ public class LocationService implements FindCountry, FindState, FindCity { private final TextNormaliser textNormaliser; + /** + * Default constructor, creates a LocationService class with default dependencies + */ + public LocationService() { + this.textNormaliser = new DefaultTextNormaliser(); + var dataLoader = new DefaultCountriesDataLoaderImpl(); + countries = dataLoader.getCountries(); + buildDataStructures(); + } + public LocationService(TextNormaliser textNormaliser) { this.textNormaliser = textNormaliser; var dataLoader = new DefaultCountriesDataLoaderImpl(); diff --git a/src/main/java/com/tomaytotomato/SearchLocationService.java b/src/main/java/com/tomaytotomato/SearchLocationService.java index 6c86a3e..1a4a12c 100644 --- a/src/main/java/com/tomaytotomato/SearchLocationService.java +++ b/src/main/java/com/tomaytotomato/SearchLocationService.java @@ -23,6 +23,10 @@ import java.util.Objects; import java.util.logging.Logger; +/** + * Provides search functionality to find a Country, State or City based on text input + * + */ public class SearchLocationService implements SearchLocation { private final Logger logger = Logger.getLogger(this.getClass().getName()); @@ -419,32 +423,35 @@ private List getTopMatchingLocations(Map countryHits return List.of(locationMapper.toLocation(topState)); } } else { - var locationBuilder = Location.builder(); - - locationBuilder.countryName(topCountry.getName()); - locationBuilder.countryId(topCountry.getId()); - locationBuilder.countryIso2Code(topCountry.getIso2()); - locationBuilder.countryIso3Code(topCountry.getIso3()); - locationBuilder.latitude(topCountry.getLatitude()); - locationBuilder.longitude(topCountry.getLongitude()); - - if (!Objects.isNull(topState)) { - locationBuilder.stateName(topState.getName()); - locationBuilder.stateCode(topState.getStateCode()); - locationBuilder.stateId(topState.getId()); - locationBuilder.latitude(topState.getLatitude()); - locationBuilder.longitude(topState.getLongitude()); - } + return List.of(buildLocationResult(topCountry, topState, topCity)); + } + } - if (!Objects.isNull(topCity)) { - locationBuilder.city(topCity.getName()); - locationBuilder.cityId(topCity.getId()); - locationBuilder.latitude(topCity.getLatitude()); - locationBuilder.longitude(topCity.getLongitude()); - } + private static Location buildLocationResult(Country topCountry, State topState, City topCity) { + var locationBuilder = Location.builder(); + + locationBuilder.countryName(topCountry.getName()); + locationBuilder.countryId(topCountry.getId()); + locationBuilder.countryIso2Code(topCountry.getIso2()); + locationBuilder.countryIso3Code(topCountry.getIso3()); + locationBuilder.latitude(topCountry.getLatitude()); + locationBuilder.longitude(topCountry.getLongitude()); + + if (!Objects.isNull(topState)) { + locationBuilder.stateName(topState.getName()); + locationBuilder.stateCode(topState.getStateCode()); + locationBuilder.stateId(topState.getId()); + locationBuilder.latitude(topState.getLatitude()); + locationBuilder.longitude(topState.getLongitude()); + } - return List.of(locationBuilder.build()); + if (!Objects.isNull(topCity)) { + locationBuilder.city(topCity.getName()); + locationBuilder.cityId(topCity.getId()); + locationBuilder.latitude(topCity.getLatitude()); + locationBuilder.longitude(topCity.getLongitude()); } + return locationBuilder.build(); } /**