[
https://issues.apache.org/jira/browse/LANG-496?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12728318#action_12728318
]
Oliver Heger commented on LANG-496:
-----------------------------------
Using a LazyInitializer should be straightforward. The only complication is
that for this use case two objects - the list and the set - are involved, while
the initializer only creates a single one. So we would have to create a class
that combines these two objects (as a private nested class in {{LocaleUtils}}):
{code}
private static class LocaleData {
final List<Locale> cAvailableLocaleList;
final Set<Locale> cAvailableLocaleSet;
public LocaleData() {
List<Locale> list = Arrays.asList(Locale.getAvailableLocales());
cAvailableLocaleList = Collections.unmodifiableList(list);
cAvailableLocaleSet = Collections.unmodifiableSet(cAvailableLocaleList);
}
}
{code}
Now we can create an initializer class:
{code}
private static class LocaleInitializer extends LazyInitializer<LocaleData> {
protected LocaleData initialize() {
return new LocaleData();
}
}
{code}
An object of this class is declared as static field in {{LocaleUtils}} instead
of the list and the set fields, e.g.
{code}
private static final LocaleInitializer localeInitializer = new
LocaleInitializer();
{code}
Then the methods for accessing the list or the set simply query this
initializer object, for instance:
{code}
public static Set<Locale> availableLocaleSet() {
return localeInitializer.get().cAvailableLocaleSet;
}
{code}
Note that no synchronization is required as this is handled by the
{{LazyInitializer}} base class.
As a side note: I think in the current implementation of {{LocaleUtils}} the
list and the set have to be declared as *volatile*. Otherwise changes to these
fields are not guaranteed to be visible for other threads immediately.
> A generic implementation of the Lazy initialization pattern
> -----------------------------------------------------------
>
> Key: LANG-496
> URL: https://issues.apache.org/jira/browse/LANG-496
> Project: Commons Lang
> Issue Type: New Feature
> Reporter: Oliver Heger
> Fix For: 3.0
>
> Attachments: LazyInitializer.patch
>
>
> This is a fully functional implementation of the double-check idiom for lazy
> initialization of an instance field as discussed in Joshua Bloch's "Effective
> Java".
> If there is interest, this could be the first element of a set of helper
> classes related to concurrent programming.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.