Avoid creating a new array when we notify our observers in SAFE_IN_ORDER
mode unless our list has grown since we last issued a notification. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1707 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -181,11 +181,14 @@ public class ObserverList extends ArrayList
|
|||||||
// create a snapshot of the observer array at the time we
|
// create a snapshot of the observer array at the time we
|
||||||
// start the notification to ensure that modifications to the
|
// start the notification to ensure that modifications to the
|
||||||
// array during notification don't hose us
|
// array during notification don't hose us
|
||||||
Object[] obs = toArray();
|
int ocount = size();
|
||||||
int ocount = obs.length;
|
if (_snap == null || _snap.length < ocount) {
|
||||||
|
_snap = new Object[ocount];
|
||||||
|
}
|
||||||
|
Object[] obs = toArray(_snap);
|
||||||
for (int ii = 0; ii < ocount; ii++) {
|
for (int ii = 0; ii < ocount; ii++) {
|
||||||
if (!checkedApply(obop, obs[ii])) {
|
if (!checkedApply(obop, _snap[ii])) {
|
||||||
remove(obs[ii]);
|
remove(_snap[ii]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,6 +227,10 @@ public class ObserverList extends ArrayList
|
|||||||
/** Whether to allow observers to observe more than once simultaneously. */
|
/** Whether to allow observers to observe more than once simultaneously. */
|
||||||
protected boolean _allowDups;
|
protected boolean _allowDups;
|
||||||
|
|
||||||
|
/** Used to avoid creating a new snapshot array every time we notify
|
||||||
|
* our observers if the size has not changed. */
|
||||||
|
protected Object[] _snap;
|
||||||
|
|
||||||
/** Message reported for unsupported <code>add()</code> variants. */
|
/** Message reported for unsupported <code>add()</code> variants. */
|
||||||
protected static final String UNSUPPORTED_ADD_MESSAGE =
|
protected static final String UNSUPPORTED_ADD_MESSAGE =
|
||||||
"Observers may only be added via ObserverList.add(Object).";
|
"Observers may only be added via ObserverList.add(Object).";
|
||||||
|
|||||||
Reference in New Issue
Block a user