2010-09-10 15:29:40 -07:00
|
|
|
/*
|
2018-02-08 18:45:30 +01:00
|
|
|
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
|
2010-09-10 15:29:40 -07:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
* Copyright (C) 2009-2010, International Business Machines Corporation and *
|
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
package sun.util.locale;
|
|
|
|
|
2020-09-24 15:28:10 +00:00
|
|
|
import jdk.internal.misc.CDS;
|
2019-04-03 17:06:35 +02:00
|
|
|
import jdk.internal.vm.annotation.Stable;
|
|
|
|
|
2018-02-08 18:45:30 +01:00
|
|
|
import java.lang.ref.SoftReference;
|
2014-08-27 22:08:19 +04:00
|
|
|
import java.util.StringJoiner;
|
2010-09-10 15:29:40 -07:00
|
|
|
|
|
|
|
public final class BaseLocale {
|
|
|
|
|
2019-04-03 17:06:35 +02:00
|
|
|
public static @Stable BaseLocale[] constantBaseLocales;
|
|
|
|
public static final byte ENGLISH = 0,
|
|
|
|
FRENCH = 1,
|
|
|
|
GERMAN = 2,
|
|
|
|
ITALIAN = 3,
|
|
|
|
JAPANESE = 4,
|
|
|
|
KOREAN = 5,
|
|
|
|
CHINESE = 6,
|
|
|
|
SIMPLIFIED_CHINESE = 7,
|
|
|
|
TRADITIONAL_CHINESE = 8,
|
|
|
|
FRANCE = 9,
|
|
|
|
GERMANY = 10,
|
|
|
|
ITALY = 11,
|
|
|
|
JAPAN = 12,
|
|
|
|
KOREA = 13,
|
|
|
|
UK = 14,
|
|
|
|
US = 15,
|
|
|
|
CANADA = 16,
|
|
|
|
CANADA_FRENCH = 17,
|
|
|
|
ROOT = 18,
|
|
|
|
NUM_CONSTANTS = 19;
|
|
|
|
static {
|
2020-09-24 15:28:10 +00:00
|
|
|
CDS.initializeFromArchive(BaseLocale.class);
|
2019-04-03 17:06:35 +02:00
|
|
|
BaseLocale[] baseLocales = constantBaseLocales;
|
|
|
|
if (baseLocales == null) {
|
|
|
|
baseLocales = new BaseLocale[NUM_CONSTANTS];
|
|
|
|
baseLocales[ENGLISH] = createInstance("en", "");
|
|
|
|
baseLocales[FRENCH] = createInstance("fr", "");
|
|
|
|
baseLocales[GERMAN] = createInstance("de", "");
|
|
|
|
baseLocales[ITALIAN] = createInstance("it", "");
|
|
|
|
baseLocales[JAPANESE] = createInstance("ja", "");
|
|
|
|
baseLocales[KOREAN] = createInstance("ko", "");
|
|
|
|
baseLocales[CHINESE] = createInstance("zh", "");
|
|
|
|
baseLocales[SIMPLIFIED_CHINESE] = createInstance("zh", "CN");
|
|
|
|
baseLocales[TRADITIONAL_CHINESE] = createInstance("zh", "TW");
|
|
|
|
baseLocales[FRANCE] = createInstance("fr", "FR");
|
|
|
|
baseLocales[GERMANY] = createInstance("de", "DE");
|
|
|
|
baseLocales[ITALY] = createInstance("it", "IT");
|
|
|
|
baseLocales[JAPAN] = createInstance("ja", "JP");
|
|
|
|
baseLocales[KOREA] = createInstance("ko", "KR");
|
|
|
|
baseLocales[UK] = createInstance("en", "GB");
|
|
|
|
baseLocales[US] = createInstance("en", "US");
|
|
|
|
baseLocales[CANADA] = createInstance("en", "CA");
|
|
|
|
baseLocales[CANADA_FRENCH] = createInstance("fr", "CA");
|
|
|
|
baseLocales[ROOT] = createInstance("", "");
|
|
|
|
constantBaseLocales = baseLocales;
|
|
|
|
}
|
|
|
|
}
|
2010-09-10 15:29:40 -07:00
|
|
|
|
2019-04-03 17:06:35 +02:00
|
|
|
public static final String SEP = "_";
|
2010-09-10 15:29:40 -07:00
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
private final String language;
|
|
|
|
private final String script;
|
|
|
|
private final String region;
|
|
|
|
private final String variant;
|
2010-09-10 15:29:40 -07:00
|
|
|
|
2015-12-21 20:54:00 +01:00
|
|
|
private volatile int hash;
|
2011-04-14 15:59:47 +09:00
|
|
|
|
2018-02-08 18:45:30 +01:00
|
|
|
// This method must be called with normalize = false only when creating the
|
|
|
|
// Locale.* constants and non-normalized BaseLocale$Keys used for lookup.
|
|
|
|
private BaseLocale(String language, String script, String region, String variant,
|
|
|
|
boolean normalize) {
|
|
|
|
if (normalize) {
|
|
|
|
this.language = LocaleUtils.toLowerString(language).intern();
|
|
|
|
this.script = LocaleUtils.toTitleString(script).intern();
|
|
|
|
this.region = LocaleUtils.toUpperString(region).intern();
|
|
|
|
this.variant = variant.intern();
|
|
|
|
} else {
|
|
|
|
this.language = language;
|
|
|
|
this.script = script;
|
|
|
|
this.region = region;
|
|
|
|
this.variant = variant;
|
|
|
|
}
|
2011-04-14 15:59:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called for creating the Locale.* constants. No argument
|
|
|
|
// validation is performed.
|
2019-04-03 17:06:35 +02:00
|
|
|
private static BaseLocale createInstance(String language, String region) {
|
|
|
|
return new BaseLocale(language, "", region, "", false);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
public static BaseLocale getInstance(String language, String script,
|
|
|
|
String region, String variant) {
|
2019-04-03 17:06:35 +02:00
|
|
|
|
|
|
|
if (script == null) {
|
|
|
|
script = "";
|
|
|
|
}
|
|
|
|
if (region == null) {
|
|
|
|
region = "";
|
|
|
|
}
|
|
|
|
if (language == null) {
|
|
|
|
language = null;
|
|
|
|
}
|
|
|
|
if (variant == null) {
|
|
|
|
variant = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-allocating for most uses
|
|
|
|
language = LocaleUtils.toLowerString(language);
|
|
|
|
region = LocaleUtils.toUpperString(region);
|
|
|
|
|
|
|
|
// Check for constant base locales first
|
|
|
|
if (script.isEmpty() && variant.isEmpty()) {
|
|
|
|
for (BaseLocale baseLocale : constantBaseLocales) {
|
|
|
|
if (baseLocale.getLanguage().equals(language)
|
|
|
|
&& baseLocale.getRegion().equals(region)) {
|
|
|
|
return baseLocale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-10 15:29:40 -07:00
|
|
|
// JDK uses deprecated ISO639.1 language codes for he, yi and id
|
2019-04-03 17:06:35 +02:00
|
|
|
if (!language.isEmpty()) {
|
|
|
|
if (language.equals("he")) {
|
2010-09-23 20:05:20 -07:00
|
|
|
language = "iw";
|
2019-04-03 17:06:35 +02:00
|
|
|
} else if (language.equals("yi")) {
|
2010-09-23 20:05:20 -07:00
|
|
|
language = "ji";
|
2019-04-03 17:06:35 +02:00
|
|
|
} else if (language.equals("id")) {
|
2010-09-23 20:05:20 -07:00
|
|
|
language = "in";
|
|
|
|
}
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
2018-02-08 18:45:30 +01:00
|
|
|
Key key = new Key(language, script, region, variant, false);
|
2019-04-03 17:06:35 +02:00
|
|
|
return Cache.CACHE.get(key);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getLanguage() {
|
2011-04-14 15:59:47 +09:00
|
|
|
return language;
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getScript() {
|
2011-04-14 15:59:47 +09:00
|
|
|
return script;
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getRegion() {
|
2011-04-14 15:59:47 +09:00
|
|
|
return region;
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getVariant() {
|
2011-04-14 15:59:47 +09:00
|
|
|
return variant;
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
@Override
|
2010-09-10 15:29:40 -07:00
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!(obj instanceof BaseLocale)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BaseLocale other = (BaseLocale)obj;
|
2011-04-14 15:59:47 +09:00
|
|
|
return language == other.language
|
|
|
|
&& script == other.script
|
|
|
|
&& region == other.region
|
|
|
|
&& variant == other.variant;
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
@Override
|
2010-09-10 15:29:40 -07:00
|
|
|
public String toString() {
|
2014-08-27 22:08:19 +04:00
|
|
|
StringJoiner sj = new StringJoiner(", ");
|
2018-02-08 18:45:30 +01:00
|
|
|
if (!language.isEmpty()) {
|
2014-08-27 22:08:19 +04:00
|
|
|
sj.add("language=" + language);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2018-02-08 18:45:30 +01:00
|
|
|
if (!script.isEmpty()) {
|
2014-08-27 22:08:19 +04:00
|
|
|
sj.add("script=" + script);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2018-02-08 18:45:30 +01:00
|
|
|
if (!region.isEmpty()) {
|
2014-08-27 22:08:19 +04:00
|
|
|
sj.add("region=" + region);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2018-02-08 18:45:30 +01:00
|
|
|
if (!variant.isEmpty()) {
|
2014-08-27 22:08:19 +04:00
|
|
|
sj.add("variant=" + variant);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2014-08-27 22:08:19 +04:00
|
|
|
return sj.toString();
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
@Override
|
2010-09-10 15:29:40 -07:00
|
|
|
public int hashCode() {
|
2011-04-14 15:59:47 +09:00
|
|
|
int h = hash;
|
2010-09-10 15:29:40 -07:00
|
|
|
if (h == 0) {
|
|
|
|
// Generating a hash value from language, script, region and variant
|
2011-04-14 15:59:47 +09:00
|
|
|
h = language.hashCode();
|
|
|
|
h = 31 * h + script.hashCode();
|
|
|
|
h = 31 * h + region.hashCode();
|
|
|
|
h = 31 * h + variant.hashCode();
|
2015-12-21 20:54:00 +01:00
|
|
|
if (h != 0) {
|
|
|
|
hash = h;
|
|
|
|
}
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2014-04-21 11:08:30 -07:00
|
|
|
private static final class Key {
|
2018-02-08 18:45:30 +01:00
|
|
|
/**
|
|
|
|
* Keep a SoftReference to the Key data if normalized (actually used
|
|
|
|
* as a cache key) and not initialized via the constant creation path.
|
|
|
|
*
|
|
|
|
* This allows us to avoid creating SoftReferences on lookup Keys
|
|
|
|
* (which are short-lived) and for Locales created via
|
|
|
|
* Locale#createConstant.
|
|
|
|
*/
|
|
|
|
private final SoftReference<BaseLocale> holderRef;
|
|
|
|
private final BaseLocale holder;
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
private final boolean normalized;
|
|
|
|
private final int hash;
|
|
|
|
|
2018-02-08 18:45:30 +01:00
|
|
|
private Key(String language, String script, String region,
|
|
|
|
String variant, boolean normalize) {
|
|
|
|
BaseLocale locale = new BaseLocale(language, script, region, variant, normalize);
|
|
|
|
this.normalized = normalize;
|
|
|
|
if (normalized) {
|
|
|
|
this.holderRef = new SoftReference<>(locale);
|
|
|
|
this.holder = null;
|
|
|
|
} else {
|
|
|
|
this.holderRef = null;
|
|
|
|
this.holder = locale;
|
|
|
|
}
|
|
|
|
this.hash = hashCode(locale);
|
2011-04-14 15:59:47 +09:00
|
|
|
}
|
|
|
|
|
2018-02-08 18:45:30 +01:00
|
|
|
public int hashCode() {
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int hashCode(BaseLocale locale) {
|
2011-04-14 15:59:47 +09:00
|
|
|
int h = 0;
|
2018-02-08 18:45:30 +01:00
|
|
|
String lang = locale.getLanguage();
|
|
|
|
int len = lang.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31*h + LocaleUtils.toLower(lang.charAt(i));
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2018-02-08 18:45:30 +01:00
|
|
|
String scrt = locale.getScript();
|
|
|
|
len = scrt.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31*h + LocaleUtils.toLower(scrt.charAt(i));
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2018-02-08 18:45:30 +01:00
|
|
|
String regn = locale.getRegion();
|
|
|
|
len = regn.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31*h + LocaleUtils.toLower(regn.charAt(i));
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2018-02-08 18:45:30 +01:00
|
|
|
String vart = locale.getVariant();
|
|
|
|
len = vart.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31*h + vart.charAt(i);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2018-02-08 18:45:30 +01:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
private BaseLocale getBaseLocale() {
|
|
|
|
return (holder == null) ? holderRef.get() : holder;
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
@Override
|
2010-09-10 15:29:40 -07:00
|
|
|
public boolean equals(Object obj) {
|
2014-04-21 11:08:30 -07:00
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (obj instanceof Key && this.hash == ((Key)obj).hash) {
|
2018-02-08 18:45:30 +01:00
|
|
|
BaseLocale other = ((Key) obj).getBaseLocale();
|
|
|
|
BaseLocale locale = this.getBaseLocale();
|
|
|
|
if (other != null && locale != null
|
|
|
|
&& LocaleUtils.caseIgnoreMatch(other.getLanguage(), locale.getLanguage())
|
|
|
|
&& LocaleUtils.caseIgnoreMatch(other.getScript(), locale.getScript())
|
|
|
|
&& LocaleUtils.caseIgnoreMatch(other.getRegion(), locale.getRegion())
|
|
|
|
// variant is case sensitive in JDK!
|
|
|
|
&& other.getVariant().equals(locale.getVariant())) {
|
|
|
|
return true;
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-21 11:08:30 -07:00
|
|
|
return false;
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Key normalize(Key key) {
|
2011-04-14 15:59:47 +09:00
|
|
|
if (key.normalized) {
|
|
|
|
return key;
|
|
|
|
}
|
2010-09-10 15:29:40 -07:00
|
|
|
|
2018-02-08 18:45:30 +01:00
|
|
|
// Only normalized keys may be softly referencing the data holder
|
|
|
|
assert (key.holder != null && key.holderRef == null);
|
|
|
|
BaseLocale locale = key.holder;
|
|
|
|
return new Key(locale.getLanguage(), locale.getScript(),
|
|
|
|
locale.getRegion(), locale.getVariant(), true);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Cache extends LocaleObjectCache<Key, BaseLocale> {
|
|
|
|
|
2019-04-03 17:06:35 +02:00
|
|
|
private static final Cache CACHE = new Cache();
|
|
|
|
|
2010-09-10 15:29:40 -07:00
|
|
|
public Cache() {
|
|
|
|
}
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
@Override
|
2010-09-10 15:29:40 -07:00
|
|
|
protected Key normalizeKey(Key key) {
|
|
|
|
return Key.normalize(key);
|
|
|
|
}
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
@Override
|
2010-09-10 15:29:40 -07:00
|
|
|
protected BaseLocale createObject(Key key) {
|
2018-02-08 18:45:30 +01:00
|
|
|
return Key.normalize(key).getBaseLocale();
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|