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 11 package ohos.global.icu.impl.locale; 12 13 14 /** 15 * @hide exposed on OHOS 16 */ 17 public final class BaseLocale { 18 19 private static final boolean JDKIMPL = false; 20 21 public static final String SEP = "_"; 22 23 private static final Cache CACHE = new Cache(); 24 public static final BaseLocale ROOT = BaseLocale.getInstance("", "", "", ""); 25 26 private String _language = ""; 27 private String _script = ""; 28 private String _region = ""; 29 private String _variant = ""; 30 31 private transient volatile int _hash = 0; 32 BaseLocale(String language, String script, String region, String variant)33 private BaseLocale(String language, String script, String region, String variant) { 34 if (language != null) { 35 _language = AsciiUtil.toLowerString(language).intern(); 36 } 37 if (script != null) { 38 _script = AsciiUtil.toTitleString(script).intern(); 39 } 40 if (region != null) { 41 _region = AsciiUtil.toUpperString(region).intern(); 42 } 43 if (variant != null) { 44 if (JDKIMPL) { 45 // preserve upper/lower cases 46 _variant = variant.intern(); 47 } else { 48 _variant = AsciiUtil.toUpperString(variant).intern(); 49 } 50 } 51 } 52 getInstance(String language, String script, String region, String variant)53 public static BaseLocale getInstance(String language, String script, String region, String variant) { 54 if (JDKIMPL) { 55 // JDK uses deprecated ISO639.1 language codes for he, yi and id 56 if (AsciiUtil.caseIgnoreMatch(language, "he")) { 57 language = "iw"; 58 } else if (AsciiUtil.caseIgnoreMatch(language, "yi")) { 59 language = "ji"; 60 } else if (AsciiUtil.caseIgnoreMatch(language, "id")) { 61 language = "in"; 62 } 63 } 64 Key key = new Key(language, script, region, variant); 65 BaseLocale baseLocale = CACHE.get(key); 66 return baseLocale; 67 } 68 getLanguage()69 public String getLanguage() { 70 return _language; 71 } 72 getScript()73 public String getScript() { 74 return _script; 75 } 76 getRegion()77 public String getRegion() { 78 return _region; 79 } 80 getVariant()81 public String getVariant() { 82 return _variant; 83 } 84 85 @Override equals(Object obj)86 public boolean equals(Object obj) { 87 if (this == obj) { 88 return true; 89 } 90 if (!(obj instanceof BaseLocale)) { 91 return false; 92 } 93 BaseLocale other = (BaseLocale)obj; 94 return hashCode() == other.hashCode() 95 && _language.equals(other._language) 96 && _script.equals(other._script) 97 && _region.equals(other._region) 98 && _variant.equals(other._variant); 99 } 100 101 @Override toString()102 public String toString() { 103 StringBuilder buf = new StringBuilder(); 104 if (_language.length() > 0) { 105 buf.append("language="); 106 buf.append(_language); 107 } 108 if (_script.length() > 0) { 109 if (buf.length() > 0) { 110 buf.append(", "); 111 } 112 buf.append("script="); 113 buf.append(_script); 114 } 115 if (_region.length() > 0) { 116 if (buf.length() > 0) { 117 buf.append(", "); 118 } 119 buf.append("region="); 120 buf.append(_region); 121 } 122 if (_variant.length() > 0) { 123 if (buf.length() > 0) { 124 buf.append(", "); 125 } 126 buf.append("variant="); 127 buf.append(_variant); 128 } 129 return buf.toString(); 130 } 131 132 @Override hashCode()133 public int hashCode() { 134 int h = _hash; 135 if (h == 0) { 136 // Generating a hash value from language, script, region and variant 137 for (int i = 0; i < _language.length(); i++) { 138 h = 31*h + _language.charAt(i); 139 } 140 for (int i = 0; i < _script.length(); i++) { 141 h = 31*h + _script.charAt(i); 142 } 143 for (int i = 0; i < _region.length(); i++) { 144 h = 31*h + _region.charAt(i); 145 } 146 for (int i = 0; i < _variant.length(); i++) { 147 h = 31*h + _variant.charAt(i); 148 } 149 _hash = h; 150 } 151 return h; 152 } 153 154 private static class Key implements Comparable<Key> { 155 private String _lang = ""; 156 private String _scrt = ""; 157 private String _regn = ""; 158 private String _vart = ""; 159 160 private volatile int _hash; // Default to 0 161 Key(String language, String script, String region, String variant)162 public Key(String language, String script, String region, String variant) { 163 if (language != null) { 164 _lang = language; 165 } 166 if (script != null) { 167 _scrt = script; 168 } 169 if (region != null) { 170 _regn = region; 171 } 172 if (variant != null) { 173 _vart = variant; 174 } 175 } 176 177 @Override equals(Object obj)178 public boolean equals(Object obj) { 179 if (JDKIMPL) { 180 return (this == obj) || 181 (obj instanceof Key) 182 && AsciiUtil.caseIgnoreMatch(((Key)obj)._lang, this._lang) 183 && AsciiUtil.caseIgnoreMatch(((Key)obj)._scrt, this._scrt) 184 && AsciiUtil.caseIgnoreMatch(((Key)obj)._regn, this._regn) 185 && ((Key)obj)._vart.equals(_vart); // variant is case sensitive in JDK! 186 } 187 return (this == obj) || 188 (obj instanceof Key) 189 && AsciiUtil.caseIgnoreMatch(((Key)obj)._lang, this._lang) 190 && AsciiUtil.caseIgnoreMatch(((Key)obj)._scrt, this._scrt) 191 && AsciiUtil.caseIgnoreMatch(((Key)obj)._regn, this._regn) 192 && AsciiUtil.caseIgnoreMatch(((Key)obj)._vart, this._vart); 193 } 194 195 @Override compareTo(Key other)196 public int compareTo(Key other) { 197 int res = AsciiUtil.caseIgnoreCompare(this._lang, other._lang); 198 if (res == 0) { 199 res = AsciiUtil.caseIgnoreCompare(this._scrt, other._scrt); 200 if (res == 0) { 201 res = AsciiUtil.caseIgnoreCompare(this._regn, other._regn); 202 if (res == 0) { 203 if (JDKIMPL) { 204 res = this._vart.compareTo(other._vart); 205 } else { 206 res = AsciiUtil.caseIgnoreCompare(this._vart, other._vart); 207 } 208 } 209 } 210 } 211 return res; 212 } 213 214 @Override hashCode()215 public int hashCode() { 216 int h = _hash; 217 if (h == 0) { 218 // Generating a hash value from language, script, region and variant 219 for (int i = 0; i < _lang.length(); i++) { 220 h = 31*h + AsciiUtil.toLower(_lang.charAt(i)); 221 } 222 for (int i = 0; i < _scrt.length(); i++) { 223 h = 31*h + AsciiUtil.toLower(_scrt.charAt(i)); 224 } 225 for (int i = 0; i < _regn.length(); i++) { 226 h = 31*h + AsciiUtil.toLower(_regn.charAt(i)); 227 } 228 for (int i = 0; i < _vart.length(); i++) { 229 if (JDKIMPL) { 230 h = 31*h + _vart.charAt(i); 231 } else { 232 h = 31*h + AsciiUtil.toLower(_vart.charAt(i)); 233 } 234 } 235 _hash = h; 236 } 237 return h; 238 } 239 normalize(Key key)240 public static Key normalize(Key key) { 241 String lang = AsciiUtil.toLowerString(key._lang).intern(); 242 String scrt = AsciiUtil.toTitleString(key._scrt).intern(); 243 String regn = AsciiUtil.toUpperString(key._regn).intern(); 244 String vart; 245 if (JDKIMPL) { 246 // preserve upper/lower cases 247 vart = key._vart.intern(); 248 } else { 249 vart = AsciiUtil.toUpperString(key._vart).intern(); 250 } 251 return new Key(lang, scrt, regn, vart); 252 } 253 } 254 255 private static class Cache extends LocaleObjectCache<Key, BaseLocale> { 256 Cache()257 public Cache() { 258 } 259 260 @Override normalizeKey(Key key)261 protected Key normalizeKey(Key key) { 262 return Key.normalize(key); 263 } 264 265 @Override createObject(Key key)266 protected BaseLocale createObject(Key key) { 267 return new BaseLocale(key._lang, key._scrt, key._regn, key._vart); 268 } 269 270 } 271 } 272