commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Oliver Heger (JIRA)" <>
Subject [jira] Commented: (LANG-580) Add Event Support Utilities
Date Wed, 06 Jan 2010 19:24:54 GMT


Oliver Heger commented on LANG-580:

I like the idea to add support for event listeners to [lang] because it can be tricky to implement
this in a thread-safe and performant way.

That said I am not sure how much benefit the proposed solution would actually offer. IMO support
for event listeners has two aspects: storing the registered listeners (i.e. the implementation
of the {{addListener()}} and {{removeListener()}} methods, and firing events (i.e. {{fireXXX()}}
methods). The {{EventSupport}} interface and the {{AbstractEventSupport}} class mainly focus
on the former.

The JDK contains at least two classes I am aware of that are well suited for managing event
* {{javax.swing.event.EventListenerList}} is used by Swing and is a bit more complicated because
it can deal with event listeners of multiple types.
* {{java.util.concurrent.CopyOnWriteArrayList}} is from its semantics a proper choice for
storing event listeners.

So {{AbstractEventSupport}} would probably be a thin wrapper over one of these classes. Would
this really provide benefit for users? If we could add some support for the {{fire()}} methods,
the answer would be a clear yes, but I don't know how this can be achieved (it would probably
be easier if Java had support for closures).

> Add Event Support Utilities
> ---------------------------
>                 Key: LANG-580
>                 URL:
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: General
>    Affects Versions: 3.0
>         Environment: Java SE 5.0+
>            Reporter: Michael Wooten
>            Priority: Minor
>             Fix For: 3.0
>   Original Estimate: 96h
>  Remaining Estimate: 96h
> I would like to propose some support be added to Lang for basic event handling. This
would be based on the way that PropertyChangeSupport can be used to add and remove listeners
and post events. 
> Add interface EventSupport<L extends EventListener> 
> addListener(L listener)
> The signature for the method that can add a listener of some subtype of EventListener
> removeListener(L listener)
> The signature for the method that can remove a listener of some subtype of EventListener
> Add class AbstractEventSupport implements EventSupport<L>, Iterable<L>
> AbstractEventSupport(Object eventSource)
> Constructs a new AbstractEventSupport object and associates it with the object that will
be used as the source of all events (much like PropertyChangeSupport).
> addListener(L)
> An implementation that adds a listener to an internal collection.
> removeListener(L)
> An implementation that removes a listener from an internal collection.
> iterator()
> Returns an iterator over the attached listeners.
> getSource()
> Returns a reference to the source object of all events.
> The best way to describe this would be to demonstrate an example of how it can be used.
> public class ButtonPressedEventSupport extends AbstractEventSupport<ButtonPressedListener>
>     public ButtonPressedEventSupport(Object source) { super(source); }
>     public void fireButtonPressed(Button button) {
>         ButtonPressedEvent bpe = new ButtonPressedEvent(getSource(), button);
>         for (ButtonPressedListener listener : this)
>         {
>             listener.buttonPressed(bpe);
>         }
>     }
> }
> public class MyWindow implements EventSupport<ButtonPressedListener> {
>      private final ButtonPressedEventSupport buttonPressedEventSupport;
>      public MyWindow { buttonPressedEventSupport = new ButtonPressedEventSupport(this);
>      public void addListener(ButtonPressedListener listener) { buttonPressedEventSupport.addListener(listener);
>      public void removeListener(ButtonPressedListener listener) { buttonPressedEventSupport.removeListener(listener);
>      ...
>     private void onDetectButtonPressed(Button button) {
>         buttonPressedEventSupport.fireButtonPressed(button);
>     }
> }
> I haven't compiled the above code. It's just an example of how these classes could be
used so that you're not constantly rewriting the code and interfaces for adding and removing
listeners, and it provides a fairly easy method of creating methods to fire events.

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message