Project

General

Profile

bug #7872

use firePropertyChange only if the value has changed

Added by Andreas Kohlbecker 7 months ago. Updated 7 months ago.

Status:
Worksforme
Priority:
New
Category:
cdmlib
Target version:
-
Start date:
10/26/2018
Due date:
% Done:

0%

Severity:
normal
Found in Version:

Description

this is an example from Team where a PropertyChangeEvent is fired even if this.nomenclaturalTitle.equals(nomenclaturalTitle) == true

Before firing the event an equality check should be done to prevent from sending events unnecessarily.

public void setNomenclaturalTitle(String nomenclaturalTitle, boolean protectedNomenclaturalTitleCache) {
        firePropertyChange("nomenclaturalTitle", this.nomenclaturalTitle, nomenclaturalTitle);
        this.nomenclaturalTitle = nomenclaturalTitle == "" ? null: nomenclaturalTitle;
        this.protectedNomenclaturalTitleCache = protectedNomenclaturalTitleCache;
    }

History

#1 Updated by Andreas Müller 7 months ago

  • Status changed from New to Resolved
  • Assignee changed from Andreas Müller to Andreas Kohlbecker
  • Target version changed from Unassigned CDM tickets to Release 5.5

If you follow the firePropertyChange call you come to PropertyChangeSupport

    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));
        }
    }

so the call is not further propagated and therefore not problematic.

I suggest to close with worksforme

#2 Updated by Andreas Kohlbecker 7 months ago

  • Status changed from Resolved to Worksforme
  • Target version deleted (Release 5.5)

great and agreed!

one hit before closing this as "worksforme", just in case you missed this out so far:

Since java 1.7 these awkward null save checks can elegantly be replaced by Objects.equals(), so

if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {

can become

if (!Objects.equals(oldValue, newValue)) {

#3 Updated by Andreas Müller 7 months ago

Andreas Kohlbecker wrote:

great and agreed!

one hit before closing this as "worksforme", just in case you missed this out so far:

Since java 1.7 these awkward null save checks can elegantly be replaced by Objects.equals(), so

if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {

can become

if (!Objects.equals(oldValue, newValue)) {

Ahh, didn't know this. I am always using CdmUtils.nullSafeEquals(o,o)

Also available in: Atom PDF

Add picture from clipboard (Maximum size: 40 MB)