b586353cf9b51fed6ed47374ccfebed796b8d581
[jSite2.git] / src / net / pterodactylus / util / beans / AbstractBean.java
1 /*
2  * jSite2 - AbstractBean.java
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.util.beans;
21
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 /**
29  * Abstract bean super class that contains property change listener management.
30  * 
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  * @version $Id$
33  */
34 public abstract class AbstractBean {
35
36         /** Property change listeners. */
37         private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
38
39         /**
40          * Adds a property change listener.
41          * 
42          * @param propertyChangeListener
43          *            The property change listener to add
44          */
45         public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
46                 propertyChangeListeners.add(propertyChangeListener);
47         }
48
49         /**
50          * Removes a property change listener.
51          * 
52          * @param propertyChangeListener
53          *            The property change listener to remove
54          */
55         public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
56                 propertyChangeListeners.remove(propertyChangeListener);
57         }
58
59         /**
60          * Notifies all listeners that a property has changed.
61          * 
62          * @param property
63          *            The name of the property
64          * @param oldValue
65          *            The old value of the property
66          * @param newValue
67          *            The new value of the property
68          */
69         protected void firePropertyChange(String property, Object oldValue, Object newValue) {
70                 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
71                 for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
72                         propertyChangeListener.propertyChange(propertyChangeEvent);
73                 }
74
75         }
76
77         /**
78          * Fires a property change event if the two values are not equal.
79          * 
80          * @param propertyName
81          *            The name of the property
82          * @param oldValue
83          *            The old value of the property
84          * @param newValue
85          *            The new value of the property
86          */
87         protected void fireIfPropertyChanged(String propertyName, Object oldValue, Object newValue) {
88                 if (!equal(oldValue, newValue)) {
89                         firePropertyChange(propertyName, oldValue, newValue);
90                 }
91         }
92
93         //
94         // PRIVATE METHODS
95         //
96
97         /**
98          * Compares the two objects and returns whether they are equal according to
99          * {@link Object#equals(Object)}. This method takes <code>null</code>
100          * into account as a valid value for an object.
101          * 
102          * @param first
103          *            The first object
104          * @param second
105          *            The second object
106          * @return <code>true</code> if the two objects are equal,
107          *         <code>false</code> otherwise
108          */
109         private boolean equal(Object first, Object second) {
110                 return ((first == null) && (second == null)) || ((first != null) && first.equals(second)) || ((second != null) && second.equals(first));
111         }
112
113 }