KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > countries > DefaultCountryService


1 package org.springframework.samples.countries;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.springframework.beans.factory.InitializingBean;
11
12 /**
13  * @author Jean-Pierre Pawlak
14  * @author Juergen Hoeller
15  */

16 public class DefaultCountryService implements CountryService, InitializingBean {
17
18     private final Map JavaDoc countriesLists = new HashMap JavaDoc();
19
20     private final Map JavaDoc countriesMaps = new HashMap JavaDoc();
21
22
23     public void afterPropertiesSet() {
24         String JavaDoc countries[] = Locale.getISOCountries();
25         String JavaDoc langs[] = {"fr", "en", "de"};
26         for (int langId = 0; langId < langs.length; langId++) {
27             String JavaDoc lang = langs[langId];
28             Locale JavaDoc crtLoc = new Locale JavaDoc(lang, "");
29             List JavaDoc list = new ArrayList JavaDoc();
30             Map JavaDoc map = new HashMap JavaDoc();
31             Country country = null;
32             for (int j = 0; j < countries.length; j++) {
33                 String JavaDoc countryCode = countries[j];
34                 Locale JavaDoc countryLoc = new Locale JavaDoc("en", countryCode);
35                 String JavaDoc name = countryLoc.getDisplayCountry(crtLoc);
36                 if (!name.equals(countryCode)) {
37                     country = new Country(countryCode, name);
38                     list.add(country);
39                     map.put(countryCode, country);
40                 }
41             }
42             this.countriesLists.put(lang, list);
43             this.countriesMaps.put(lang, map);
44         }
45     }
46
47
48     public List JavaDoc getAllCountries(Locale JavaDoc locale) {
49         List JavaDoc countries = (List JavaDoc) this.countriesLists.get(getLanguage(locale));
50         if (countries == null) {
51             countries = (List JavaDoc) this.countriesLists.get(Locale.getDefault().getLanguage());
52         }
53         return countries;
54     }
55
56     public List JavaDoc getFilteredCountries(String JavaDoc name, String JavaDoc code, Locale JavaDoc locale) {
57         List JavaDoc allCountries = getAllCountries(locale);
58         List JavaDoc countries = new ArrayList JavaDoc();
59         Iterator JavaDoc it = allCountries.iterator();
60         while (it.hasNext()) {
61             Country country = (Country) it.next();
62             if ((name == null || country.getName().startsWith(name)) &&
63                     (code == null || country.getCode().startsWith(code))) {
64                 countries.add(country);
65             }
66         }
67         return countries;
68     }
69
70     public Country getCountry(String JavaDoc code, Locale JavaDoc locale) {
71         return (Country) ((Map JavaDoc) this.countriesMaps.get(getLanguage(locale))).get(code);
72     }
73
74     private String JavaDoc getLanguage(Locale JavaDoc locale) {
75         if (locale != null) {
76             return locale.getLanguage();
77         }
78         return Locale.getDefault().getLanguage();
79     }
80
81 }
82
83
Popular Tags