2010-09-10 15:29:40 -07:00
|
|
|
/*
|
2011-04-14 15:59:47 +09:00
|
|
|
* Copyright (c) 2010, 2011, 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;
|
2014-04-21 11:08:30 -07:00
|
|
|
import java.lang.ref.SoftReference;
|
2010-09-10 15:29:40 -07:00
|
|
|
|
2014-08-27 22:08:19 +04:00
|
|
|
import java.util.StringJoiner;
|
2010-09-10 15:29:40 -07:00
|
|
|
|
|
|
|
public final class BaseLocale {
|
|
|
|
|
|
|
|
public static final String SEP = "_";
|
|
|
|
|
|
|
|
private static final Cache CACHE = new Cache();
|
|
|
|
|
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
|
|
|
|
|
|
|
// This method must be called only when creating the Locale.* constants.
|
|
|
|
private BaseLocale(String language, String region) {
|
|
|
|
this.language = language;
|
|
|
|
this.script = "";
|
|
|
|
this.region = region;
|
|
|
|
this.variant = "";
|
|
|
|
}
|
2010-09-10 15:29:40 -07:00
|
|
|
|
|
|
|
private BaseLocale(String language, String script, String region, String variant) {
|
2011-04-14 15:59:47 +09:00
|
|
|
this.language = (language != null) ? LocaleUtils.toLowerString(language).intern() : "";
|
|
|
|
this.script = (script != null) ? LocaleUtils.toTitleString(script).intern() : "";
|
|
|
|
this.region = (region != null) ? LocaleUtils.toUpperString(region).intern() : "";
|
|
|
|
this.variant = (variant != null) ? variant.intern() : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called for creating the Locale.* constants. No argument
|
|
|
|
// validation is performed.
|
|
|
|
public static BaseLocale createInstance(String language, String region) {
|
|
|
|
BaseLocale base = new BaseLocale(language, region);
|
|
|
|
CACHE.put(new Key(language, region), base);
|
|
|
|
return base;
|
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) {
|
2010-09-10 15:29:40 -07:00
|
|
|
// JDK uses deprecated ISO639.1 language codes for he, yi and id
|
2010-09-23 20:05:20 -07:00
|
|
|
if (language != null) {
|
2011-04-14 15:59:47 +09:00
|
|
|
if (LocaleUtils.caseIgnoreMatch(language, "he")) {
|
2010-09-23 20:05:20 -07:00
|
|
|
language = "iw";
|
2011-04-14 15:59:47 +09:00
|
|
|
} else if (LocaleUtils.caseIgnoreMatch(language, "yi")) {
|
2010-09-23 20:05:20 -07:00
|
|
|
language = "ji";
|
2011-04-14 15:59:47 +09:00
|
|
|
} else if (LocaleUtils.caseIgnoreMatch(language, "id")) {
|
2010-09-23 20:05:20 -07:00
|
|
|
language = "in";
|
|
|
|
}
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Key key = new Key(language, script, region, variant);
|
|
|
|
BaseLocale baseLocale = CACHE.get(key);
|
|
|
|
return baseLocale;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(", ");
|
2011-04-14 15:59:47 +09:00
|
|
|
if (language.length() > 0) {
|
2014-08-27 22:08:19 +04:00
|
|
|
sj.add("language=" + language);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2011-04-14 15:59:47 +09:00
|
|
|
if (script.length() > 0) {
|
2014-08-27 22:08:19 +04:00
|
|
|
sj.add("script=" + script);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2011-04-14 15:59:47 +09:00
|
|
|
if (region.length() > 0) {
|
2014-08-27 22:08:19 +04:00
|
|
|
sj.add("region=" + region);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2011-04-14 15:59:47 +09:00
|
|
|
if (variant.length() > 0) {
|
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 {
|
|
|
|
private final SoftReference<String> lang;
|
|
|
|
private final SoftReference<String> scrt;
|
|
|
|
private final SoftReference<String> regn;
|
|
|
|
private final SoftReference<String> vart;
|
2011-04-14 15:59:47 +09:00
|
|
|
private final boolean normalized;
|
|
|
|
private final int hash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Key. language and region must be normalized
|
|
|
|
* (intern'ed in the proper case).
|
|
|
|
*/
|
|
|
|
private Key(String language, String region) {
|
|
|
|
assert language.intern() == language
|
|
|
|
&& region.intern() == region;
|
2010-09-10 15:29:40 -07:00
|
|
|
|
2014-04-21 11:08:30 -07:00
|
|
|
lang = new SoftReference<>(language);
|
|
|
|
scrt = new SoftReference<>("");
|
|
|
|
regn = new SoftReference<>(region);
|
|
|
|
vart = new SoftReference<>("");
|
2011-04-14 15:59:47 +09:00
|
|
|
this.normalized = true;
|
|
|
|
|
|
|
|
int h = language.hashCode();
|
|
|
|
if (region != "") {
|
|
|
|
int len = region.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31 * h + LocaleUtils.toLower(region.charAt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hash = h;
|
|
|
|
}
|
2010-09-10 15:29:40 -07:00
|
|
|
|
|
|
|
public Key(String language, String script, String region, String variant) {
|
2011-04-14 15:59:47 +09:00
|
|
|
this(language, script, region, variant, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Key(String language, String script, String region,
|
|
|
|
String variant, boolean normalized) {
|
|
|
|
int h = 0;
|
2010-09-10 15:29:40 -07:00
|
|
|
if (language != null) {
|
2014-04-21 11:08:30 -07:00
|
|
|
lang = new SoftReference<>(language);
|
2011-04-14 15:59:47 +09:00
|
|
|
int len = language.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31*h + LocaleUtils.toLower(language.charAt(i));
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-21 11:08:30 -07:00
|
|
|
lang = new SoftReference<>("");
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
if (script != null) {
|
2014-04-21 11:08:30 -07:00
|
|
|
scrt = new SoftReference<>(script);
|
2011-04-14 15:59:47 +09:00
|
|
|
int len = script.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31*h + LocaleUtils.toLower(script.charAt(i));
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-21 11:08:30 -07:00
|
|
|
scrt = new SoftReference<>("");
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
if (region != null) {
|
2014-04-21 11:08:30 -07:00
|
|
|
regn = new SoftReference<>(region);
|
2011-04-14 15:59:47 +09:00
|
|
|
int len = region.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31*h + LocaleUtils.toLower(region.charAt(i));
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-21 11:08:30 -07:00
|
|
|
regn = new SoftReference<>("");
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
if (variant != null) {
|
2014-04-21 11:08:30 -07:00
|
|
|
vart = new SoftReference<>(variant);
|
2011-04-14 15:59:47 +09:00
|
|
|
int len = variant.length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
h = 31*h + variant.charAt(i);
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-21 11:08:30 -07:00
|
|
|
vart = new SoftReference<>("");
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
2011-04-14 15:59:47 +09:00
|
|
|
hash = h;
|
|
|
|
this.normalized = normalized;
|
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;
|
|
|
|
}
|
2010-09-10 15:29:40 -07:00
|
|
|
|
2014-04-21 11:08:30 -07:00
|
|
|
if (obj instanceof Key && this.hash == ((Key)obj).hash) {
|
|
|
|
String tl = this.lang.get();
|
|
|
|
String ol = ((Key)obj).lang.get();
|
|
|
|
if (tl != null && ol != null &&
|
|
|
|
LocaleUtils.caseIgnoreMatch(ol, tl)) {
|
|
|
|
String ts = this.scrt.get();
|
|
|
|
String os = ((Key)obj).scrt.get();
|
|
|
|
if (ts != null && os != null &&
|
|
|
|
LocaleUtils.caseIgnoreMatch(os, ts)) {
|
|
|
|
String tr = this.regn.get();
|
|
|
|
String or = ((Key)obj).regn.get();
|
|
|
|
if (tr != null && or != null &&
|
|
|
|
LocaleUtils.caseIgnoreMatch(or, tr)) {
|
|
|
|
String tv = this.vart.get();
|
|
|
|
String ov = ((Key)obj).vart.get();
|
|
|
|
return (ov != null && ov.equals(tv));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
return hash;
|
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
|
|
|
|
2014-04-21 11:08:30 -07:00
|
|
|
String lang = LocaleUtils.toLowerString(key.lang.get()).intern();
|
|
|
|
String scrt = LocaleUtils.toTitleString(key.scrt.get()).intern();
|
|
|
|
String regn = LocaleUtils.toUpperString(key.regn.get()).intern();
|
|
|
|
String vart = key.vart.get().intern(); // preserve upper/lower cases
|
2011-04-14 15:59:47 +09:00
|
|
|
|
|
|
|
return new Key(lang, scrt, regn, vart, true);
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Cache extends LocaleObjectCache<Key, BaseLocale> {
|
|
|
|
|
|
|
|
public Cache() {
|
|
|
|
}
|
|
|
|
|
2011-04-14 15:59:47 +09:00
|
|
|
@Override
|
2010-09-10 15:29:40 -07:00
|
|
|
protected Key normalizeKey(Key key) {
|
2014-04-21 11:08:30 -07:00
|
|
|
assert key.lang.get() != null &&
|
|
|
|
key.scrt.get() != null &&
|
|
|
|
key.regn.get() != null &&
|
|
|
|
key.vart.get() != null;
|
|
|
|
|
2010-09-10 15:29:40 -07:00
|
|
|
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) {
|
2014-04-21 11:08:30 -07:00
|
|
|
return new BaseLocale(key.lang.get(), key.scrt.get(),
|
|
|
|
key.regn.get(), key.vart.get());
|
2010-09-10 15:29:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|