Skip to content

Commit

Permalink
Add Builder classes
Browse files Browse the repository at this point in the history
Allows the creation of SearchLocationService.java and LocationService.java to be more controlled.

Fixes #13
  • Loading branch information
tomaytotomato committed Aug 11, 2024
1 parent fa782ec commit 15b6480
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 76 deletions.
21 changes: 1 addition & 20 deletions location4j/src/main/java/com/tomaytotomato/LocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import static java.lang.Math.sqrt;

import com.tomaytotomato.loader.CountriesDataLoader;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
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 Down Expand Up @@ -51,24 +49,7 @@ 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();
countries = dataLoader.getCountries();
buildDataStructures();
}

public LocationService(TextNormaliser textNormaliser, CountriesDataLoader dataLoader) {
protected LocationService(TextNormaliser textNormaliser, CountriesDataLoader dataLoader) {
this.textNormaliser = textNormaliser;
countries = dataLoader.getCountries();
buildDataStructures();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.tomaytotomato;

import com.tomaytotomato.loader.CountriesDataLoader;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.text.normaliser.DefaultTextNormaliser;
import com.tomaytotomato.text.normaliser.TextNormaliser;

public final class LocationServiceBuilder {

private TextNormaliser textNormaliser = new DefaultTextNormaliser();
private CountriesDataLoader countriesDataLoader = new DefaultCountriesDataLoaderImpl();

public static LocationServiceBuilder builder() {
return new LocationServiceBuilder();
}

private LocationServiceBuilder() {
}

public LocationServiceBuilder withCountriesDataLoader(CountriesDataLoader countriesDataLoader) {
this.countriesDataLoader = countriesDataLoader;
return this;
}

public LocationServiceBuilder withTextNormaliser(TextNormaliser textNormaliser) {
this.textNormaliser = textNormaliser;
return this;
}

public LocationService build() {
return new LocationService(textNormaliser, countriesDataLoader);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package com.tomaytotomato;

import com.tomaytotomato.aliases.DefaultLocationAliases;
import com.tomaytotomato.aliases.LocationAliases;
import com.tomaytotomato.loader.CountriesDataLoader;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.mapper.DefaultLocationMapper;
import com.tomaytotomato.mapper.LocationMapper;
import com.tomaytotomato.model.City;
import com.tomaytotomato.model.Country;
import com.tomaytotomato.model.Location;
import com.tomaytotomato.model.State;
import com.tomaytotomato.text.normaliser.DefaultTextNormaliser;
import com.tomaytotomato.text.normaliser.TextNormaliser;
import com.tomaytotomato.text.tokeniser.DefaultTextTokeniser;
import com.tomaytotomato.text.tokeniser.TextTokeniser;
import com.tomaytotomato.usecase.SearchLocation;
import java.util.ArrayList;
Expand Down Expand Up @@ -51,17 +46,7 @@ public class SearchLocationService implements SearchLocation {
private final LocationMapper locationMapper;
private final LocationAliases locationAliases;

public SearchLocationService() {
textTokeniser = new DefaultTextTokeniser();
textNormaliser = new DefaultTextNormaliser();
locationMapper = new DefaultLocationMapper();
locationAliases = new DefaultLocationAliases();
var dataLoader = new DefaultCountriesDataLoaderImpl();
countries = dataLoader.getCountries();
buildDataStructures();
}

public SearchLocationService(TextTokeniser textTokeniser, TextNormaliser textNormaliser,
protected SearchLocationService(TextTokeniser textTokeniser, TextNormaliser textNormaliser,
LocationMapper locationMapper, CountriesDataLoader dataLoader,
LocationAliases locationAliases) {
this.textTokeniser = textTokeniser;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.tomaytotomato;

import com.tomaytotomato.aliases.DefaultLocationAliases;
import com.tomaytotomato.aliases.LocationAliases;
import com.tomaytotomato.loader.CountriesDataLoader;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.mapper.DefaultLocationMapper;
import com.tomaytotomato.mapper.LocationMapper;
import com.tomaytotomato.text.normaliser.DefaultTextNormaliser;
import com.tomaytotomato.text.normaliser.TextNormaliser;
import com.tomaytotomato.text.tokeniser.DefaultTextTokeniser;
import com.tomaytotomato.text.tokeniser.TextTokeniser;

/**
* Allows the customisation and creation of the {@link SearchLocationService}
*/
public final class SearchLocationServiceBuilder {

private TextTokeniser textTokeniser = new DefaultTextTokeniser();
private TextNormaliser textNormaliser = new DefaultTextNormaliser();
private LocationMapper locationMapper = new DefaultLocationMapper();
private LocationAliases locationAliases = new DefaultLocationAliases();
private CountriesDataLoader countriesDataLoader = new DefaultCountriesDataLoaderImpl();

public static SearchLocationServiceBuilder builder() {
return new SearchLocationServiceBuilder();
}

private SearchLocationServiceBuilder() {
}

public SearchLocationServiceBuilder withTextTokeniser(TextTokeniser textTokeniser) {
this.textTokeniser = textTokeniser;
return this;
}

public SearchLocationServiceBuilder withTextNormaliser(TextNormaliser textNormaliser) {
this.textNormaliser = textNormaliser;
return this;
}

public SearchLocationServiceBuilder withLocationMapper(LocationMapper locationMapper) {
this.locationMapper = locationMapper;
return this;
}

public SearchLocationServiceBuilder withLocationAliases(LocationAliases locationAliases) {
this.locationAliases = locationAliases;
return this;
}

public SearchLocationServiceBuilder withCountriesDataLoader(CountriesDataLoader countriesDataLoader) {
this.countriesDataLoader = countriesDataLoader;
return this;
}

public SearchLocationService build() {
return new SearchLocationService(textTokeniser, textNormaliser, locationMapper,
countriesDataLoader,
locationAliases);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.tomaytotomato.LocationService;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.text.normaliser.DefaultTextNormaliser;
import com.tomaytotomato.LocationServiceBuilder;
import java.math.BigDecimal;
import jdk.jfr.Description;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -21,9 +19,8 @@ class FindCityTest {
private final FindCity locationService;

public FindCityTest() {
var textNormaliser = new DefaultTextNormaliser();
var dataLoader = new DefaultCountriesDataLoaderImpl();
locationService = new LocationService(textNormaliser, dataLoader);
locationService = LocationServiceBuilder.builder()
.build();
}

@Description("Find City by Lat/long with BigDecimal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.tomaytotomato.LocationService;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.LocationServiceBuilder;
import com.tomaytotomato.model.Country;
import com.tomaytotomato.text.normaliser.DefaultTextNormaliser;
import jdk.jfr.Description;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -20,9 +18,7 @@ class FindCountriesByStateTest {
private final FindCountry locationService;

public FindCountriesByStateTest() {
var textNormaliser = new DefaultTextNormaliser();
var dataLoader = new DefaultCountriesDataLoaderImpl();
locationService = new LocationService(textNormaliser, dataLoader);
locationService = LocationServiceBuilder.builder().build();
}

@Description("Find All Countries By State Name, when null or blank then throw exception")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.tomaytotomato.LocationService;
import com.tomaytotomato.LocationServiceBuilder;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.text.normaliser.DefaultTextNormaliser;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -19,9 +19,10 @@ class FindCountryByISOCodeTest {
private final FindCountry locationService;

public FindCountryByISOCodeTest() {
var textNormaliser = new DefaultTextNormaliser();
var dataLoader = new DefaultCountriesDataLoaderImpl();
locationService = new LocationService(textNormaliser, dataLoader);
locationService = LocationServiceBuilder.builder()
.withCountriesDataLoader(new DefaultCountriesDataLoaderImpl())
.withTextNormaliser(new DefaultTextNormaliser())
.build();
}

@DisplayName("Get Country By ISO2 Code, when null or blank then throw exception")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.tomaytotomato.LocationService;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.LocationServiceBuilder;
import com.tomaytotomato.model.Country;
import com.tomaytotomato.text.normaliser.DefaultTextNormaliser;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -21,9 +19,7 @@ class FindCountryTest {
private final FindCountry locationService;

public FindCountryTest() {
var textNormaliser = new DefaultTextNormaliser();
var dataLoader = new DefaultCountriesDataLoaderImpl();
locationService = new LocationService(textNormaliser, dataLoader);
locationService = LocationServiceBuilder.builder().build();
}

@DisplayName("Find Country By ID, when valid and exists, then return Country")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.tomaytotomato.LocationService;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.text.normaliser.DefaultTextNormaliser;
import com.tomaytotomato.LocationServiceBuilder;
import jdk.jfr.Description;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -20,9 +18,7 @@ class FindStateTest {
private final FindState locationService;

public FindStateTest() {
var textNormaliser = new DefaultTextNormaliser();
var dataLoader = new DefaultCountriesDataLoaderImpl();
locationService = new LocationService(textNormaliser, dataLoader);
locationService = LocationServiceBuilder.builder().build();
}

@Description("Find State By ID, when null then throw exception")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import com.tomaytotomato.SearchLocationService;
import com.tomaytotomato.SearchLocationServiceBuilder;
import com.tomaytotomato.aliases.DefaultLocationAliases;
import com.tomaytotomato.loader.DefaultCountriesDataLoaderImpl;
import com.tomaytotomato.mapper.DefaultLocationMapper;
Expand All @@ -23,24 +23,21 @@ class SearchLocationTest {
private final SearchLocation searchLocationService;

public SearchLocationTest() {
var textNormaliser = new DefaultTextNormaliser();
var textTokeniser = new DefaultTextTokeniser();
var locationMapper = new DefaultLocationMapper();
var locationAliases = new DefaultLocationAliases();
var dataLoader = new DefaultCountriesDataLoaderImpl();

searchLocationService = new SearchLocationService(textTokeniser, textNormaliser, locationMapper,
dataLoader, locationAliases);
searchLocationService = SearchLocationServiceBuilder.builder()
.withLocationAliases(new DefaultLocationAliases())
.withLocationMapper(new DefaultLocationMapper())
.withCountriesDataLoader(new DefaultCountriesDataLoaderImpl())
.withTextNormaliser(new DefaultTextNormaliser())
.withTextTokeniser(new DefaultTextTokeniser())
.build();
}

@Description("SearchLocation, when null or empty text, then throw exception")
@Test
void search_WhenNullOrBlank_ThenThrowException() {

// When Then
assertThatThrownBy(() -> {
searchLocationService.search(null);
}).isInstanceOf(IllegalArgumentException.class)
assertThatThrownBy(() -> searchLocationService.search(null)).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("SearchLocation Text cannot be null or empty");
}

Expand Down

0 comments on commit 15b6480

Please sign in to comment.