2 * jSite2 - AbstractBean.java
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.util.beans;
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;
29 * Abstract bean super class that contains property change listener management.
31 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34 public abstract class AbstractBean {
36 /** Property change listeners. */
37 private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
40 * Adds a property change listener.
42 * @param propertyChangeListener
43 * The property change listener to add
45 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
46 propertyChangeListeners.add(propertyChangeListener);
50 * Removes a property change listener.
52 * @param propertyChangeListener
53 * The property change listener to remove
55 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
56 propertyChangeListeners.remove(propertyChangeListener);
60 * Notifies all listeners that a property has changed.
63 * The name of the property
65 * The old value of the property
67 * The new value of the property
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);
78 * Fires a property change event if the two values are not equal.
81 * The name of the property
83 * The old value of the property
85 * The new value of the property
87 protected void fireIfPropertyChanged(String propertyName, Object oldValue, Object newValue) {
88 if (!equal(oldValue, newValue)) {
89 firePropertyChange(propertyName, oldValue, newValue);
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.
106 * @return <code>true</code> if the two objects are equal,
107 * <code>false</code> otherwise
109 private boolean equal(Object first, Object second) {
110 return ((first == null) && (second == null)) || ((first != null) && first.equals(second)) || ((second != null) && second.equals(first));