public class ChangeSupport extends Object
A utility class to provide management for informing ChangeListeners of ChangeEvents.
This is loosely modelled after the standard PropertyChangeEvent objects.
For an object to correctly fire these events, they must follow a broad
outline like this:
public void mutator(foo arg) throw ChangeVetoException {
ChangeEvent cevt = new ChangeEvent(this, SOME_EVENT_TYPE, arg);
synchronized(changeSupport) {
changeSupport.firePreChangeEvent(cevt);
// update our state using arg
// ...
changeSupport.firePostChangeEvent(cevt);
}
}
The methods that delegate adding and removing listeners to a ChangeSupport must take responsibility for synchronizing on the delegate.
Constructor and Description |
---|
ChangeSupport()
Generate a new ChangeSupport instance.
|
ChangeSupport(int initialSize)
Generate a new ChangeSupport instance which has room for initialSize
listeners before it needs to grow any resources.
|
ChangeSupport(int initialSize,
int delta)
Generate a new ChangeSupport instance which has room for initialSize
listeners before it needs to grow any resources, and which will grow by
delta each time.
|
ChangeSupport(Set unchanging) |
ChangeSupport(Set unchanging,
int initialSize,
int delta)
Generate a new ChangeSupport instance which has room for initialSize
listeners before it needs to grow any resources, and which will grow by
delta each time.
|
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(ChangeListener cl)
Add a listener that will be informed of all changes.
|
void |
addChangeListener(ChangeListener cl,
ChangeType ct)
Add a listener that will be informed of changes of a given type (and it's subtypes)
|
String |
displayString() |
void |
firePostChangeEvent(ChangeEvent ce)
Inform the listeners that a change has taken place using their
firePostChangeEvent methods.
|
void |
firePreChangeEvent(ChangeEvent ce)
Inform the listeners that a change is about to take place using their
firePreChangeEvent methods.
|
protected void |
growIfNecessary()
Grows the internal resources if by adding one more listener they would be
full.
|
boolean |
hasListeners()
Return true if we have any listeners registered at all.
|
boolean |
hasListeners(ChangeType ct)
Return true if we have listeners registered for a particular change type.
|
boolean |
isUnchanging(ChangeType ct) |
protected void |
reapGarbageListeners()
Remove all references to listeners which have been cleared by the
garbage collector.
|
void |
removeChangeListener(ChangeListener cl)
Remove a listener that was interested in all types of changes.
|
void |
removeChangeListener(ChangeListener cl,
ChangeType ct)
Remove a listener that was interested in a specific types of changes.
|
public ChangeSupport()
public ChangeSupport(int initialSize)
initialSize
- the number of listeners that can be added before this
needs to grow for the first timepublic ChangeSupport(int initialSize, int delta)
initialSize
- the number of listeners that can be added before this
needs to grow for the first timedelta
- the number of listener slots that this will grow by each time
it needs topublic ChangeSupport(Set unchanging)
public ChangeSupport(Set unchanging, int initialSize, int delta)
unchanging
- Set of ChangeTypes that can never be firedinitialSize
- the number of listeners that can be added before this
needs to grow for the first timedelta
- the number of listener slots that this will grow by each time
it needs topublic boolean hasListeners()
public boolean hasListeners(ChangeType ct)
ct
- the ChangeType to checkpublic void addChangeListener(ChangeListener cl)
cl
- the ChangeListener to addpublic void addChangeListener(ChangeListener cl, ChangeType ct)
cl
- the ChangeListenerct
- the ChangeType it is to be informed ofprotected void growIfNecessary()
public void removeChangeListener(ChangeListener cl)
cl
- a ChangeListener to removepublic void removeChangeListener(ChangeListener cl, ChangeType ct)
cl
- a ChangeListener to removect
- the ChangeType that it was interested inprotected void reapGarbageListeners()
public void firePreChangeEvent(ChangeEvent ce) throws ChangeVetoException
Inform the listeners that a change is about to take place using their firePreChangeEvent methods.
Listeners will be informed if they were interested in all types of event, or if ce.getType() is equal to the type they are registered for.
This method must be called while the current thread holds the lock on this change support.
ce
- the ChangeEvent to pass onChangeVetoException
- if any of the listeners veto this changepublic void firePostChangeEvent(ChangeEvent ce)
Inform the listeners that a change has taken place using their firePostChangeEvent methods.
Listeners will be informed if they were interested in all types of event, or if ce.getType() is equal to the type they are registered for.
This method must be called while the current thread holds the lock on this change support.
ce
- the ChangeEvent to pass onpublic boolean isUnchanging(ChangeType ct)
public String displayString()
Copyright © 2014 BioJava. All rights reserved.