Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3E419200BB5 for ; Sat, 22 Oct 2016 22:05:00 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 3CA6B160ADB; Sat, 22 Oct 2016 20:05:00 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 8675D160AE0 for ; Sat, 22 Oct 2016 22:04:59 +0200 (CEST) Received: (qmail 1184 invoked by uid 500); 22 Oct 2016 20:04:58 -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 1074 invoked by uid 99); 22 Oct 2016 20:04:58 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Oct 2016 20:04:58 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id 5C5342C0D54 for ; Sat, 22 Oct 2016 20:04:58 +0000 (UTC) Date: Sat, 22 Oct 2016 20:04:58 +0000 (UTC) From: "Gary Gregory (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Updated] (LANG-1144) Multiple calls of org.apache.commons.lang3.concurrent.LazyInitializer.initialize() are possible MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Sat, 22 Oct 2016 20:05:00 -0000 [ https://issues.apache.org/jira/browse/LANG-1144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Gary Gregory updated LANG-1144: ------------------------------- Attachment: (was: commons-lang.patch) > Multiple calls of org.apache.commons.lang3.concurrent.LazyInitializer.initialize() are possible > ----------------------------------------------------------------------------------------------- > > Key: LANG-1144 > URL: https://issues.apache.org/jira/browse/LANG-1144 > Project: Commons Lang > Issue Type: Bug > Components: lang.concurrent.* > Affects Versions: 3.4 > Environment: Java 1.8 on Windows 7 x64 > Reporter: Waldemar Maier > Assignee: Gary Gregory > Attachments: 0001-LANG-1144-allow-nulls-as-return-value.patch, commons-lang.patch > > > It is possible to create a construct, that allows multiple calls of LazyInitializer.initialize, when calculations (which can be very expensive) return null as result. > In the Javadoc is described that the initialize method will be called only on the first access > {code:java} > /** > * Creates and initializes the object managed by this {@code > * LazyInitializer}. This method is called by {@link #get()} when the object > * is accessed for the first time. An implementation can focus on the > * creation of the object. No synchronization is needed, as this is already > * handled by {@code get()}. > * > * @return the managed data object > * @throws ConcurrentException if an error occurs during object creation > */ > protected abstract T initialize() throws ConcurrentException; > {code} > The Junit Test can be something like this: > *(fix can be appplied from attached patch-file)* > {code:java} > package edu.test; > import static org.junit.Assert.assertEquals; > import org.apache.commons.lang3.concurrent.ConcurrentException; > import org.apache.commons.lang3.concurrent.LazyInitializer; > import org.junit.Test; > public class LazyInitializerTest { > private int lazyinitCounter = 0; > private LazyInitializer lazyIinit = new LazyInitializer() { > @Override > protected Object initialize() throws ConcurrentException { > lazyinitCounter++; > return doSomeVeryExpensiveOperations(); > } > }; > > > private Object doSomeVeryExpensiveOperations() { > // do db calls > // do some complex math calculations > // the result of them all is null > return null; > } > > > @Test > public void testInitialization() throws Exception { > lazyIinit.get(); > lazyIinit.get(); > assertEquals("Multiple call of LazyInitializer#initialize", 1, lazyinitCounter); > } > } > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)