KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > tz > DefaultNameProvider


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 JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
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 JavaDoc iByLocaleCache = createCache();
34
35     public DefaultNameProvider() {
36     }
37
38     public String JavaDoc getShortName(Locale JavaDoc locale, String JavaDoc id, String JavaDoc nameKey) {
39         String JavaDoc[] nameSet = getNameSet(locale, id, nameKey);
40         return nameSet == null ? null : nameSet[0];
41     }
42     
43     public String JavaDoc getName(Locale JavaDoc locale, String JavaDoc id, String JavaDoc nameKey) {
44         String JavaDoc[] nameSet = getNameSet(locale, id, nameKey);
45         return nameSet == null ? null : nameSet[1];
46     }
47
48     private synchronized String JavaDoc[] getNameSet(Locale JavaDoc locale, String JavaDoc id, String JavaDoc nameKey) {
49         if (locale == null || id == null || nameKey == null) {
50             return null;
51         }
52
53         HashMap JavaDoc byIdCache = (HashMap JavaDoc)iByLocaleCache.get(locale);
54         if (byIdCache == null) {
55             iByLocaleCache.put(locale, byIdCache = createCache());
56         }
57
58         HashMap JavaDoc byNameKeyCache = (HashMap JavaDoc)byIdCache.get(id);
59         if (byNameKeyCache == null) {
60             byIdCache.put(id, byNameKeyCache = createCache());
61             String JavaDoc[][] zoneStrings = new DateFormatSymbols JavaDoc(locale).getZoneStrings();
62             for (int i=0; i<zoneStrings.length; i++) {
63                 String JavaDoc[] set = zoneStrings[i];
64                 if (set != null && set.length == 5 && id.equals(set[0])) {
65                     byNameKeyCache.put(set[2], new String JavaDoc[] {set[2], set[1]});
66                     byNameKeyCache.put(set[4], new String JavaDoc[] {set[4], set[3]});
67                 }
68             }
69         }
70
71         return (String JavaDoc[])byNameKeyCache.get(nameKey);
72     }
73
74     private HashMap JavaDoc createCache() {
75         return new HashMap JavaDoc(7);
76     }
77 }
78
Popular Tags