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>
33 public abstract class AbstractBean {
35 /** Property change listeners. */
36 private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
39 * Adds a property change listener.
41 * @param propertyChangeListener
42 * The property change listener to add
44 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
45 propertyChangeListeners.add(propertyChangeListener);
49 * Removes a property change listener.
51 * @param propertyChangeListener
52 * The property change listener to remove
54 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
55 propertyChangeListeners.remove(propertyChangeListener);
59 * Notifies all listeners that a property has changed.
62 * The name of the property
64 * The old value of the property
66 * The new value of the property
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);
77 * Fires a property change event if the two values are not equal.
80 * The name of the property
82 * The old value of the property
84 * The new value of the property
86 protected void fireIfPropertyChanged(String propertyName, Object oldValue, Object newValue) {
87 if (!equal(oldValue, newValue)) {
88 firePropertyChange(propertyName, oldValue, newValue);
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.
105 * @return <code>true</code> if the two objects are equal,
106 * <code>false</code> otherwise
108 private boolean equal(Object first, Object second) {
109 return ((first == null) && (second == null)) || ((first != null) && first.equals(second)) || ((second != null) && second.equals(first));