2
votes

Why method firePropertyChange (String propertyName, Object oldValue, Object newValue) in class PropertyChangeSupport don't check that old and new value can be null at the same time?

2
Do you mean "why doesn't firePropertyChange(String,Object,Object) handle the case of both objects being equal to null the same way it handles the case of objects being Object#equals"? - Ramon

2 Answers

0
votes

PropertyChangeEvent javadoc may give some clue about it:

Null values may be provided for the old and the new values if their true values are not known.

An event source may send a null object as the name to indicate that an arbitrary set of if its properties have changed. In this case the old and new values should also be null.

So it looks like having oldValue==null and newValue==null can have some special meaning when source==null as well. Because of this is may want to always propagate the change when both values are nulls even though they are the same.

0
votes

I am not sure what do you mean, but here is actual code that is definitelly more clean than me trying to explain it :)

/**
     * Reports a bound property update to listeners
     * that have been registered to track updates of
     * all properties or a property with the specified name.
     * <p>
     * No event is fired if old and new values are equal and non-null.
     * <p>
     * This is merely a convenience wrapper around the more general
     * {@link #firePropertyChange(PropertyChangeEvent)} method.
     *
     * @param propertyName  the programmatic name of the property that was changed
     * @param oldValue      the old value of the property
     * @param newValue      the new value of the property
     */
    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
            firePropertyChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
        }
    }