public abstract class IlvAbstractEventListenerCollection<L extends EventListener> extends Object implements IlvEventListenerCollection<L>, Serializable
IlvAbstractEventListenerCollection
implements a thread-safe
collection of event listeners. Concrete subclasses can choose a specific
collection implementation based on the tradeoffs of event dispatch speed vs.
the speed of adding and removing listeners. Several general issues should be
weighed when choosing between using an IlvAbstractEventListenerCollection
subclass or a multicaster to manage the event dispatch system for a class:
IlvAbstractEventListenerCollection
will
dispatch events through an iterative algorithm, while a multicaster
uses a recursive algorithm. For large numbers of event listeners, a
multicaster can cause a stack overflow exception to occur.Here is an example that shows how a class can use an
IlvAbstractEventListenerCollection
concrete subclass for
registering event listeners and dispatching events to them:
public class MyClass { IlvEventListenerCollection<XYZListener> xyzListeners = new IlvEventListenerSet<XYZListener>(); public void addXYZListener(XYZListener aListener) { xyzListeners.addListener(aListener); } public void removeXYZListener(XYZListener aListener) { xyzListeners.removeListener(aListener); } public void notifyXYZListeners () { XYZEvent event = new XYZEvent(this); for (Iterator<XYZListener> i = xyzListeners.getListeners(); i.hasNext(); ) { XYZListener xyzListener = i.next(); xyzListener.xyzHappened(event); } } }
Constructor and Description |
---|
IlvAbstractEventListenerCollection()
Creates a new
IlvAbstractEventListenerCollection . |
Modifier and Type | Method and Description |
---|---|
void |
addListener(L listener)
Adds the specified listener to this collection.
|
protected abstract Collection<L> |
cloneListeners()
Returns a copy of the collection of event listeners.
|
boolean |
contains(L listener)
Returns whether this collection contains the specified listener.
|
Iterator<L> |
getListeners()
Returns an iterator over the event listeners.
|
protected abstract void |
initListeners()
Initializes the collection of event listeners.
|
void |
removeListener(L listener)
Removes the specified listener from this collection.
|
void |
save(ObjectOutputStream s,
String k)
Serializes the collection of event listeners as a series of key-value pairs.
|
int |
size()
Returns the number of listeners in this collection.
|
public IlvAbstractEventListenerCollection()
IlvAbstractEventListenerCollection
.protected abstract void initListeners()
protected abstract Collection<L> cloneListeners()
public void addListener(L listener)
addListener
in interface IlvEventListenerCollection<L extends EventListener>
listener
- The listener.public void removeListener(L listener)
removeListener
in interface IlvEventListenerCollection<L extends EventListener>
listener
- The listener.public Iterator<L> getListeners()
getListeners
in interface IlvEventListenerCollection<L extends EventListener>
public int size()
public boolean contains(L listener)
contains
in interface IlvEventListenerCollection<L extends EventListener>
listener
- The listener.true
if this collection contains listener
,
otherwise false
.public void save(ObjectOutputStream s, String k) throws IOException
save
in interface IlvEventListenerCollection<L extends EventListener>
s
- The object output stream to write the event listeners.k
- A key string that is written in front of
each listener to identify its type.IOException
© Copyright Rogue Wave Software, Inc. 1997, 2018. All Rights Reserved.