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
This commit is contained in:
Henri Yandell 2009-07-07 07:10:02 +00:00
parent 76555b1480
commit a4325e5559
1 changed files with 35 additions and 12 deletions

View File

@ -40,18 +40,18 @@ import java.util.Set;
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 @@ public class LocaleUtils {
* @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 @@ public class LocaleUtils {
* @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;
}
//-----------------------------------------------------------------------