Fwd: API REVIEW: Weak event handler

Lubomir Nerad lubomir.nerad at oracle.com
Tue Jun 12 08:15:02 PDT 2012


Hi Sven,

> Any reason why
>
> public static<T extends Event>  WeakEventHandler<T>
> create(EventHandler<T>  eventHandler) { ... }
>
> is not
>
> public static<T extends Event>  EventHandler<T>  create(EventHandler<T>
> eventHandler) { ... }
>
> Is there a special use case why the usaer of the API should know about
> the implementation?

I don't see any harm specifying that we return WeakEventHandler from the 
method. The WeakEventHandler is to be part of the public API and anybody 
who will be creating some container for event handlers will need to be 
aware of it.

>  From my  point of view just hide it behind a
> static method call on EventHandler itself..

The EventHandler is an interface.

> Regards
>
> Sven

Thanks,
Lubo

> On Tue, Jun 12, 2012 at 1:47 PM, Lubomir Nerad<lubomir.nerad at oracle.com>  wrote:
>> Hi All,
>>
>> there is a request to add a WeakEventHandler to the API
>> (http://javafx-jira.kenai.com/browse/RT-13706). This will be a simple
>> wrapper for the target event handler to which it will delegate the handle
>> calls. It will reference the target weakly and so it won't prevent it from
>> being garbage collected.
>>
>> My proposal for this class is:
>>
>> package javafx.event;
>>
>> public final class WeakEventHandler<T extends Event>
>>         extends WeakReference<EventHandler<T>>
>>         implements EventHandler<T>  {
>>
>>     private WeakEventHandler(EventHandler<T>  eventHandler) { ... }
>>
>>     @Override public void handle(final T event) { ... }
>>
>>     public static<T extends Event>  WeakEventHandler<T>
>> create(EventHandler<T>  eventHandler) { ... }
>> }
>>
>> I decided to make this class both an EventHandler (implements) and a
>> WeakReference (extends). Mainly I needed to have some way how to test from
>> event handler containers whether the target event handler for a stored
>> wrapper has been garbage collected and so the wrapper can be removed as
>> well. Extending WeakReference adds the required method (WeakReference.get())
>> and saves some object instances. It also brings the clear method, which can
>> simplify testing of this feature.
>>
>> WeakEventHandler instances will be created through the create factory
>> method. This is to avoid repetition of the event type during event handler
>> registration.
>>
>> So instead of:
>> node.setOnMousePressed(new WeakEventHandler<MouseEvent>(new
>> EventHandler<MouseEvent>() { ... }));
>>
>> we can use:
>> node.setOnMousePressed(WeakEventHandler.create(new
>> EventHandler<MouseEvent>() { ... }));
>>
>> I also considered to define the WeakEventHandler as an interface which
>> extends EventHandler, but adds no additional methods. Then leave it to event
>> handler containers to reference such event handlers weakly. This has very
>> simple and direct usage, but also its own problems. We can discuss it
>> further if you find this solution preferred to the wrapper approach.
>>
>> Thanks,
>> Lubo
>>
>
>
> --
> Sven Reimers
>
> * Senior Expert Software Architect
> * NetBeans Dream Team Member: http://dreamteam.netbeans.org
> * NetBeans Governance Board Member: http://netbeans.org/about/os/governance.html
> * Community Leader  NetBeans: http://community.java.net/netbeans
>                                Desktop Java:
> http://community.java.net/javadesktop
> * Duke's Choice Award Winner 2009
> * Blog: http://nbguru.blogspot.com
>
> * XING: https://www.xing.com/profile/Sven_Reimers8
> * LinkedIn: http://www.linkedin.com/in/svenreimers
>
> Join the NetBeans Groups:
> * XING: http://www.xing.com/group-20148.82db20
> * NUGM: http://haug-server.dyndns.org/display/NUGM/Home
> * LinkedIn: http://www.linkedin.com/groups?gid=1860468
>                     http://www.linkedin.com/groups?gid=107402
>                     http://www.linkedin.com/groups?gid=1684717
> * Oracle: https://mix.oracle.com/groups/18497
>
>


More information about the openjfx-dev mailing list