1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.joda.time.tz;
17  
18  import java.text.DateFormatSymbols;
19  import java.util.HashMap;
20  import java.util.Locale;
21  import java.util.Map;
22  
23  import org.joda.time.DateTimeUtils;
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  @SuppressWarnings("unchecked")
35  public class DefaultNameProvider implements NameProvider {
36      
37      private HashMap<Locale, Map<String, Map<String, Object>>> iByLocaleCache = createCache();
38  
39      public DefaultNameProvider() {
40      }
41  
42      public String getShortName(Locale locale, String id, String nameKey) {
43          String[] nameSet = getNameSet(locale, id, nameKey);
44          return nameSet == null ? null : nameSet[0];
45      }
46      
47      public String getName(Locale locale, String id, String nameKey) {
48          String[] nameSet = getNameSet(locale, id, nameKey);
49          return nameSet == null ? null : nameSet[1];
50      }
51  
52      private synchronized String[] getNameSet(Locale locale, String id, String nameKey) {
53          if (locale == null || id == null || nameKey == null) {
54              return null;
55          }
56  
57          Map<String, Map<String, Object>> byIdCache = iByLocaleCache.get(locale);
58          if (byIdCache == null) {
59              iByLocaleCache.put(locale, byIdCache = createCache());
60          }
61  
62          Map<String, Object> byNameKeyCache = byIdCache.get(id);
63          if (byNameKeyCache == null) {
64              byIdCache.put(id, byNameKeyCache = createCache());
65              
66              String[][] zoneStringsEn = DateTimeUtils.getDateFormatSymbols(Locale.ENGLISH).getZoneStrings();
67              String[] setEn = null;
68              for (String[] strings : zoneStringsEn) {
69                if (strings != null && strings.length == 5 && id.equals(strings[0])) {
70                  setEn = strings;
71                  break;
72                }
73              }
74              String[][] zoneStringsLoc = DateTimeUtils.getDateFormatSymbols(locale).getZoneStrings();
75              String[] setLoc = null;
76              for (String[] strings : zoneStringsLoc) {
77                if (strings != null && strings.length == 5 && id.equals(strings[0])) {
78                  setLoc = strings;
79                  break;
80                }
81              }
82              
83              if (setEn != null && setLoc != null) {
84                byNameKeyCache.put(setEn[2], new String[] {setLoc[2], setLoc[1]});
85                
86                
87                
88                if (setEn[2].equals(setEn[4])) {
89                    byNameKeyCache.put(setEn[4] + "-Summer", new String[] {setLoc[4], setLoc[3]});
90                } else {
91                    byNameKeyCache.put(setEn[4], new String[] {setLoc[4], setLoc[3]});
92                }
93              }
94          }
95          return (String[]) byNameKeyCache.get(nameKey);
96      }
97  
98      private HashMap createCache() {
99          return new HashMap(7);
100     }
101 }