Skip to content

Commit

Permalink
Updating JavaDoc, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaytotomato committed Jul 28, 2024
1 parent acaa915 commit 936408c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
16 changes: 15 additions & 1 deletion src/main/java/com/tomaytotomato/LocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Country> countries;
private final List<Country> countries;
/**
* One-to-one mappings (1:1)
*/
Expand All @@ -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();
Expand Down
53 changes: 30 additions & 23 deletions src/main/java/com/tomaytotomato/SearchLocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -419,32 +423,35 @@ private List<Location> getTopMatchingLocations(Map<Country, Integer> 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();
}

/**
Expand Down

0 comments on commit 936408c

Please sign in to comment.