1 | /* |
2 | * Copyright 2001-2005 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 | |
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, String nameKey) { |
49 | if (locale == null || id == null || nameKey == null) { |
50 | return null; |
51 | } |
52 | |
53 | HashMap byIdCache = (HashMap)iByLocaleCache.get(locale); |
54 | if (byIdCache == null) { |
55 | iByLocaleCache.put(locale, byIdCache = createCache()); |
56 | } |
57 | |
58 | HashMap byNameKeyCache = (HashMap)byIdCache.get(id); |
59 | if (byNameKeyCache == null) { |
60 | byIdCache.put(id, byNameKeyCache = createCache()); |
61 | String[][] zoneStrings = new DateFormatSymbols(locale).getZoneStrings(); |
62 | for (int i=0; i<zoneStrings.length; i++) { |
63 | String[] set = zoneStrings[i]; |
64 | if (set != null && set.length == 5 && id.equals(set[0])) { |
65 | byNameKeyCache.put(set[2], new String[] {set[2], set[1]}); |
66 | // need to handle case where summer and winter have the same |
67 | // abbreviation, such as EST in Australia [1716305] |
68 | // we handle this by appending "-Summer", cf ZoneInfoCompiler |
69 | if (set[2].equals(set[4])) { |
70 | byNameKeyCache.put(set[4] + "-Summer", new String[] {set[4], set[3]}); |
71 | } else { |
72 | byNameKeyCache.put(set[4], new String[] {set[4], set[3]}); |
73 | } |
74 | break; |
75 | } |
76 | } |
77 | } |
78 | |
79 | return (String[])byNameKeyCache.get(nameKey); |
80 | } |
81 | |
82 | private HashMap createCache() { |
83 | return new HashMap(7); |
84 | } |
85 | } |