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

Commit

Permalink
implemented singleton to load json db
Browse files Browse the repository at this point in the history
  • Loading branch information
fayderflorez committed Oct 25, 2013
1 parent b35d2c7 commit 95d16db
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/vaeke/restcountries/rest/CountryRest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
public class CountryRest {

private static final Logger LOG = Logger.getLogger(CountryRest.class);
private static List<Country> countries;

@GET
public Object getCountries() {
Expand Down Expand Up @@ -169,17 +170,20 @@ private String removeDiacriticalMarks(String string) {
}

private List<Country> getAll() throws IOException {
if(countries != null) return countries;

LOG.debug("Loading JSON Database");
InputStream is = this.getClass().getClassLoader().getResourceAsStream("countries.json");
Gson gson = new Gson();
JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
List<Country> list = new ArrayList<Country>();
countries = new ArrayList<Country>();
reader.beginArray();
while(reader.hasNext()) {
Country country = gson.fromJson(reader, Country.class);
list.add(country);
countries.add(country);
}
reader.endArray();
reader.close();
return list;
return countries;
}
}

0 comments on commit 95d16db

Please sign in to comment.