1 
2 /*
3  * Copyright (c) 2010, 2014, 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 import java.util.StringJoiner;
42 
43 public class UnicodeLocaleExtension extends Extension {
44     public static final char SINGLETON = 'u';
45 
46     private final Set<String> attributes;
47     private final Map<String, String> keywords;
48 
49     public static final UnicodeLocaleExtension CA_JAPANESE
50         = new UnicodeLocaleExtension("ca", "japanese");
51     public static final UnicodeLocaleExtension NU_THAI
52         = new UnicodeLocaleExtension("nu", "thai");
53 
UnicodeLocaleExtension(String key, String value)54     private UnicodeLocaleExtension(String key, String value) {
55         super(SINGLETON, key + "-" + value);
56         attributes = Collections.emptySet();
57         keywords = Collections.singletonMap(key, value);
58     }
59 
UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords)60     UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) {
61         super(SINGLETON);
62         if (attributes != null) {
63             this.attributes = attributes;
64         } else {
65             this.attributes = Collections.emptySet();
66         }
67         if (keywords != null) {
68             this.keywords = keywords;
69         } else {
70             this.keywords = Collections.emptyMap();
71         }
72 
73         if (!this.attributes.isEmpty() || !this.keywords.isEmpty()) {
74             StringJoiner sj = new StringJoiner(LanguageTag.SEP);
75             for (String attribute : this.attributes) {
76                 sj.add(attribute);
77             }
78             for (Entry<String, String> keyword : this.keywords.entrySet()) {
79                 String key = keyword.getKey();
80                 String value = keyword.getValue();
81 
82                 sj.add(key);
83                 if (!value.isEmpty()) {
84                     sj.add(value);
85                 }
86             }
87             setValue(sj.toString());
88         }
89     }
90 
getUnicodeLocaleAttributes()91     public Set<String> getUnicodeLocaleAttributes() {
92         if (attributes == Collections.EMPTY_SET) {
93             return attributes;
94         }
95         return Collections.unmodifiableSet(attributes);
96     }
97 
getUnicodeLocaleKeys()98     public Set<String> getUnicodeLocaleKeys() {
99         if (keywords == Collections.EMPTY_MAP) {
100             return Collections.emptySet();
101         }
102         return Collections.unmodifiableSet(keywords.keySet());
103     }
104 
getUnicodeLocaleType(String unicodeLocaleKey)105     public String getUnicodeLocaleType(String unicodeLocaleKey) {
106         return keywords.get(unicodeLocaleKey);
107     }
108 
isSingletonChar(char c)109     public static boolean isSingletonChar(char c) {
110         return (SINGLETON == LocaleUtils.toLower(c));
111     }
112 
isAttribute(String s)113     public static boolean isAttribute(String s) {
114         // 3*8alphanum
115         int len = s.length();
116         return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);
117     }
118 
isKey(String s)119     public static boolean isKey(String s) {
120         // 2alphanum
121         return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);
122     }
123 
isTypeSubtag(String s)124     public static boolean isTypeSubtag(String s) {
125         // 3*8alphanum
126         int len = s.length();
127         return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);
128     }
129 }
130