[ 
https://issues.apache.org/jira/browse/LANG-1716?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17774211#comment-17774211
 ] 

Benjamin Confino commented on LANG-1716:
----------------------------------------

I don't think AutoCloseable is the correct choice. AutoCloseable objects are 
disposed at the end of the try block they are used. With LazyInit I believe the 
most common use case would be for the object to remain after its used so it can 
be reused every time it is needed, then it would be disposed just once when it 
wont be needed again, such as on application shutdown.

Regarding your more general point you made on the github pull request about 
extending LazyInitializer instead of making a new class. That can be done. I 
tested locally by replacing LazyInitalizer with a slightly modified version of 
my draft. It had a no-args constructor that set the supplier and consumer to 
null, and had an empty _initialize()_ method that is only called if the 
supplier is null. All the tests passed. If you'd prefer it this way I am happy 
to make the change. 


> Create a varient of LazyInitializer whch can dispose the contained object
> -------------------------------------------------------------------------
>
>                 Key: LANG-1716
>                 URL: https://issues.apache.org/jira/browse/LANG-1716
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.concurrent.*
>    Affects Versions: 4.0, 3.x
>            Reporter: Benjamin Confino
>            Priority: Minor
>
> Hello everyone.
> Currently there is no clean way to dispose of an object created with a 
> LazyInitializer. Either you callĀ _initializer.get().dispose();_ but if you've 
> never used _initializer_ before this will invoke a potentially expensive 
> routine just so the object can be immediately disposed.
> Or you cache the result of _initializer.get();_ and then dispose the object 
> you have cached. This means the developer has to worry about concurrency 
> issues when disposing the object, and looses all the benefits of 
> LazyInitializer having a built in cache.
> To fix this I propose that a new variant of LazyInitializer is created, this 
> variant will allow you to call a dispose method which will close the 
> LazyInitializer object and then it will, if the wrapped object has been 
> initialized, run whatever disposal routine is provided.
> I have created a pull request with a potential implementation here: 
> [https://github.com/apache/commons-lang/pull/1119] 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to