whitespace fixups
[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  */
33 public abstract class AbstractBean {
34
35         /** Property change listeners. */
36         private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
37
38         /**
39          * Adds a property change listener.
40          *
41          * @param propertyChangeListener
42          *            The property change listener to add
43          */
44         public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
45                 propertyChangeListeners.add(propertyChangeListener);
46         }
47
48         /**
49          * Removes a property change listener.
50          *
51          * @param propertyChangeListener
52          *            The property change listener to remove
53          */
54         public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
55                 propertyChangeListeners.remove(propertyChangeListener);
56         }
57
58         /**
59          * Notifies all listeners that a property has changed.
60          *
61          * @param property
62          *            The name of the property
63          * @param oldValue
64          *            The old value of the property
65          * @param newValue
66          *            The new value of the property
67          */
68         protected void firePropertyChange(String property, Object oldValue, Object newValue) {
69                 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
70                 for (PropertyChangeListener propertyChangeListener : propertyChangeListeners) {
71                         propertyChangeListener.propertyChange(propertyChangeEvent);
72                 }
73
74         }
75
76         /**
77          * Fires a property change event if the two values are not equal.
78          *
79          * @param propertyName
80          *            The name of the property
81          * @param oldValue
82          *            The old value of the property
83          * @param newValue
84          *            The new value of the property
85          */
86         protected void fireIfPropertyChanged(String propertyName, Object oldValue, Object newValue) {
87                 if (!equal(oldValue, newValue)) {
88                         firePropertyChange(propertyName, oldValue, newValue);
89                 }
90         }
91
92         //
93         // PRIVATE METHODS
94         //
95
96         /**
97          * Compares the two objects and returns whether they are equal according to
98          * {@link Object#equals(Object)}. This method takes <code>null</code>
99          * into account as a valid value for an object.
100          *
101          * @param first
102          *            The first object
103          * @param second
104          *            The second object
105          * @return <code>true</code> if the two objects are equal,
106          *         <code>false</code> otherwise
107          */
108         private boolean equal(Object first, Object second) {
109                 return ((first == null) && (second == null)) || ((first != null) && first.equals(second)) || ((second != null) && second.equals(first));
110         }
111
112 }