From a4325e55597da54e3400ac5704f4437d637771d3 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Tue, 7 Jul 2009 07:10:02 +0000 Subject: [PATCH] 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 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@791726 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/LocaleUtils.java | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/java/org/apache/commons/lang/LocaleUtils.java b/src/java/org/apache/commons/lang/LocaleUtils.java index 8635b2e8f..553baeeb4 100644 --- a/src/java/org/apache/commons/lang/LocaleUtils.java +++ b/src/java/org/apache/commons/lang/LocaleUtils.java @@ -40,18 +40,18 @@ import java.util.Set; public class LocaleUtils { /** Unmodifiable list of available locales. */ - private static final List cAvailableLocaleList; + //@GuardedBy("this") + private static List cAvailableLocaleList; // lazily created by availableLocaleList() + /** Unmodifiable set of available locales. */ //@GuardedBy("this") - private static Set cAvailableLocaleSet; // lazily created by availableLocaleSet() + 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); - } /** *

LocaleUtils instances should NOT be constructed in standard programming. @@ -193,9 +193,24 @@ public class LocaleUtils { * @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); + } + } + //----------------------------------------------------------------------- /** *

Obtains an unmodifiable set of installed locales.

@@ -207,13 +222,21 @@ public class LocaleUtils { * @return the unmodifiable set of available locales */ public static synchronized 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; } //-----------------------------------------------------------------------