View Javadoc

1   package org.appfuse.webapp.services.impl;
2   
3   import org.apache.tapestry5.ioc.services.ThreadLocale;
4   import org.appfuse.webapp.services.CountryService;
5   import org.slf4j.Logger;
6   
7   import java.text.Collator;
8   import java.util.*;
9   
10  /**
11   * Implementation of CountryService
12   *
13   * @author Serge Eby
14   */
15  public class CountryServiceImpl implements CountryService {
16      private final Logger logger;
17      private final ThreadLocale threadLocale;
18      private final Map<String, String> sortedCountries;
19  
20      public CountryServiceImpl(Logger logger, ThreadLocale threadLocale) {
21          this.logger = logger;
22          this.threadLocale = threadLocale;
23  
24          Map<String, String> countries = initialize();
25          sortedCountries = new TreeMap<String, String>
26                  (new CountryComparator(countries, this.threadLocale.getLocale()));
27          sortedCountries.putAll(countries);
28      }
29  
30      public Map<String, String> getAvailableCountries() {
31          return sortedCountries;
32      }
33  
34      private Map<String, String> initialize() {
35          Map<String, String> countries = new HashMap<String, String>();
36          Locale currentLocale = threadLocale.getLocale();
37          final String EMPTY = "";
38          Locale[] availableLocales = Locale.getAvailableLocales();
39  
40          for (Locale locale : availableLocales) {
41              String name = locale.getDisplayCountry(currentLocale);
42              String iso = locale.getCountry();
43              if (!EMPTY.equals(name) && !EMPTY.equals(iso)) {
44                  countries.put(iso, name);
45              }
46          }
47          logger.debug("Number of countries added: " + countries.size());
48  
49          return countries;
50  
51      }
52  
53      /**
54       * Class to compare LabelValues using their labels with locale-sensitive
55       * behaviour.
56       */
57      private class CountryComparator implements Comparator<String> {
58          private Collator c;
59          private Map<String, String> unsortedMap;
60  
61          /**
62           * Creates a new CountryComparator object.
63           *
64           * @param map    of country and locale
65           * @param locale The Locale used for localized String comparison.
66           */
67          public CountryComparator(Map<String, String> map, Locale locale) {
68              unsortedMap = map;
69              c = Collator.getInstance(locale);
70          }
71  
72          /**
73           * Compares the localized labels of two LabelValues.
74           *
75           * @param rhs The first String to compare.
76           * @param lhs The second String to compare.
77           * @return The value returned by comparing the localized labels.
78           */
79  
80          public final int compare(String lhs, String rhs) {
81              String lvalue = unsortedMap.get(lhs);
82              String rvalue = unsortedMap.get(rhs);
83              return c.compare(lvalue, rvalue);
84          }
85      }
86  }