diff --git a/src/main/java/fayder/restcountries/domain/BaseCountry.java b/src/main/java/fayder/restcountries/domain/BaseCountry.java index 487fd95f..28365179 100644 --- a/src/main/java/fayder/restcountries/domain/BaseCountry.java +++ b/src/main/java/fayder/restcountries/domain/BaseCountry.java @@ -46,151 +46,75 @@ public String getName() { return name; } - public void setName(String name) { - this.name = name; - } - public List getTopLevelDomain() { return topLevelDomain; } - public void setTopLevelDomain(List topLevelDomain) { - this.topLevelDomain = topLevelDomain; - } - public String getAlpha2Code() { return alpha2Code; } - public void setAlpha2Code(String alpha2Code) { - this.alpha2Code = alpha2Code; - } - public String getAlpha3Code() { return alpha3Code; } - public void setAlpha3Code(String alpha3Code) { - this.alpha3Code = alpha3Code; - } - public List getCallingCodes() { return callingCodes; } - public void setCallingCodes(List callingCodes) { - this.callingCodes = callingCodes; - } - public String getCapital() { return capital; } - public void setCapital(String capital) { - this.capital = capital; - } - public List getAltSpellings() { return altSpellings; } - public void setAltSpellings(List altSpellings) { - this.altSpellings = altSpellings; - } - public String getRelevance() { return relevance; } - public void setRelevance(String relevance) { - this.relevance = relevance; - } - public String getRegion() { return region; } - public void setRegion(String region) { - this.region = region; - } - public String getSubregion() { return subregion; } - public void setSubregion(String subregion) { - this.subregion = subregion; - } - public Integer getPopulation() { return population; } - public void setPopulation(Integer population) { - this.population = population; - } - public List getLatlng() { return latlng; } - public void setLatlng(List latlng) { - this.latlng = latlng; - } - public String getDemonym() { return demonym; } - public void setDemonym(String demonym) { - this.demonym = demonym; - } - public Double getArea() { return area; } - public void setArea(Double area) { - this.area = area; - } - public Double getGini() { return gini; } - public void setGini(Double gini) { - this.gini = gini; - } - public List getTimezones() { return timezones; } - public void setTimezones(List timezones) { - this.timezones = timezones; - } - public List getBorders() { return borders; } - public void setBorders(List borders) { - this.borders = borders; - } - public String getNativeName() { return nativeName; } - public void setNativeName(String nativeName) { - this.nativeName = nativeName; - } - public String getNumericCode() { return numericCode; } - - public void setNumericCode(String numericCode) { - this.numericCode = numericCode; - } } diff --git a/src/main/java/fayder/restcountries/rest/CountryServiceHelper.java b/src/main/java/fayder/restcountries/rest/CountryServiceBase.java similarity index 78% rename from src/main/java/fayder/restcountries/rest/CountryServiceHelper.java rename to src/main/java/fayder/restcountries/rest/CountryServiceBase.java index b9c5a26c..48e3cd70 100644 --- a/src/main/java/fayder/restcountries/rest/CountryServiceHelper.java +++ b/src/main/java/fayder/restcountries/rest/CountryServiceBase.java @@ -16,11 +16,11 @@ import java.util.Arrays; import java.util.List; -public class CountryServiceHelper { +public class CountryServiceBase { - private static final Logger LOG = Logger.getLogger(CountryServiceHelper.class); + private static final Logger LOG = Logger.getLogger(CountryServiceBase.class); - public static T getByAlpha(String alpha, List countries) { + protected T getByAlpha(String alpha, List countries) { int alphaLength = alpha.length(); for (T country : countries) { if (alphaLength == 2) { @@ -36,7 +36,7 @@ public static T getByAlpha(String alpha, List countri return null; } - public static List getByCodeList(String codeList, List countries) { + protected List getByCodeList(String codeList, List countries) { List result = new ArrayList<>(); if(codeList == null) return result; @@ -49,7 +49,7 @@ public static List getByCodeList(String codeList, List getByName(String name, boolean fullText, List countries) { + protected List getByName(String name, boolean fullText, List countries) { if(fullText) { return fulltextSearch(name, countries); } else { @@ -57,7 +57,7 @@ public static List getByName(String name, boolean fullTex } } - public static List getByCallingCode(String callingCode, List countries) { + protected List getByCallingCode(String callingCode, List countries) { List result = new ArrayList<>(); for(BaseCountry country : countries) { for(String c : country.getCallingCodes()) { @@ -68,7 +68,7 @@ public static List getByCallingCode(String callingCode, L return result; } - public static List getByCapital(String capital, List countries) { + protected List getByCapital(String capital, List countries) { List result = new ArrayList<>(); for(BaseCountry country : countries) { if(normalize(country.getCapital().toLowerCase()).contains(normalize(capital.toLowerCase()))) { @@ -78,7 +78,7 @@ public static List getByCapital(String capital, List getByRegion(String region, List countries) { + protected List getByRegion(String region, List countries) { List result = new ArrayList<>(); for(BaseCountry country : countries) { if(country.getRegion().toLowerCase().equals(region.toLowerCase())) { @@ -88,7 +88,7 @@ public static List getByRegion(String region, List getBySubregion(String subregion, List countries) { + protected List getBySubregion(String subregion, List countries) { List result = new ArrayList<>(); for(BaseCountry country : countries) { if(country.getSubregion().toLowerCase().equals(subregion.toLowerCase())) { @@ -98,26 +98,7 @@ public static List getBySubregion(String subregion, List< return result; } - public static List loadJson(String filename, Class clazz) { - LOG.debug("Loading JSON " + filename); - List countries = new ArrayList<>(); - InputStream is = CountryServiceHelper.class.getClassLoader().getResourceAsStream(filename); - Gson gson = new Gson(); - JsonReader reader; - try { - reader = new JsonReader(new InputStreamReader(is, "UTF-8")); - reader.beginArray(); - while(reader.hasNext()) { - BaseCountry country = gson.fromJson(reader, clazz); - countries.add(country); - } - } catch (Exception e) { - LOG.error("Could not load JSON " + filename); - } - return countries; - } - - private static List fulltextSearch(String name, List countries) { + private List fulltextSearch(String name, List countries) { // Using 2 different 'for' loops to give priority to 'name' matches over alternative spellings List result = new ArrayList<>(); for (BaseCountry country : countries) { @@ -136,7 +117,7 @@ private static List fulltextSearch(String name, List substringSearch(String name, List countries) { + private List substringSearch(String name, List countries) { // Using 2 different 'for' loops to give priority to 'name' matches over alternative spellings List result = new ArrayList<>(); for(BaseCountry country : countries) { @@ -155,8 +136,27 @@ private static List substringSearch(String name, List loadJson(String filename, Class clazz) { + LOG.debug("Loading JSON " + filename); + List countries = new ArrayList<>(); + InputStream is = CountryServiceBase.class.getClassLoader().getResourceAsStream(filename); + Gson gson = new Gson(); + JsonReader reader; + try { + reader = new JsonReader(new InputStreamReader(is, "UTF-8")); + reader.beginArray(); + while(reader.hasNext()) { + BaseCountry country = gson.fromJson(reader, clazz); + countries.add(country); + } + } catch (Exception e) { + LOG.error("Could not load JSON " + filename); + } + return countries; + } } diff --git a/src/main/java/fayder/restcountries/v1/rest/CountryService.java b/src/main/java/fayder/restcountries/v1/rest/CountryService.java index 034bbd00..e8a4ace7 100644 --- a/src/main/java/fayder/restcountries/v1/rest/CountryService.java +++ b/src/main/java/fayder/restcountries/v1/rest/CountryService.java @@ -3,14 +3,14 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package fayder.restcountries.v1.rest; -import fayder.restcountries.rest.CountryServiceHelper; +import fayder.restcountries.rest.CountryServiceBase; import fayder.restcountries.v1.domain.Country; import org.apache.log4j.Logger; import java.util.ArrayList; import java.util.List; -public class CountryService { +public class CountryService extends CountryServiceBase { private static final Logger LOG = Logger.getLogger(CountryService.class); @@ -33,38 +33,38 @@ public List getAll() { } public Country getByAlpha(String alpha) { - return CountryServiceHelper.getByAlpha(alpha, countries); + return super.getByAlpha(alpha, countries); } @SuppressWarnings("unchecked") public List getByCodeList(String codeList) { - return (List) CountryServiceHelper.getByCodeList(codeList, countries); + return (List) super.getByCodeList(codeList, countries); } @SuppressWarnings("unchecked") public List getByName(String name, boolean isFullText) { - return (List) CountryServiceHelper.getByName(name, isFullText, countries); + return (List) super.getByName(name, isFullText, countries); } @SuppressWarnings("unchecked") public List getByCallingCode(String callingcode) { - return (List) CountryServiceHelper.getByCallingCode(callingcode, countries); + return (List) super.getByCallingCode(callingcode, countries); } @SuppressWarnings("unchecked") public List getByCapital(String capital) { - return (List) CountryServiceHelper.getByCapital(capital, countries); + return (List) super.getByCapital(capital, countries); } @SuppressWarnings("unchecked") public List getByRegion(String region) { - return (List) CountryServiceHelper.getByRegion(region, countries); + return (List) super.getByRegion(region, countries); } @SuppressWarnings("unchecked") public List getBySubregion(String subregion) { - return (List) CountryServiceHelper.getBySubregion(subregion, countries); + return (List) super.getBySubregion(subregion, countries); } public List getByCurrency(String currency) { @@ -93,6 +93,6 @@ public List getByLanguage(String language) { @SuppressWarnings("unchecked") private void initialize() { - countries = (List) CountryServiceHelper.loadJson("countriesV1.json", Country.class); + countries = (List) super.loadJson("countriesV1.json", Country.class); } } diff --git a/src/main/java/fayder/restcountries/v2/rest/CountryService.java b/src/main/java/fayder/restcountries/v2/rest/CountryService.java index 4cb0c51e..b6eb8080 100644 --- a/src/main/java/fayder/restcountries/v2/rest/CountryService.java +++ b/src/main/java/fayder/restcountries/v2/rest/CountryService.java @@ -3,18 +3,17 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package fayder.restcountries.v2.rest; -import fayder.restcountries.rest.CountryServiceHelper; +import fayder.restcountries.rest.CountryServiceBase; import fayder.restcountries.v2.domain.Country; import fayder.restcountries.v2.domain.Currency; import fayder.restcountries.v2.domain.Language; import fayder.restcountries.v2.domain.RegionalBloc; -import org.apache.commons.codec.binary.StringUtils; import org.apache.log4j.Logger; import java.util.ArrayList; import java.util.List; -public class CountryService { +public class CountryService extends CountryServiceBase { private static final Logger LOG = Logger.getLogger(CountryService.class); @@ -37,37 +36,37 @@ public List getAll() { } public Country getByAlpha(String alpha) { - return CountryServiceHelper.getByAlpha(alpha, countries); + return super.getByAlpha(alpha, countries); } @SuppressWarnings("unchecked") public List getByCodeList(String codeList) { - return (List) CountryServiceHelper.getByCodeList(codeList, countries); + return (List) super.getByCodeList(codeList, countries); } @SuppressWarnings("unchecked") public List getByName(String name, boolean isFullText) { - return (List) CountryServiceHelper.getByName(name, isFullText, countries); + return (List) super.getByName(name, isFullText, countries); } @SuppressWarnings("unchecked") public List getByCallingCode(String callingcode) { - return (List) CountryServiceHelper.getByCallingCode(callingcode, countries); + return (List) super.getByCallingCode(callingcode, countries); } @SuppressWarnings("unchecked") public List getByCapital(String capital) { - return (List) CountryServiceHelper.getByCapital(capital, countries); + return (List) super.getByCapital(capital, countries); } @SuppressWarnings("unchecked") public List getByRegion(String region) { - return (List) CountryServiceHelper.getByRegion(region, countries); + return (List) super.getByRegion(region, countries); } @SuppressWarnings("unchecked") public List getBySubRegion(String subregion) { - return (List) CountryServiceHelper.getBySubregion(subregion, countries); + return (List) super.getBySubregion(subregion, countries); } public List getByCurrency(String currency) { @@ -107,7 +106,7 @@ public List getByLanguage(String language) { public List getByDemonym(String demonym) { List result = new ArrayList<>(); for (Country country : countries) { - if (country.getDemonym().toLowerCase().equals(CountryServiceHelper.normalize(demonym.toLowerCase()))) { + if (country.getDemonym().toLowerCase().equals(normalize(demonym.toLowerCase()))) { result.add(country); } } @@ -129,6 +128,6 @@ public List getByRegionalBloc(String regionalBloc) { @SuppressWarnings("unchecked") private void initialize() { - countries = (List) CountryServiceHelper.loadJson("countriesV2.json", Country.class); + countries = (List) super.loadJson("countriesV2.json", Country.class); } }