When trying to adopt the style of implementing a listener using anonymous or nested class in order to hide the notification methods for other uses than listening (i.e. I don't want anybody to be able to call actionPerformed). For example from java action listener: implements vs anonymous class:
public MyClass() {
myButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//doSomething
}
});
}
The question is if theres an elegant way to remove the listener again using this idiom? I figured out that the instantiation of ActionListener
does not produce equal objects every time so Collection.remove()
won't remove the originally added object.
In order to be considered equal the listeners should have the same outer this. To implement equals I would need to get hold of outer this for the other object. So it will go something like this (which I find a little bit clumpsy):
interface MyListener {
Object getOuter();
}
abstract class MyActionListener extends ActionListener
implement MyListener {
}
public MyClass() {
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// doSomething on MyClass.this
}
public Object getOuter() {
return MyClass.this;
}
public boolean equals(Object other)
{
if( other instanceof MyListener )
{
return getOuter() == other.getOuter();
}
return super.equals(other);
});
}
}
Or will I be forced to keep the ActionListener object as a (private) member of the outer class?