01: /*
02: * Copyright 2001-2005 Stephen Colebourne
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
22: /**
23: * The default name provider acquires localized names from
24: * {@link DateFormatSymbols java.text.DateFormatSymbols}.
25: * <p>
26: * DefaultNameProvider is thread-safe and immutable.
27: *
28: * @author Brian S O'Neill
29: * @since 1.0
30: */
31: public class DefaultNameProvider implements NameProvider {
32: // locale -> (id -> (nameKey -> [shortName, name]))
33: private HashMap iByLocaleCache = createCache();
34:
35: public DefaultNameProvider() {
36: }
37:
38: public String getShortName(Locale locale, String id, String nameKey) {
39: String[] nameSet = getNameSet(locale, id, nameKey);
40: return nameSet == null ? null : nameSet[0];
41: }
42:
43: public String getName(Locale locale, String id, String nameKey) {
44: String[] nameSet = getNameSet(locale, id, nameKey);
45: return nameSet == null ? null : nameSet[1];
46: }
47:
48: private synchronized String[] getNameSet(Locale locale, String id,
49: String nameKey) {
50: if (locale == null || id == null || nameKey == null) {
51: return null;
52: }
53:
54: HashMap byIdCache = (HashMap) iByLocaleCache.get(locale);
55: if (byIdCache == null) {
56: iByLocaleCache.put(locale, byIdCache = createCache());
57: }
58:
59: HashMap byNameKeyCache = (HashMap) byIdCache.get(id);
60: if (byNameKeyCache == null) {
61: byIdCache.put(id, byNameKeyCache = createCache());
62: String[][] zoneStrings = new DateFormatSymbols(locale)
63: .getZoneStrings();
64: for (int i = 0; i < zoneStrings.length; i++) {
65: String[] set = zoneStrings[i];
66: if (set != null && set.length == 5 && id.equals(set[0])) {
67: byNameKeyCache.put(set[2], new String[] { set[2],
68: set[1] });
69: // need to handle case where summer and winter have the same
70: // abbreviation, such as EST in Australia [1716305]
71: // we handle this by appending "-Summer", cf ZoneInfoCompiler
72: if (set[2].equals(set[4])) {
73: byNameKeyCache.put(set[4] + "-Summer",
74: new String[] { set[4], set[3] });
75: } else {
76: byNameKeyCache.put(set[4], new String[] {
77: set[4], set[3] });
78: }
79: break;
80: }
81: }
82: }
83:
84: return (String[]) byNameKeyCache.get(nameKey);
85: }
86:
87: private HashMap createCache() {
88: return new HashMap(7);
89: }
90: }
|