@@ -545,13 +545,13 @@ public Locale toLocale() {
545
545
/**
546
546
* Keep our own default ULocale.
547
547
*/
548
- private static Locale defaultLocale = Locale .getDefault ();
549
- private static ULocale defaultULocale ;
548
+ private static volatile ULocale defaultULocale ;
550
549
551
550
private static Locale [] defaultCategoryLocales = new Locale [Category .values ().length ];
552
551
private static ULocale [] defaultCategoryULocales = new ULocale [Category .values ().length ];
553
552
554
553
static {
554
+ Locale defaultLocale = Locale .getDefault ();
555
555
defaultULocale = forLocale (defaultLocale );
556
556
557
557
if (JDKLocaleHelper .hasLocaleCategories ()) {
@@ -581,34 +581,47 @@ public Locale toLocale() {
581
581
* @stable ICU 2.8
582
582
*/
583
583
public static ULocale getDefault () {
584
+ // Only synchronize if we must update the default locale.
585
+ ULocale currentDefaultULocale = defaultULocale ;
586
+ if (currentDefaultULocale == null ) {
587
+ // When Java's default locale has extensions (such as ja-JP-u-ca-japanese),
588
+ // Locale -> ULocale mapping requires BCP47 keyword mapping data that is currently
589
+ // stored in a resource bundle.
590
+ // If this happens during the class initialization's call to .forLocale(defaultLocale),
591
+ // then defaultULocale is still null until forLocale() returns.
592
+ // However, UResourceBundle currently requires non-null default ULocale.
593
+ // For now, this implementation returns ULocale.ROOT to avoid the problem.
594
+ // TODO: Consider moving BCP47 mapping data out of resource bundle later.
595
+ return ULocale .ROOT ;
596
+ } else if (currentDefaultULocale .locale .equals (Locale .getDefault ())) {
597
+ return currentDefaultULocale ;
598
+ }
584
599
synchronized (ULocale .class ) {
585
- if (defaultULocale == null ) {
586
- // When Java's default locale has extensions (such as ja-JP-u-ca-japanese),
587
- // Locale -> ULocale mapping requires BCP47 keyword mapping data that is currently
588
- // stored in a resource bundle. However, UResourceBundle currently requires
589
- // non-null default ULocale. For now, this implementation returns ULocale.ROOT
590
- // to avoid the problem.
600
+ Locale currentDefault = Locale .getDefault ();
601
+ assert currentDefault != null ;
591
602
592
- // TODO: Consider moving BCP47 mapping data out of resource bundle later.
603
+ currentDefaultULocale = defaultULocale ;
604
+ assert currentDefaultULocale != null ;
593
605
594
- return ULocale .ROOT ;
606
+ if (currentDefaultULocale .locale .equals (currentDefault )) {
607
+ return currentDefaultULocale ;
595
608
}
596
- Locale currentDefault = Locale . getDefault ();
597
- if (! defaultLocale . equals ( currentDefault )) {
598
- defaultLocale = currentDefault ;
599
- defaultULocale = forLocale ( currentDefault );
600
-
601
- if (! JDKLocaleHelper . hasLocaleCategories ()) {
602
- // Detected Java default Locale change.
603
- // We need to update category defaults to match
604
- // Java 7's behavior on Android API level 21..23.
605
- for ( Category cat : Category . values ()) {
606
- int idx = cat . ordinal () ;
607
- defaultCategoryLocales [idx ] = currentDefault ;
608
- defaultCategoryULocales [ idx ] = forLocale ( currentDefault );
609
- }
610
- } }
611
- return defaultULocale ;
609
+
610
+ ULocale nextULocale = forLocale ( currentDefault );
611
+ assert nextULocale != null ;
612
+
613
+ if (! JDKLocaleHelper . hasLocaleCategories ()) {
614
+ // Detected Java default Locale change.
615
+ // We need to update category defaults to match
616
+ // Java 7's behavior on Android API level 21..23.
617
+ for ( Category cat : Category . values ()) {
618
+ int idx = cat . ordinal ();
619
+ defaultCategoryLocales [ idx ] = currentDefault ;
620
+ defaultCategoryULocales [idx ] = nextULocale ;
621
+ }
622
+ }
623
+
624
+ return defaultULocale = nextULocale ;
612
625
}
613
626
}
614
627
@@ -630,8 +643,7 @@ public static ULocale getDefault() {
630
643
* @stable ICU 3.0
631
644
*/
632
645
public static synchronized void setDefault (ULocale newLocale ){
633
- defaultLocale = newLocale .toLocale ();
634
- Locale .setDefault (defaultLocale );
646
+ Locale .setDefault (newLocale .toLocale ());
635
647
defaultULocale = newLocale ;
636
648
// This method also updates all category default locales
637
649
for (Category cat : Category .values ()) {
@@ -675,8 +687,7 @@ public static ULocale getDefault(Category category) {
675
687
// time.
676
688
677
689
Locale currentDefault = Locale .getDefault ();
678
- if (!defaultLocale .equals (currentDefault )) {
679
- defaultLocale = currentDefault ;
690
+ if (!defaultULocale .locale .equals (currentDefault )) {
680
691
defaultULocale = forLocale (currentDefault );
681
692
682
693
for (Category cat : Category .values ()) {
0 commit comments