EDU.oswego.cs.dl.util.concurrent
public class VetoableChangeMulticaster extends Object implements Serializable
Sample usage.
class Thing {
protected Color myColor = Color.red; // an example property
protected boolean changePending; // track whether in midst of change
// vetoable listeners:
protected VetoableChangeMulticaster vetoers =
new VetoableChangeMulticaster(this);
// Possibly also some ordinary listeners:
protected PropertyChangeMulticaster listeners =
new PropertyChangeMulticaster(this);
// registration methods, including:
void addVetoer(VetoableChangeListener l) {
// Use the `ifAbsent' version to avoid duplicate notifications
vetoers.addVetoableChangeListenerIfAbsent(l);
}
public synchronized Color getColor() { // accessor
return myColor;
}
// Simple transactional control for vetos
public void setColor(int newColor) throws PropertyVetoException {
Color oldColor = prepareSetColor(newColor);
try {
vetoers.fireVetoableChange("color", oldColor, newColor);
commitColor(newColor);
listeners.firePropertyChange("color", oldColor, newColor);
}
catch(PropertyVetoException ex) {
abortSetColor();
throw ex;
}
}
// Called on entry to proposed vetoable change from setColor.
// Throws exception if there is already another change in progress.
// Returns current color
synchronized int prepareSetColor(Color c) throws PropertyVetoException {
// only support one transaction at a time
if (changePending)
throw new PropertyVetoException("Concurrent modification");
// (Could alternatively wait out other transactions via
// a wait/notify construction based on changePending.)
// perhaps some other screenings, like:
else if (c == null)
throw new PropertyVetoException("Cannot change color to Null");
else {
changePending = true;
return myColor;
}
}
synchronized void commitColor(Color newColor) {
myColor = newColor;
changePending = false;
}
synchronized void abortSetColor() {
changePending = false;
}
}
| Field Summary | |
|---|---|
| protected HashMap | children
HashMap for managing listeners for specific properties.
|
| protected VetoableChangeListener[] | listeners
The array of listeners. |
| protected Object | source
The object to be provided as the "source" for any generated events. |
| Constructor Summary | |
|---|---|
| VetoableChangeMulticaster(Object sourceBean)
Constructs a VetoableChangeMulticaster object.
| |
| Method Summary | |
|---|---|
| void | addVetoableChangeListener(VetoableChangeListener listener)
Add a VetoableChangeListener to the listener list.
|
| void | addVetoableChangeListener(String propertyName, VetoableChangeListener listener)
Add a VetoableChangeListener for a specific property. |
| void | addVetoableChangeListenerIfAbsent(VetoableChangeListener listener)
Add a PropertyChangeListener to the listener list if it is
not already present.
|
| void | addVetoableChangeListenerIfAbsent(String propertyName, VetoableChangeListener listener)
Add a VetoableChangeListener for a specific property, if it is not
already registered. |
| void | fireVetoableChange(String propertyName, Object oldValue, Object newValue)
Report a vetoable property update to any registered listeners.
|
| void | fireVetoableChange(String propertyName, int oldValue, int newValue)
Report a vetoable property update to any registered listeners.
|
| void | fireVetoableChange(String propertyName, boolean oldValue, boolean newValue)
Report a vetoable property update to any registered listeners.
|
| void | fireVetoableChange(PropertyChangeEvent evt)
Report a vetoable property update to any registered listeners.
|
| protected VetoableChangeMulticaster | getChild(String propertyName)
Return the child associated with property, or null if no such
|
| boolean | hasListeners(String propertyName)
Check if there are any listeners for a specific property.
|
| protected void | multicast(PropertyChangeEvent evt)
Helper method to relay evt to all listeners.
|
| void | removeVetoableChangeListener(VetoableChangeListener listener)
Remove an occurrence of a VetoableChangeListener from the listener list.
|
| void | removeVetoableChangeListener(String propertyName, VetoableChangeListener listener)
Remove a VetoableChangeListener for a specific property.
|
Serial:
Serial:
VetoableChangeMulticaster object.
Parameters: sourceBean The bean to be given as the source for any events.
Throws: NullPointerException if sourceBean is null
Parameters: listener The VetoableChangeListener to be added
Parameters: propertyName The name of the property to listen on. listener The VetoableChangeListener to be added
Throws: NullPointerException If listener is null
Parameters: listener The PropertyChangeListener to be added
Throws: NullPointerException If listener is null
Parameters: propertyName The name of the property to listen on. listener The VetoableChangeListener to be added
Throws: NullPointerException If listener is null
No event is fired if old and new are equal non-null.
Parameters: propertyName The programmatic name of the property that was changed. oldValue The old value of the property. newValue The new value of the property.
Throws: PropertyVetoException if a recipient wishes the property change to be rolled back.
No event is fired if old and new are equal.
This is merely a convenience wrapper around the more general fireVetoableChange method that takes Object values.
Parameters: propertyName The programmatic name of the property that was changed. oldValue The old value of the property. newValue The new value of the property.
Throws: PropertyVetoException if the recipient wishes the property change to be rolled back.
No event is fired if old and new are equal.
This is merely a convenience wrapper around the more general fireVetoableChange method that takes Object values.
Parameters: propertyName The programmatic name of the property that was changed. oldValue The old value of the property. newValue The new value of the property.
Throws: PropertyVetoException if the recipient wishes the property change to be rolled back.
No event is fired if old and new are equal and non-null. equal and non-null.
Parameters: evt The PropertyChangeEvent object.
Throws: PropertyVetoException if the recipient wishes the property change to be rolled back.
Parameters: propertyName the property name.
Returns: true if there are one or more listeners for the given property
Parameters: listener The VetoableChangeListener to be removed
Parameters: propertyName The name of the property that was listened on. listener The VetoableChangeListener to be removed