org.jitsi.utils.event.PropertyChangeNotifier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jitsi-utils Show documentation
Show all versions of jitsi-utils Show documentation
A set of basic utilities used in Jitsi projects
/*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.utils.event;
import java.beans.*;
import java.util.*;
import org.jitsi.utils.logging.*;
/**
* Represents a source of PropertyChangeEvents which notifies
* PropertyChangeListeners about changes in the values of properties.
*
* @author Lyubomir Marinov
*/
public class PropertyChangeNotifier
{
/**
* The Logger used by the PropertyChangeNotifier class and
* its instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(PropertyChangeNotifier.class);
/**
* The list of PropertyChangeListeners interested in and notified
* about changes in the values of the properties of this
* PropertyChangeNotifier.
*/
private final List listeners
= new ArrayList();
/**
* Initializes a new PropertyChangeNotifier instance.
*/
public PropertyChangeNotifier()
{
}
/**
* Adds a specific PropertyChangeListener to the list of listeners
* interested in and notified about changes in the values of the properties
* of this PropertyChangeNotifier.
*
* @param listener a PropertyChangeListener to be notified about
* changes in the values of the properties of this
* PropertyChangeNotifier. If the specified listener is already in
* the list of interested listeners (i.e. it has been previously added), it
* is not added again.
*/
public void addPropertyChangeListener(PropertyChangeListener listener)
{
if (listener == null)
{
if (logger.isDebugEnabled())
{
logger.debug(
"The specified argument listener is null"
+ " and that does not make sense.");
}
}
else
{
synchronized (listeners)
{
if (!listeners.contains(listener))
listeners.add(listener);
}
}
}
/**
* Fires a new PropertyChangeEvent to the
* PropertyChangeListeners registered with this
* PropertyChangeNotifier in order to notify about a change in the
* value of a specific property which had its old value modified to a
* specific new value. PropertyChangeNotifier does not check
* whether the specified oldValue and newValue are indeed
* different.
*
* @param property the name of the property of this
* PropertyChangeNotifier which had its value changed
* @param oldValue the value of the property with the specified name before
* the change
* @param newValue the value of the property with the specified name after
* the change
*/
protected void firePropertyChange(
String property,
Object oldValue, Object newValue)
{
PropertyChangeListener[] ls;
synchronized (listeners)
{
ls
= listeners.toArray(
new PropertyChangeListener[listeners.size()]);
}
if (ls.length != 0)
{
PropertyChangeEvent ev
= new PropertyChangeEvent(
getPropertyChangeSource(property, oldValue, newValue),
property,
oldValue, newValue);
for (PropertyChangeListener l : ls)
{
try
{
l.propertyChange(ev);
}
catch (Throwable t)
{
if (t instanceof InterruptedException)
{
Thread.currentThread().interrupt();
}
else if (t instanceof ThreadDeath)
{
throw (ThreadDeath) t;
}
else
{
logger.error(
"A PropertyChangeListener threw an exception"
+ " while handling a PropertyChangeEvent.",
t);
}
}
}
}
}
/**
* Gets the Object to be reported as the source of a new
* PropertyChangeEvent which is to notify the
* PropertyChangeListeners registered with this
* PropertyChangeNotifier about the change in the value of a
* property with a specific name from a specific old value to a specific new
* value.
*
* @param property the name of the property which had its value changed from
* the specified old value to the specified new value
* @param oldValue the value of the property with the specified name before
* the change
* @param newValue the value of the property with the specified name after
* the change
* @return the Object to be reported as the source of the new
* PropertyChangeEvent which is to notify the
* PropertyChangeListeners registered with this
* PropertyChangeNotifier about the change in the value of the
* property with the specified name from the specified old value to the
* specified new value
*/
protected Object getPropertyChangeSource(
String property,
Object oldValue, Object newValue)
{
return this;
}
/**
* Removes a specific PropertyChangeListener from the list of
* listeners interested in and notified about changes in the values of the
* properties of this PropertyChangeNotifer.
*
* @param listener a PropertyChangeListener to no longer be
* notified about changes in the values of the properties of this
* PropertyChangeNotifier
*/
public void removePropertyChangeListener(PropertyChangeListener listener)
{
if (listener != null)
{
synchronized (listeners)
{
listeners.remove(listener);
}
}
}
}