commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r911968 - in /commons/proper/lang/branches/LANG_2_X: RELEASE-NOTES.txt src/main/java/org/apache/commons/lang/LocaleUtils.java src/site/changes/changes.xml
Date Fri, 19 Feb 2010 20:26:22 GMT
Author: niallp
Date: Fri Feb 19 20:26:21 2010
New Revision: 911968

URL: http://svn.apache.org/viewvc?rev=911968&view=rev
Log:
Port LANG-511 to 2.x branch -  Moving availableLocaleSet and availableLocaleList to both lazily
initialize in a separate synchronized method. This brings the two pieces of code into line
with each other, allows availableLocaleSet() to be unsynchronized as desired in LANG-488 and
removes the static initialization of availableLocaleList() as requested in LANG-511

Modified:
    commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/LocaleUtils.java
    commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml

Modified: commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt?rev=911968&r1=911967&r2=911968&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt (original)
+++ commons/proper/lang/branches/LANG_2_X/RELEASE-NOTES.txt Fri Feb 19 20:26:21 2010
@@ -75,4 +75,5 @@
     * [LANG-459] - HashCodeBuilder - reflectionHashCode() can generate incorrect hashcodes
     * [LANG-487] - ToStringBuilder - make default style thread-safe
     * [LANG-472] - RandomUtils - nextLong() always produces even numbers
+    * [LANG-511] - LocaleUtils - initialization of available locales can be deferred
 

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/LocaleUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/LocaleUtils.java?rev=911968&r1=911967&r2=911968&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/LocaleUtils.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/LocaleUtils.java
Fri Feb 19 20:26:21 2010
@@ -40,17 +40,16 @@
 public class LocaleUtils {
 
     /** Unmodifiable list of available locales. */
-    private static final List cAvailableLocaleList;
+    private static List cAvailableLocaleList; // lazily created by availableLocaleList()
+
     /** Unmodifiable set of available locales. */
-    private static Set cAvailableLocaleSet;
+    private static Set cAvailableLocaleSet;   // lazily created by availableLocaleSet()
+
     /** Unmodifiable map of language locales by country. */
     private static final Map cLanguagesByCountry = Collections.synchronizedMap(new HashMap());
+
     /** Unmodifiable map of country locales by language. */
     private static final Map cCountriesByLanguage = Collections.synchronizedMap(new HashMap());
-    static {
-        List list = Arrays.asList(Locale.getAvailableLocales());
-        cAvailableLocaleList = Collections.unmodifiableList(list);
-    }
 
     /**
      * <p><code>LocaleUtils</code> instances should NOT be constructed
in standard programming.
@@ -192,9 +191,24 @@
      * @return the unmodifiable list of available locales
      */
     public static List availableLocaleList() {
+        if(cAvailableLocaleList == null) { 
+            initAvailableLocaleList(); 
+        }
         return cAvailableLocaleList;
     }
 
+    /**
+     * Initializes the availableLocaleList. It is separate from availableLocaleList() 
+     * to avoid the synchronized block affecting normal use, yet synchronized and 
+     * lazy loading to avoid a static block affecting other methods in this class. 
+     */
+    private static synchronized void initAvailableLocaleList() {
+        if(cAvailableLocaleList == null) {
+            List list = Arrays.asList(Locale.getAvailableLocales());
+            cAvailableLocaleList = Collections.unmodifiableList(list);
+        }
+    }
+
     //-----------------------------------------------------------------------
     /**
      * <p>Obtains an unmodifiable set of installed locales.</p>
@@ -206,13 +220,21 @@
      * @return the unmodifiable set of available locales
      */
     public static Set availableLocaleSet() {
-        Set set = cAvailableLocaleSet;
-        if (set == null) {
-            set = new HashSet(availableLocaleList());
-            set = Collections.unmodifiableSet(set);
-            cAvailableLocaleSet = set;
+        if(cAvailableLocaleSet == null) { 
+            initAvailableLocaleSet(); 
+        }
+        return cAvailableLocaleSet;
+    }
+
+    /**
+     * Initializes the availableLocaleSet. It is separate from availableLocaleSet() 
+     * to avoid the synchronized block affecting normal use, yet synchronized and 
+     * lazy loading to avoid a static block affecting other methods in this class. 
+     */
+    private static synchronized void initAvailableLocaleSet() {
+        if(cAvailableLocaleSet == null) {
+            cAvailableLocaleSet = Collections.unmodifiableSet( new HashSet(availableLocaleList())
);
         }
-        return set;
     }
 
     //-----------------------------------------------------------------------

Modified: commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml?rev=911968&r1=911967&r2=911968&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml (original)
+++ commons/proper/lang/branches/LANG_2_X/src/site/changes/changes.xml Fri Feb 19 20:26:21
2010
@@ -73,6 +73,7 @@
     <action type="fix" issue="LANG-459">HashCodeBuilder - reflectionHashCode() can
generate incorrect hashcodes</action>
     <action type="fix" issue="LANG-487">ToStringBuilder - make default style thread-safe</action>
     <action type="fix" issue="LANG-472">RandomUtils - nextLong() always produces even
numbers</action>
+    <action type="fix" issue="LANG-511">LocaleUtils - Initialization of available locales
in LocaleUtils can be deferred</action>
   </release>
 
   <release version="2.4" date="2008-03-18" description="">



Mime
View raw message