commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Oliver Heger (JIRA)" <>
Subject [jira] Commented: (LANG-496) A generic implementation of the Lazy initialization pattern
Date Tue, 07 Jul 2009 19:34:14 GMT


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}}):

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);

Now we can create an initializer class:

private static class LocaleInitializer extends LazyInitializer<LocaleData> {
    protected LocaleData initialize() {
        return new LocaleData();

An object of this class is declared as static field in {{LocaleUtils}} instead of the list
and the set fields, e.g.

private static final LocaleInitializer localeInitializer = new LocaleInitializer();

Then the methods for accessing the list or the set simply query this initializer object, for

public static Set<Locale> availableLocaleSet() {
    return localeInitializer.get().cAvailableLocaleSet;

Note that no synchronization is required as this is handled by the {{LazyInitializer}} base

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:
>             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.

View raw message