KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > convertor > LocaleMap


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.cocoon.woody.datatype.convertor;
17
18 import org.apache.commons.collections.FastHashMap;
19
20 import java.util.Locale JavaDoc;
21
22 /**
23  * Map using Locale objects as keys.
24  *
25  * <p>This map should be filled once using calls to {@link #put(Locale, Object)},
26  * before any calls are made to {@link #get(Locale)}.
27  *
28  * @version CVS $Id: LocaleMap.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  */

30 public class LocaleMap {
31     private FastHashMap map = new FastHashMap();
32
33     /**
34      * Gets an object based on the given locale. An automatic fallback mechanism is used:
35      * if nothing is found for language-COUNTRY-variant, then language-COUNTRY is searched,
36      * the language, and finally "" (empty string). If nothing is found null is returned.
37      */

38     public Object JavaDoc get(Locale JavaDoc locale) {
39         if (map.size() == 0)
40             return null;
41
42         String JavaDoc full = getFullKey(locale);
43
44         if (!map.containsKey(full)) {
45             // check more general variant (lang-COUNTRY and lang), and store result in the map
46
// under the full key, so that next time we have a direct hit
47
String JavaDoc altKey = locale.getLanguage() + '-' + locale.getCountry();
48             Object JavaDoc object = map.get(altKey);
49             if (object != null) {
50                 map.put(full, object);
51                 return object;
52             }
53
54             altKey = locale.getLanguage();
55             object = map.get(altKey);
56             if (object != null) {
57                 map.put(full, object);
58                 return object;
59             }
60
61             object = map.get("");
62             if (object != null) {
63                 map.put(full, object);
64                 return object;
65             }
66
67             map.put(full, null);
68         }
69
70         return map.get(full);
71     }
72
73     private final String JavaDoc getFullKey(Locale JavaDoc locale) {
74         return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant();
75     }
76
77     private final String JavaDoc getKey(Locale JavaDoc locale) {
78         boolean hasLanguage = !locale.getLanguage().equals("");
79         boolean hasCountry = !locale.getCountry().equals("");
80         boolean hasVariant = !locale.getVariant().equals("");
81
82         if (hasLanguage && hasCountry && hasVariant)
83             return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant();
84         else if (hasLanguage && hasCountry)
85             return locale.getLanguage() + '-' + locale.getCountry();
86         else if (hasLanguage)
87             return locale.getLanguage();
88         else
89             return "";
90     }
91
92     public void put(Locale JavaDoc locale, Object JavaDoc object) {
93         map.put(getKey(locale), object);
94     }
95 }
96
Popular Tags