jdk/src/share/classes/java/util/Locale.java
changeset 11282 2c8538c9daa6
parent 10419 12c063b39232
child 13583 dc0017b1a452
equal deleted inserted replaced
11281:58ecbc808474 11282:2c8538c9daa6
   735      *
   735      *
   736      * @return the default locale for this instance of the Java Virtual Machine
   736      * @return the default locale for this instance of the Java Virtual Machine
   737      */
   737      */
   738     public static Locale getDefault() {
   738     public static Locale getDefault() {
   739         // do not synchronize this method - see 4071298
   739         // do not synchronize this method - see 4071298
   740         // it's OK if more than one default locale happens to be created
       
   741         if (defaultLocale == null) {
       
   742             initDefault();
       
   743         }
       
   744         return defaultLocale;
   740         return defaultLocale;
   745     }
   741     }
   746 
   742 
   747     /**
   743     /**
   748      * Gets the current value of the default locale for the specified Category
   744      * Gets the current value of the default locale for the specified Category
   760      * @see #setDefault(Locale.Category, Locale)
   756      * @see #setDefault(Locale.Category, Locale)
   761      * @since 1.7
   757      * @since 1.7
   762      */
   758      */
   763     public static Locale getDefault(Locale.Category category) {
   759     public static Locale getDefault(Locale.Category category) {
   764         // do not synchronize this method - see 4071298
   760         // do not synchronize this method - see 4071298
   765         // it's OK if more than one default locale happens to be created
       
   766         switch (category) {
   761         switch (category) {
   767         case DISPLAY:
   762         case DISPLAY:
   768             if (defaultDisplayLocale == null) {
   763             if (defaultDisplayLocale == null) {
   769                 initDefault(category);
   764                 synchronized(Locale.class) {
       
   765                     if (defaultDisplayLocale == null) {
       
   766                         defaultDisplayLocale = initDefault(category);
       
   767                     }
       
   768                 }
   770             }
   769             }
   771             return defaultDisplayLocale;
   770             return defaultDisplayLocale;
   772         case FORMAT:
   771         case FORMAT:
   773             if (defaultFormatLocale == null) {
   772             if (defaultFormatLocale == null) {
   774                 initDefault(category);
   773                 synchronized(Locale.class) {
       
   774                     if (defaultFormatLocale == null) {
       
   775                         defaultFormatLocale = initDefault(category);
       
   776                     }
       
   777                 }
   775             }
   778             }
   776             return defaultFormatLocale;
   779             return defaultFormatLocale;
   777         default:
   780         default:
   778             assert false: "Unknown Category";
   781             assert false: "Unknown Category";
   779         }
   782         }
   780         return getDefault();
   783         return getDefault();
   781     }
   784     }
   782 
   785 
   783     private static void initDefault() {
   786     private static Locale initDefault() {
   784         String language, region, script, country, variant;
   787         String language, region, script, country, variant;
   785         language = AccessController.doPrivileged(
   788         language = AccessController.doPrivileged(
   786             new GetPropertyAction("user.language", "en"));
   789             new GetPropertyAction("user.language", "en"));
   787         // for compatibility, check for old user.region property
   790         // for compatibility, check for old user.region property
   788         region = AccessController.doPrivileged(
   791         region = AccessController.doPrivileged(
   804             country = AccessController.doPrivileged(
   807             country = AccessController.doPrivileged(
   805                 new GetPropertyAction("user.country", ""));
   808                 new GetPropertyAction("user.country", ""));
   806             variant = AccessController.doPrivileged(
   809             variant = AccessController.doPrivileged(
   807                 new GetPropertyAction("user.variant", ""));
   810                 new GetPropertyAction("user.variant", ""));
   808         }
   811         }
   809         defaultLocale = getInstance(language, script, country, variant, null);
   812 
   810     }
   813         return getInstance(language, script, country, variant, null);
   811 
   814     }
   812     private static void initDefault(Locale.Category category) {
   815 
   813         // make sure defaultLocale is initialized
   816     private static Locale initDefault(Locale.Category category) {
   814         if (defaultLocale == null) {
   817         return getInstance(
   815             initDefault();
       
   816         }
       
   817 
       
   818         Locale defaultCategoryLocale = getInstance(
       
   819             AccessController.doPrivileged(
   818             AccessController.doPrivileged(
   820                 new GetPropertyAction(category.languageKey, defaultLocale.getLanguage())),
   819                 new GetPropertyAction(category.languageKey, defaultLocale.getLanguage())),
   821             AccessController.doPrivileged(
   820             AccessController.doPrivileged(
   822                 new GetPropertyAction(category.scriptKey, defaultLocale.getScript())),
   821                 new GetPropertyAction(category.scriptKey, defaultLocale.getScript())),
   823             AccessController.doPrivileged(
   822             AccessController.doPrivileged(
   824                 new GetPropertyAction(category.countryKey, defaultLocale.getCountry())),
   823                 new GetPropertyAction(category.countryKey, defaultLocale.getCountry())),
   825             AccessController.doPrivileged(
   824             AccessController.doPrivileged(
   826                 new GetPropertyAction(category.variantKey, defaultLocale.getVariant())),
   825                 new GetPropertyAction(category.variantKey, defaultLocale.getVariant())),
   827             null);
   826             null);
   828 
       
   829         switch (category) {
       
   830         case DISPLAY:
       
   831             defaultDisplayLocale = defaultCategoryLocale;
       
   832             break;
       
   833         case FORMAT:
       
   834             defaultFormatLocale = defaultCategoryLocale;
       
   835             break;
       
   836         }
       
   837     }
   827     }
   838 
   828 
   839     /**
   829     /**
   840      * Sets the default locale for this instance of the Java Virtual Machine.
   830      * Sets the default locale for this instance of the Java Virtual Machine.
   841      * This does not affect the host locale.
   831      * This does not affect the host locale.
  1914     /**
  1904     /**
  1915      * Calculated hashcode
  1905      * Calculated hashcode
  1916      */
  1906      */
  1917     private transient volatile int hashCodeValue = 0;
  1907     private transient volatile int hashCodeValue = 0;
  1918 
  1908 
  1919     private static Locale defaultLocale = null;
  1909     private volatile static Locale defaultLocale = initDefault();
  1920     private static Locale defaultDisplayLocale = null;
  1910     private volatile static Locale defaultDisplayLocale = null;
  1921     private static Locale defaultFormatLocale = null;
  1911     private volatile static Locale defaultFormatLocale = null;
  1922 
  1912 
  1923     /**
  1913     /**
  1924      * Return an array of the display names of the variant.
  1914      * Return an array of the display names of the variant.
  1925      * @param bundle the ResourceBundle to use to get the display names
  1915      * @param bundle the ResourceBundle to use to get the display names
  1926      * @return an array of display names, possible of zero length.
  1916      * @return an array of display names, possible of zero length.