1 
2 /*
3  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 /*
28  *******************************************************************************
29  * Copyright (C) 2009-2010, International Business Machines Corporation and    *
30  * others. All Rights Reserved.                                                *
31  *******************************************************************************
32  */
33 package sun.util.locale;
34 
35 import java.util.Collections;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import java.util.Set;
39 import java.util.SortedMap;
40 import java.util.SortedSet;
41 
42 public class UnicodeLocaleExtension extends Extension {
43     public static final char SINGLETON = 'u';
44 
45     private final Set<String> attributes;
46     private final Map<String, String> keywords;
47 
48     public static final UnicodeLocaleExtension CA_JAPANESE
49         = new UnicodeLocaleExtension("ca", "japanese");
50     public static final UnicodeLocaleExtension NU_THAI
51         = new UnicodeLocaleExtension("nu", "thai");
52 
UnicodeLocaleExtension(String key, String value)53     private UnicodeLocaleExtension(String key, String value) {
54         super(SINGLETON, key + "-" + value);
55         attributes = Collections.emptySet();
56         keywords = Collections.singletonMap(key, value);
57     }
58 
UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords)59     UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) {
60         super(SINGLETON);
61         if (attributes != null) {
62             this.attributes = attributes;
63         } else {
64             this.attributes = Collections.emptySet();
65         }
66         if (keywords != null) {
67             this.keywords = keywords;
68         } else {
69             this.keywords = Collections.emptyMap();
70         }
71 
72         if (!this.attributes.isEmpty() || !this.keywords.isEmpty()) {
73             StringBuilder sb = new StringBuilder();
74             for (String attribute : this.attributes) {
75                 sb.append(LanguageTag.SEP).append(attribute);
76             }
77             for (Entry<String, String> keyword : this.keywords.entrySet()) {
78                 String key = keyword.getKey();
79                 String value = keyword.getValue();
80 
81                 sb.append(LanguageTag.SEP).append(key);
82                 if (value.length() > 0) {
83                     sb.append(LanguageTag.SEP).append(value);
84                 }
85             }
86             setValue(sb.substring(1));   // skip leading '-'
87         }
88     }
89 
getUnicodeLocaleAttributes()90     public Set<String> getUnicodeLocaleAttributes() {
91         if (attributes == Collections.EMPTY_SET) {
92             return attributes;
93         }
94         return Collections.unmodifiableSet(attributes);
95     }
96 
getUnicodeLocaleKeys()97     public Set<String> getUnicodeLocaleKeys() {
98         if (keywords == Collections.EMPTY_MAP) {
99             return Collections.emptySet();
100         }
101         return Collections.unmodifiableSet(keywords.keySet());
102     }
103 
getUnicodeLocaleType(String unicodeLocaleKey)104     public String getUnicodeLocaleType(String unicodeLocaleKey) {
105         return keywords.get(unicodeLocaleKey);
106     }
107 
isSingletonChar(char c)108     public static boolean isSingletonChar(char c) {
109         return (SINGLETON == LocaleUtils.toLower(c));
110     }
111 
isAttribute(String s)112     public static boolean isAttribute(String s) {
113         // 3*8alphanum
114         int len = s.length();
115         return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);
116     }
117 
isKey(String s)118     public static boolean isKey(String s) {
119         // 2alphanum
120         return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);
121     }
122 
isTypeSubtag(String s)123     public static boolean isTypeSubtag(String s) {
124         // 3*8alphanum
125         int len = s.length();
126         return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);
127     }
128 }
129