View Javadoc

1   /*
2    *  Copyright 2001-2011 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * The default name provider acquires localized names from
27   * {@link DateFormatSymbols java.text.DateFormatSymbols}.
28   * <p>
29   * DefaultNameProvider is thread-safe and immutable.
30   *
31   * @author Brian S O'Neill
32   * @since 1.0
33   */
34  @SuppressWarnings("unchecked")
35  public class DefaultNameProvider implements NameProvider {
36      // locale -> (id -> (nameKey -> [shortName, name]))
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                // need to handle case where summer and winter have the same
86                // abbreviation, such as EST in Australia [1716305]
87                // we handle this by appending "-Summer", cf ZoneInfoCompiler
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 }