History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GROOVY-2786
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Andres Almiray
Reporter: Mingfai Ma
Votes: 0
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
groovy

ObservableMap doesn't fire Property Change Event upon removal

Created: 27/Apr/08 04:30 PM   Updated: Wednesday 01:40 PM
Component/s: None
Affects Version/s: 1.5.6
Fix Version/s: 1.6-beta-2

Time Tracking:
Not Specified


 Description  « Hide
Taking the example from doc

def map = [one: 1, two: 2] as ObservableMap
map.addPropertyChangeListener({evt -> println "${evt.propertyName}: ${evt.oldValue} -> ${evt.newValue}"} as PropertyChangeListener)

map.remove("one")

nothing is printed. Other operations like put, replace element do work without problem.

map.clear() also do nth. as putAll() does fire events, i think clear() should do so.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Alex Peters - 28/Apr/08 09:42 AM
I took a look at the remove and clear method. they do not fire any property change event.
but is this really a bug or should more documentation explain the behaviour?

PropertyChangeEvent requires an oldValue and newValue.
When i remove a key from the map what values should the event carry?


Guillaume Laforge - 28/Apr/08 09:52 AM
I think it's designed that way.
But Andres may shade some more light on this?

Andres Almiray - 28/Apr/08 12:45 PM
As Guillaume noted, it is designed that way, the intention of ObservableMap is to mimic an observable bean, you can't remove properties from a bean, can you?

On the other hand I see the benefit of having OM publish events when an element or all elements are removed, but the event triggered should not be a PropertyChangeEvent. This may be confusing as OM currently sends a PCE when a 'new" property is "added", so it should trigger a PropertyAddedEvent instead, but in a sense OM holds every property possible from the start (again if trying to mimic an observable bean).

Two questions remain then:
a) should PropertyRemovedEvent/PropertiesClearedEvent be added to OM ?
b) if a) should PropertyAddedEvent be triggered when a "new" property is added to the map ?


Mingfai Ma - 28/Apr/08 06:17 PM
I guess we agree it's necessary to fire removal event. Otherwise, the Observable Map is somehow useless.

>PropertyChangeEvent requires an oldValue and newValue.
>When i remove a key from the map what values should the event carry?

If an entry is removed, I expect it to return:
assert( evt.newValue == null )


Andres Almiray - 28/Apr/08 06:45 PM
Alright, then I think the best course of action would be to subclass PCE to add a type [add|remove|change|clear] and continue using PropertyChangeListener. If a client would like to know more about the event then it can read the type field.

Andres Almiray - 14/May/08 01:24 PM
Added the following detail to fired events
  • ObservableMap.PropertyAddedEvent - a new property is added to the map
  • ObservableMap.PropertyRemovedEvent - a property is removed from the map
  • ObservableMap.PropertyUpdatedEvent - a property changes value (same as regular PropertyChangeEvent)
  • ObservableMap.PropertyClearedEvent - all properties have been removed from the map
  • ObservableMap.MultiPropertyAddedEvent - triggered by calling map.putAll(), contains Added|Updated events

Andres Almiray - 14/May/08 01:40 PM
Backported to 1.5.x