01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.datatype.convertor;
18:
19: import org.apache.commons.collections.FastHashMap;
20:
21: import java.util.Locale;
22:
23: /**
24: * Map using Locale objects as keys.
25: *
26: * <p>This map should be filled once using calls to {@link #put(Locale, Object)},
27: * before any calls are made to {@link #get(Locale)}.
28: *
29: * @version $Id: LocaleMap.java 449149 2006-09-23 03:58:05Z crossley $
30: */
31: public class LocaleMap {
32: private FastHashMap map = new FastHashMap();
33:
34: /**
35: * Gets an object based on the given locale. An automatic fallback mechanism is used:
36: * if nothing is found for language-COUNTRY-variant, then language-COUNTRY is searched,
37: * the language, and finally "" (empty string). If nothing is found null is returned.
38: */
39: public Object get(Locale locale) {
40: if (map.size() == 0)
41: return null;
42:
43: String full = getFullKey(locale);
44:
45: if (!map.containsKey(full)) {
46: // check more general variant (lang-COUNTRY and lang), and store result in the map
47: // under the full key, so that next time we have a direct hit
48: String altKey = locale.getLanguage() + '-'
49: + locale.getCountry();
50: Object object = map.get(altKey);
51: if (object != null) {
52: map.put(full, object);
53: return object;
54: }
55:
56: altKey = locale.getLanguage();
57: object = map.get(altKey);
58: if (object != null) {
59: map.put(full, object);
60: return object;
61: }
62:
63: object = map.get("");
64: if (object != null) {
65: map.put(full, object);
66: return object;
67: }
68:
69: map.put(full, null);
70: }
71:
72: return map.get(full);
73: }
74:
75: private final String getFullKey(Locale locale) {
76: return locale.getLanguage() + '-' + locale.getCountry() + '-'
77: + locale.getVariant();
78: }
79:
80: private final String getKey(Locale locale) {
81: boolean hasLanguage = !locale.getLanguage().equals("");
82: boolean hasCountry = !locale.getCountry().equals("");
83: boolean hasVariant = !locale.getVariant().equals("");
84:
85: if (hasLanguage && hasCountry && hasVariant)
86: return locale.getLanguage() + '-' + locale.getCountry()
87: + '-' + locale.getVariant();
88: else if (hasLanguage && hasCountry)
89: return locale.getLanguage() + '-' + locale.getCountry();
90: else if (hasLanguage)
91: return locale.getLanguage();
92: else
93: return "";
94: }
95:
96: public void put(Locale locale, Object object) {
97: map.put(getKey(locale), object);
98: }
99: }
|