Return-Path: Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: (qmail 87657 invoked from network); 7 Jul 2009 19:34:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 7 Jul 2009 19:34:31 -0000 Received: (qmail 5759 invoked by uid 500); 7 Jul 2009 19:34:40 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 5624 invoked by uid 500); 7 Jul 2009 19:34:39 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 5411 invoked by uid 99); 7 Jul 2009 19:34:39 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Jul 2009 19:34:39 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Jul 2009 19:34:36 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id ECCFF234C1F2 for ; Tue, 7 Jul 2009 12:34:14 -0700 (PDT) Message-ID: <1863785415.1246995254969.JavaMail.jira@brutus> Date: Tue, 7 Jul 2009 12:34:14 -0700 (PDT) From: "Oliver Heger (JIRA)" To: issues@commons.apache.org Subject: [jira] Commented: (LANG-496) A generic implementation of the Lazy initialization pattern In-Reply-To: <1499083569.1238526650477.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 X-Virus-Checked: Checked by ClamAV on apache.org [ 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 cAvailableLocaleList; final Set cAvailableLocaleSet; public LocaleData() { List 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 { 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 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.