Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
fayderflorez committed May 17, 2017
2 parents 4efeee2 + 6519f5a commit 0d30235
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 129 deletions.
76 changes: 0 additions & 76 deletions src/main/java/fayder/restcountries/domain/BaseCountry.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,151 +46,75 @@ public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<String> getTopLevelDomain() {
return topLevelDomain;
}

public void setTopLevelDomain(List<String> 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<String> getCallingCodes() {
return callingCodes;
}

public void setCallingCodes(List<String> callingCodes) {
this.callingCodes = callingCodes;
}

public String getCapital() {
return capital;
}

public void setCapital(String capital) {
this.capital = capital;
}

public List<String> getAltSpellings() {
return altSpellings;
}

public void setAltSpellings(List<String> 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<Double> getLatlng() {
return latlng;
}

public void setLatlng(List<Double> 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<String> getTimezones() {
return timezones;
}

public void setTimezones(List<String> timezones) {
this.timezones = timezones;
}

public List<String> getBorders() {
return borders;
}

public void setBorders(List<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends BaseCountry> T getByAlpha(String alpha, List<T> countries) {
protected <T extends BaseCountry> T getByAlpha(String alpha, List<T> countries) {
int alphaLength = alpha.length();
for (T country : countries) {
if (alphaLength == 2) {
Expand All @@ -36,7 +36,7 @@ public static <T extends BaseCountry> T getByAlpha(String alpha, List<T> countri
return null;
}

public static List<? extends BaseCountry> getByCodeList(String codeList, List<? extends BaseCountry> countries) {
protected List<? extends BaseCountry> getByCodeList(String codeList, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
if(codeList == null) return result;

Expand All @@ -49,15 +49,15 @@ public static List<? extends BaseCountry> getByCodeList(String codeList, List<?
return result;
}

public static List<? extends BaseCountry> getByName(String name, boolean fullText, List<? extends BaseCountry> countries) {
protected List<? extends BaseCountry> getByName(String name, boolean fullText, List<? extends BaseCountry> countries) {
if(fullText) {
return fulltextSearch(name, countries);
} else {
return substringSearch(name, countries);
}
}

public static List<? extends BaseCountry> getByCallingCode(String callingCode, List<? extends BaseCountry> countries) {
protected List<? extends BaseCountry> getByCallingCode(String callingCode, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for(BaseCountry country : countries) {
for(String c : country.getCallingCodes()) {
Expand All @@ -68,7 +68,7 @@ public static List<? extends BaseCountry> getByCallingCode(String callingCode, L
return result;
}

public static List<? extends BaseCountry> getByCapital(String capital, List<? extends BaseCountry> countries) {
protected List<? extends BaseCountry> getByCapital(String capital, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for(BaseCountry country : countries) {
if(normalize(country.getCapital().toLowerCase()).contains(normalize(capital.toLowerCase()))) {
Expand All @@ -78,7 +78,7 @@ public static List<? extends BaseCountry> getByCapital(String capital, List<? ex
return result;
}

public static List<? extends BaseCountry> getByRegion(String region, List<? extends BaseCountry> countries) {
protected List<? extends BaseCountry> getByRegion(String region, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for(BaseCountry country : countries) {
if(country.getRegion().toLowerCase().equals(region.toLowerCase())) {
Expand All @@ -88,7 +88,7 @@ public static List<? extends BaseCountry> getByRegion(String region, List<? exte
return result;
}

public static List<? extends BaseCountry> getBySubregion(String subregion, List<? extends BaseCountry> countries) {
protected List<? extends BaseCountry> getBySubregion(String subregion, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for(BaseCountry country : countries) {
if(country.getSubregion().toLowerCase().equals(subregion.toLowerCase())) {
Expand All @@ -98,26 +98,7 @@ public static List<? extends BaseCountry> getBySubregion(String subregion, List<
return result;
}

public static List<? extends BaseCountry> loadJson(String filename, Class<? extends BaseCountry> clazz) {
LOG.debug("Loading JSON " + filename);
List<BaseCountry> 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<? extends BaseCountry> fulltextSearch(String name, List<? extends BaseCountry> countries) {
private List<? extends BaseCountry> fulltextSearch(String name, List<? extends BaseCountry> countries) {
// Using 2 different 'for' loops to give priority to 'name' matches over alternative spellings
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
Expand All @@ -136,7 +117,7 @@ private static List<? extends BaseCountry> fulltextSearch(String name, List<? ex
return result;
}

private static List<? extends BaseCountry> substringSearch(String name, List<? extends BaseCountry> countries) {
private List<? extends BaseCountry> substringSearch(String name, List<? extends BaseCountry> countries) {
// Using 2 different 'for' loops to give priority to 'name' matches over alternative spellings
List<BaseCountry> result = new ArrayList<>();
for(BaseCountry country : countries) {
Expand All @@ -155,8 +136,27 @@ private static List<? extends BaseCountry> substringSearch(String name, List<? e
return result;
}

public static String normalize(String string) {
protected String normalize(String string) {
return Normalizer.normalize(string, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}

protected List<? extends BaseCountry> loadJson(String filename, Class<? extends BaseCountry> clazz) {
LOG.debug("Loading JSON " + filename);
List<BaseCountry> 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;
}
}
20 changes: 10 additions & 10 deletions src/main/java/fayder/restcountries/v1/rest/CountryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -33,38 +33,38 @@ public List<Country> getAll() {
}

public Country getByAlpha(String alpha) {
return CountryServiceHelper.getByAlpha(alpha, countries);
return super.getByAlpha(alpha, countries);
}

@SuppressWarnings("unchecked")
public List<Country> getByCodeList(String codeList) {
return (List<Country>) CountryServiceHelper.getByCodeList(codeList, countries);
return (List<Country>) super.getByCodeList(codeList, countries);
}

@SuppressWarnings("unchecked")
public List<Country> getByName(String name, boolean isFullText) {
return (List<Country>) CountryServiceHelper.getByName(name, isFullText, countries);
return (List<Country>) super.getByName(name, isFullText, countries);

}

@SuppressWarnings("unchecked")
public List<Country> getByCallingCode(String callingcode) {
return (List<Country>) CountryServiceHelper.getByCallingCode(callingcode, countries);
return (List<Country>) super.getByCallingCode(callingcode, countries);
}

@SuppressWarnings("unchecked")
public List<Country> getByCapital(String capital) {
return (List<Country>) CountryServiceHelper.getByCapital(capital, countries);
return (List<Country>) super.getByCapital(capital, countries);
}

@SuppressWarnings("unchecked")
public List<Country> getByRegion(String region) {
return (List<Country>) CountryServiceHelper.getByRegion(region, countries);
return (List<Country>) super.getByRegion(region, countries);
}

@SuppressWarnings("unchecked")
public List<Country> getBySubregion(String subregion) {
return (List<Country>) CountryServiceHelper.getBySubregion(subregion, countries);
return (List<Country>) super.getBySubregion(subregion, countries);
}

public List<Country> getByCurrency(String currency) {
Expand Down Expand Up @@ -93,6 +93,6 @@ public List<Country> getByLanguage(String language) {

@SuppressWarnings("unchecked")
private void initialize() {
countries = (List<Country>) CountryServiceHelper.loadJson("countriesV1.json", Country.class);
countries = (List<Country>) super.loadJson("countriesV1.json", Country.class);
}
}
Loading

0 comments on commit 0d30235

Please sign in to comment.