Class ALcdWeakPropertyChangeListener<T>
- Type Parameters:
T
- The type of the short-lived object
- All Implemented Interfaces:
PropertyChangeListener
,EventListener
A PropertyChangeListener
implementation which only keeps a WeakReference
to the object it
wants to update when property changes are received. This avoids memory leaks, even without removing the
listener.
The class javadoc of ALcdWeakModelListener
contains an example of a weak listener,
and some rules on the usage of weak listeners.
This particular listener has two extra rules compared to the general rules of weak listeners:
- The object to listen to must implement
ILcdPropertyChangeSource
. - Do not register the listener for a particular property, so avoid
addPropertyChangeListener(String, ...)
- Since:
- 2015.1
- See Also:
-
Constructor Summary
ModifierConstructorDescriptionprotected
ALcdWeakPropertyChangeListener
(T aObjectToModify) Creates a newALcdWeakPropertyChangeListener
instance which can updateaObjectToModify
when it receives events. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
protected abstract void
propertyChangeImpl
(T aToModify, PropertyChangeEvent aPropertyChangeEvent) This method is called each time aPropertyChangeEvent
is received by this listener, and the short-living object to modify passed in the constructor of this listener is not yet GC-ed.
-
Constructor Details
-
ALcdWeakPropertyChangeListener
Creates a new
ALcdWeakPropertyChangeListener
instance which can updateaObjectToModify
when it receives events.Warning: this listener can only be registered to a
ILcdPropertyChangeSource
. The listener will throw exceptions when it receivesPropertyChangeEvent
instances where the source is not anILcdPropertyChangeSource
instance.- Parameters:
aObjectToModify
- The object you want to modify when aPropertyChangeEvent
is received. This is a short-lived object, while the listener will be attached to a long-livingILcdPropertyChangeSource
object.
-
-
Method Details
-
propertyChange
This method will check whether the short-living object is still available. If it is, this method will pass the short-living object and the event to the
propertyChangeImpl(Object, java.beans.PropertyChangeEvent)
method. If the short-living object is already garbage collected, this method will de-register the listener from the source.- Specified by:
propertyChange
in interfacePropertyChangeListener
- See Also:
-
propertyChangeImpl
This method is called each time a
PropertyChangeEvent
is received by this listener, and the short-living object to modify passed in the constructor of this listener is not yet GC-ed.- Parameters:
aToModify
- The object to modify, as passed in the constructor of this class.aPropertyChangeEvent
- The received event
-