提交 e87b4d05 编写于 作者: N naoto

8036936: Use local locales

Summary: Made sure cache key is cleared on GC invocation
Reviewed-by: okutsu
上级 9d4d4689
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
*/ */
package sun.util.locale; package sun.util.locale;
import java.lang.ref.SoftReference;
public final class BaseLocale { public final class BaseLocale {
...@@ -163,11 +164,11 @@ public final class BaseLocale { ...@@ -163,11 +164,11 @@ public final class BaseLocale {
return h; return h;
} }
private static final class Key implements Comparable<Key> { private static final class Key {
private final String lang; private final SoftReference<String> lang;
private final String scrt; private final SoftReference<String> scrt;
private final String regn; private final SoftReference<String> regn;
private final String vart; private final SoftReference<String> vart;
private final boolean normalized; private final boolean normalized;
private final int hash; private final int hash;
...@@ -179,10 +180,10 @@ public final class BaseLocale { ...@@ -179,10 +180,10 @@ public final class BaseLocale {
assert language.intern() == language assert language.intern() == language
&& region.intern() == region; && region.intern() == region;
lang = language; lang = new SoftReference(language);
scrt = ""; scrt = new SoftReference("");
regn = region; regn = new SoftReference(region);
vart = ""; vart = new SoftReference("");
this.normalized = true; this.normalized = true;
int h = language.hashCode(); int h = language.hashCode();
...@@ -203,40 +204,40 @@ public final class BaseLocale { ...@@ -203,40 +204,40 @@ public final class BaseLocale {
String variant, boolean normalized) { String variant, boolean normalized) {
int h = 0; int h = 0;
if (language != null) { if (language != null) {
lang = language; lang = new SoftReference(language);
int len = language.length(); int len = language.length();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
h = 31*h + LocaleUtils.toLower(language.charAt(i)); h = 31*h + LocaleUtils.toLower(language.charAt(i));
} }
} else { } else {
lang = ""; lang = new SoftReference("");
} }
if (script != null) { if (script != null) {
scrt = script; scrt = new SoftReference(script);
int len = script.length(); int len = script.length();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
h = 31*h + LocaleUtils.toLower(script.charAt(i)); h = 31*h + LocaleUtils.toLower(script.charAt(i));
} }
} else { } else {
scrt = ""; scrt = new SoftReference("");
} }
if (region != null) { if (region != null) {
regn = region; regn = new SoftReference(region);
int len = region.length(); int len = region.length();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
h = 31*h + LocaleUtils.toLower(region.charAt(i)); h = 31*h + LocaleUtils.toLower(region.charAt(i));
} }
} else { } else {
regn = ""; regn = new SoftReference("");
} }
if (variant != null) { if (variant != null) {
vart = variant; vart = new SoftReference(variant);
int len = variant.length(); int len = variant.length();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
h = 31*h + variant.charAt(i); h = 31*h + variant.charAt(i);
} }
} else { } else {
vart = ""; vart = new SoftReference("");
} }
hash = h; hash = h;
this.normalized = normalized; this.normalized = normalized;
...@@ -244,28 +245,31 @@ public final class BaseLocale { ...@@ -244,28 +245,31 @@ public final class BaseLocale {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return (this == obj) || if (this == obj) {
(obj instanceof Key) return true;
&& this.hash == ((Key)obj).hash
&& LocaleUtils.caseIgnoreMatch(((Key)obj).lang, this.lang)
&& LocaleUtils.caseIgnoreMatch(((Key)obj).scrt, this.scrt)
&& LocaleUtils.caseIgnoreMatch(((Key)obj).regn, this.regn)
&& ((Key)obj).vart.equals(vart); // variant is case sensitive in JDK!
} }
@Override if (obj instanceof Key && this.hash == ((Key)obj).hash) {
public int compareTo(Key other) { String tl = this.lang.get();
int res = LocaleUtils.caseIgnoreCompare(this.lang, other.lang); String ol = ((Key)obj).lang.get();
if (res == 0) { if (tl != null && ol != null &&
res = LocaleUtils.caseIgnoreCompare(this.scrt, other.scrt); LocaleUtils.caseIgnoreMatch(ol, tl)) {
if (res == 0) { String ts = this.scrt.get();
res = LocaleUtils.caseIgnoreCompare(this.regn, other.regn); String os = ((Key)obj).scrt.get();
if (res == 0) { if (ts != null && os != null &&
res = this.vart.compareTo(other.vart); 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));
} }
} }
} }
return res; }
return false;
} }
@Override @Override
...@@ -278,10 +282,10 @@ public final class BaseLocale { ...@@ -278,10 +282,10 @@ public final class BaseLocale {
return key; return key;
} }
String lang = LocaleUtils.toLowerString(key.lang).intern(); String lang = LocaleUtils.toLowerString(key.lang.get()).intern();
String scrt = LocaleUtils.toTitleString(key.scrt).intern(); String scrt = LocaleUtils.toTitleString(key.scrt.get()).intern();
String regn = LocaleUtils.toUpperString(key.regn).intern(); String regn = LocaleUtils.toUpperString(key.regn.get()).intern();
String vart = key.vart.intern(); // preserve upper/lower cases String vart = key.vart.get().intern(); // preserve upper/lower cases
return new Key(lang, scrt, regn, vart, true); return new Key(lang, scrt, regn, vart, true);
} }
...@@ -294,12 +298,18 @@ public final class BaseLocale { ...@@ -294,12 +298,18 @@ public final class BaseLocale {
@Override @Override
protected Key normalizeKey(Key key) { protected Key normalizeKey(Key key) {
assert key.lang.get() != null &&
key.scrt.get() != null &&
key.regn.get() != null &&
key.vart.get() != null;
return Key.normalize(key); return Key.normalize(key);
} }
@Override @Override
protected BaseLocale createObject(Key key) { protected BaseLocale createObject(Key key) {
return new BaseLocale(key.lang, key.scrt, key.regn, key.vart); return new BaseLocale(key.lang.get(), key.scrt.get(),
key.regn.get(), key.vart.get());
} }
} }
} }
...@@ -57,8 +57,10 @@ public abstract class LocaleObjectCache<K, V> { ...@@ -57,8 +57,10 @@ public abstract class LocaleObjectCache<K, V> {
value = entry.get(); value = entry.get();
} }
if (value == null) { if (value == null) {
key = normalizeKey(key);
V newVal = createObject(key); V newVal = createObject(key);
// make sure key is normalized *after* the object creation
// so that newVal is assured to be created from a valid key.
key = normalizeKey(key);
if (key == null || newVal == null) { if (key == null || newVal == null) {
// subclass must return non-null key/value object // subclass must return non-null key/value object
return null; return null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册