KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > util > LocaleUtils


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.util;
56
57 import java.util.HashMap JavaDoc;
58 import java.util.Locale JavaDoc;
59
60 /**
61  * utility class for caching locale objects
62  *
63  * @author J R Briggs
64  */

65 public final class LocaleUtils implements Constants {
66   private static HashMap JavaDoc locales = new HashMap JavaDoc();
67
68   private static final Locale JavaDoc get(String JavaDoc lang, String JavaDoc cty, String JavaDoc var) {
69     Locale JavaDoc loc = new Locale JavaDoc((lang == null ? EMPTY : lang), (cty == null ? EMPTY : cty), (var == null ? EMPTY : var));
70
71     synchronized (locales) {
72       String JavaDoc key = getKey(lang, cty, var);
73       if (!locales.containsKey(key)) {
74         locales.put(key, loc);
75       }
76       else {
77         loc = (Locale JavaDoc)locales.get(key);
78       }
79     }
80     
81     return loc;
82   }
83   
84   private static final String JavaDoc getKey(String JavaDoc lang, String JavaDoc cty, String JavaDoc var) {
85     lang = (lang == null ? lang = EMPTY : lang);
86     cty = (cty == null ? cty = EMPTY : cty);
87     var = (var == null ? var = EMPTY : var);
88     
89     return lang + UNDERSCORE + cty + UNDERSCORE + var;
90   }
91   
92  /**
93   * get a locale using the specified language, country and variant.
94   * Note: if this is not found in the cache, it will be created
95   */

96   public static final Locale JavaDoc getLocale(String JavaDoc lang, String JavaDoc cty, String JavaDoc var) {
97     String JavaDoc key = getKey(lang, cty, var);
98     if (locales.containsKey(key)) {
99       return (Locale JavaDoc)locales.get(key);
100     }
101     else {
102       return get(lang, cty, var);
103     }
104   }
105   
106   public static final Locale JavaDoc getLocale(String JavaDoc fullkey) {
107     if (locales.containsKey(fullkey)) {
108       return (Locale JavaDoc)locales.get(fullkey);
109     }
110     else {
111       String JavaDoc[] s = fullkey.split(UNDERSCORE, 3);
112       if (s.length == 3) {
113         return get(s[0], s[1], s[2]);
114       }
115       else if (s.length == 2) {
116         return get(s[0], s[1], null);
117       }
118       else {
119         return get(s[0], null, null);
120       }
121     }
122   }
123   
124 }
Popular Tags