You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/07/07 09:10:02 UTC

svn commit: r791726 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java

Author: bayard
Date: Tue Jul  7 07:10:02 2009
New Revision: 791726

URL: http://svn.apache.org/viewvc?rev=791726&view=rev
Log:
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/trunk/src/java/org/apache/commons/lang/LocaleUtils.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java?rev=791726&r1=791725&r2=791726&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java Tue Jul  7 07:10:02 2009
@@ -40,18 +40,18 @@
 public class LocaleUtils {
 
     /** Unmodifiable list of available locales. */
-    private static final List<Locale> cAvailableLocaleList;
+    //@GuardedBy("this")
+    private static List<Locale> cAvailableLocaleList; // lazily created by availableLocaleList()
+
     /** Unmodifiable set of available locales. */
     //@GuardedBy("this")
-    private static Set<Locale> cAvailableLocaleSet; // lazily created by availableLocaleSet()
+    private static Set<Locale> cAvailableLocaleSet;   // lazily created by availableLocaleSet()
+
     /** Unmodifiable map of language locales by country. */
     private static final Map<String, List<Locale>> cLanguagesByCountry = Collections.synchronizedMap(new HashMap<String, List<Locale>>());
+
     /** Unmodifiable map of country locales by language. */
     private static final Map<String, List<Locale>> cCountriesByLanguage = Collections.synchronizedMap(new HashMap<String, List<Locale>>());
-    static {
-        List<Locale> list = Arrays.asList(Locale.getAvailableLocales());
-        cAvailableLocaleList = Collections.unmodifiableList(list);
-    }
 
     /**
      * <p><code>LocaleUtils</code> instances should NOT be constructed in standard programming.
@@ -193,9 +193,24 @@
      * @return the unmodifiable list of available locales
      */
     public static List<Locale> 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<Locale> list = Arrays.asList(Locale.getAvailableLocales());
+            cAvailableLocaleList = Collections.unmodifiableList(list);
+        }
+    }
+
     //-----------------------------------------------------------------------
     /**
      * <p>Obtains an unmodifiable set of installed locales.</p>
@@ -207,13 +222,21 @@
      * @return the unmodifiable set of available locales
      */
     public static synchronized Set<Locale> availableLocaleSet() {
-        Set<Locale> set = cAvailableLocaleSet;
-        if (set == null) {
-            set = new HashSet<Locale>(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<Locale>(availableLocaleList()) );
         }
-        return set;
     }
 
     //-----------------------------------------------------------------------