1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2009-2010, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package ohos.global.icu.impl.locale; 11 12 import java.util.Collections; 13 import java.util.Map; 14 import java.util.Map.Entry; 15 import java.util.Set; 16 import java.util.SortedMap; 17 import java.util.TreeMap; 18 import java.util.TreeSet; 19 20 import ohos.global.icu.impl.locale.InternalLocaleBuilder.CaseInsensitiveChar; 21 import ohos.global.icu.impl.locale.InternalLocaleBuilder.CaseInsensitiveString; 22 23 24 /** 25 * @hide exposed on OHOS 26 */ 27 public class LocaleExtensions { 28 29 private SortedMap<Character, Extension> _map; 30 private String _id; 31 32 private static final SortedMap<Character, Extension> EMPTY_MAP = 33 Collections.unmodifiableSortedMap(new TreeMap<Character, Extension>()); 34 35 public static final LocaleExtensions EMPTY_EXTENSIONS; 36 public static final LocaleExtensions CALENDAR_JAPANESE; 37 public static final LocaleExtensions NUMBER_THAI; 38 39 static { 40 EMPTY_EXTENSIONS = new LocaleExtensions(); 41 EMPTY_EXTENSIONS._id = ""; 42 EMPTY_EXTENSIONS._map = EMPTY_MAP; 43 44 CALENDAR_JAPANESE = new LocaleExtensions(); 45 CALENDAR_JAPANESE._id = "u-ca-japanese"; 46 CALENDAR_JAPANESE._map = new TreeMap<Character, Extension>(); Character.valueOf(UnicodeLocaleExtension.SINGLETON)47 CALENDAR_JAPANESE._map.put(Character.valueOf(UnicodeLocaleExtension.SINGLETON), UnicodeLocaleExtension.CA_JAPANESE); 48 49 NUMBER_THAI = new LocaleExtensions(); 50 NUMBER_THAI._id = "u-nu-thai"; 51 NUMBER_THAI._map = new TreeMap<Character, Extension>(); Character.valueOf(UnicodeLocaleExtension.SINGLETON)52 NUMBER_THAI._map.put(Character.valueOf(UnicodeLocaleExtension.SINGLETON), UnicodeLocaleExtension.NU_THAI); 53 } 54 LocaleExtensions()55 private LocaleExtensions() { 56 } 57 58 /* 59 * Package local constructor, only used by InternalLocaleBuilder. 60 */ LocaleExtensions(Map<CaseInsensitiveChar, String> extensions, Set<CaseInsensitiveString> uattributes, Map<CaseInsensitiveString, String> ukeywords)61 LocaleExtensions(Map<CaseInsensitiveChar, String> extensions, 62 Set<CaseInsensitiveString> uattributes, Map<CaseInsensitiveString, String> ukeywords) { 63 boolean hasExtension = (extensions != null && extensions.size() > 0); 64 boolean hasUAttributes = (uattributes != null && uattributes.size() > 0); 65 boolean hasUKeywords = (ukeywords != null && ukeywords.size() > 0); 66 67 if (!hasExtension && !hasUAttributes && !hasUKeywords) { 68 _map = EMPTY_MAP; 69 _id = ""; 70 return; 71 } 72 73 // Build extension map 74 _map = new TreeMap<Character, Extension>(); 75 if (hasExtension) { 76 for (Entry<CaseInsensitiveChar, String> ext : extensions.entrySet()) { 77 char key = AsciiUtil.toLower(ext.getKey().value()); 78 String value = ext.getValue(); 79 80 if (LanguageTag.isPrivateusePrefixChar(key)) { 81 // we need to exclude special variant in privuateuse, e.g. "x-abc-lvariant-DEF" 82 value = InternalLocaleBuilder.removePrivateuseVariant(value); 83 if (value == null) { 84 continue; 85 } 86 } 87 88 Extension e = new Extension(key, AsciiUtil.toLowerString(value)); 89 _map.put(Character.valueOf(key), e); 90 } 91 } 92 93 if (hasUAttributes || hasUKeywords) { 94 TreeSet<String> uaset = null; 95 TreeMap<String, String> ukmap = null; 96 97 if (hasUAttributes) { 98 uaset = new TreeSet<String>(); 99 for (CaseInsensitiveString cis : uattributes) { 100 uaset.add(AsciiUtil.toLowerString(cis.value())); 101 } 102 } 103 104 if (hasUKeywords) { 105 ukmap = new TreeMap<String, String>(); 106 for (Entry<CaseInsensitiveString, String> kwd : ukeywords.entrySet()) { 107 String key = AsciiUtil.toLowerString(kwd.getKey().value()); 108 String type = AsciiUtil.toLowerString(kwd.getValue()); 109 ukmap.put(key, type); 110 } 111 } 112 113 UnicodeLocaleExtension ule = new UnicodeLocaleExtension(uaset, ukmap); 114 _map.put(Character.valueOf(UnicodeLocaleExtension.SINGLETON), ule); 115 } 116 117 if (_map.size() == 0) { 118 // this could happen when only privuateuse with special variant 119 _map = EMPTY_MAP; 120 _id = ""; 121 } else { 122 _id = toID(_map); 123 } 124 } 125 getKeys()126 public Set<Character> getKeys() { 127 return Collections.unmodifiableSet(_map.keySet()); 128 } 129 getExtension(Character key)130 public Extension getExtension(Character key) { 131 return _map.get(Character.valueOf(AsciiUtil.toLower(key.charValue()))); 132 } 133 getExtensionValue(Character key)134 public String getExtensionValue(Character key) { 135 Extension ext = _map.get(Character.valueOf(AsciiUtil.toLower(key.charValue()))); 136 if (ext == null) { 137 return null; 138 } 139 return ext.getValue(); 140 } 141 getUnicodeLocaleAttributes()142 public Set<String> getUnicodeLocaleAttributes() { 143 Extension ext = _map.get(Character.valueOf(UnicodeLocaleExtension.SINGLETON)); 144 if (ext == null) { 145 return Collections.emptySet(); 146 } 147 assert (ext instanceof UnicodeLocaleExtension); 148 return ((UnicodeLocaleExtension)ext).getUnicodeLocaleAttributes(); 149 } 150 getUnicodeLocaleKeys()151 public Set<String> getUnicodeLocaleKeys() { 152 Extension ext = _map.get(Character.valueOf(UnicodeLocaleExtension.SINGLETON)); 153 if (ext == null) { 154 return Collections.emptySet(); 155 } 156 assert (ext instanceof UnicodeLocaleExtension); 157 return ((UnicodeLocaleExtension)ext).getUnicodeLocaleKeys(); 158 } 159 getUnicodeLocaleType(String unicodeLocaleKey)160 public String getUnicodeLocaleType(String unicodeLocaleKey) { 161 Extension ext = _map.get(Character.valueOf(UnicodeLocaleExtension.SINGLETON)); 162 if (ext == null) { 163 return null; 164 } 165 assert (ext instanceof UnicodeLocaleExtension); 166 return ((UnicodeLocaleExtension)ext).getUnicodeLocaleType(AsciiUtil.toLowerString(unicodeLocaleKey)); 167 } 168 isEmpty()169 public boolean isEmpty() { 170 return _map.isEmpty(); 171 } 172 isValidKey(char c)173 public static boolean isValidKey(char c) { 174 return LanguageTag.isExtensionSingletonChar(c) || LanguageTag.isPrivateusePrefixChar(c); 175 } 176 isValidUnicodeLocaleKey(String ukey)177 public static boolean isValidUnicodeLocaleKey(String ukey) { 178 return UnicodeLocaleExtension.isKey(ukey); 179 } 180 toID(SortedMap<Character, Extension> map)181 private static String toID(SortedMap<Character, Extension> map) { 182 StringBuilder buf = new StringBuilder(); 183 Extension privuse = null; 184 for (Entry<Character, Extension> entry : map.entrySet()) { 185 char singleton = entry.getKey().charValue(); 186 Extension extension = entry.getValue(); 187 if (LanguageTag.isPrivateusePrefixChar(singleton)) { 188 privuse = extension; 189 } else { 190 if (buf.length() > 0) { 191 buf.append(LanguageTag.SEP); 192 } 193 buf.append(extension); 194 } 195 } 196 if (privuse != null) { 197 if (buf.length() > 0) { 198 buf.append(LanguageTag.SEP); 199 } 200 buf.append(privuse); 201 } 202 return buf.toString(); 203 } 204 205 206 @Override toString()207 public String toString() { 208 return _id; 209 } 210 getID()211 public String getID() { 212 return _id; 213 } 214 215 @Override hashCode()216 public int hashCode() { 217 return _id.hashCode(); 218 } 219 220 @Override equals(Object other)221 public boolean equals(Object other) { 222 if (this == other) { 223 return true; 224 } 225 if (!(other instanceof LocaleExtensions)) { 226 return false; 227 } 228 return this._id.equals(((LocaleExtensions)other)._id); 229 } 230 } 231