whitespace fixups
[jSite2.git] / src / net / pterodactylus / util / beans / AbstractBean.java
index df6f789..7200324 100644 (file)
@@ -27,9 +27,8 @@ import java.util.List;
 
 /**
  * Abstract bean super class that contains property change listener management.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
  */
 public abstract class AbstractBean {
 
@@ -38,7 +37,7 @@ public abstract class AbstractBean {
 
        /**
         * Adds a property change listener.
-        * 
+        *
         * @param propertyChangeListener
         *            The property change listener to add
         */
@@ -48,7 +47,7 @@ public abstract class AbstractBean {
 
        /**
         * Removes a property change listener.
-        * 
+        *
         * @param propertyChangeListener
         *            The property change listener to remove
         */
@@ -58,7 +57,7 @@ public abstract class AbstractBean {
 
        /**
         * Notifies all listeners that a property has changed.
-        * 
+        *
         * @param property
         *            The name of the property
         * @param oldValue
@@ -68,10 +67,46 @@ public abstract class AbstractBean {
         */
        protected void firePropertyChange(String property, Object oldValue, Object newValue) {
                PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
-               for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
+               for (PropertyChangeListener propertyChangeListener : propertyChangeListeners) {
                        propertyChangeListener.propertyChange(propertyChangeEvent);
                }
 
        }
 
+       /**
+        * Fires a property change event if the two values are not equal.
+        *
+        * @param propertyName
+        *            The name of the property
+        * @param oldValue
+        *            The old value of the property
+        * @param newValue
+        *            The new value of the property
+        */
+       protected void fireIfPropertyChanged(String propertyName, Object oldValue, Object newValue) {
+               if (!equal(oldValue, newValue)) {
+                       firePropertyChange(propertyName, oldValue, newValue);
+               }
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Compares the two objects and returns whether they are equal according to
+        * {@link Object#equals(Object)}. This method takes <code>null</code>
+        * into account as a valid value for an object.
+        *
+        * @param first
+        *            The first object
+        * @param second
+        *            The second object
+        * @return <code>true</code> if the two objects are equal,
+        *         <code>false</code> otherwise
+        */
+       private boolean equal(Object first, Object second) {
+               return ((first == null) && (second == null)) || ((first != null) && first.equals(second)) || ((second != null) && second.equals(first));
+       }
+
 }