diff --git a/projects/samskivert/src/java/com/samskivert/util/ObserverList.java b/projects/samskivert/src/java/com/samskivert/util/ObserverList.java index 7645577d..f8d5e499 100644 --- a/projects/samskivert/src/java/com/samskivert/util/ObserverList.java +++ b/projects/samskivert/src/java/com/samskivert/util/ObserverList.java @@ -181,11 +181,14 @@ public class ObserverList extends ArrayList // create a snapshot of the observer array at the time we // start the notification to ensure that modifications to the // array during notification don't hose us - Object[] obs = toArray(); - int ocount = obs.length; + int ocount = size(); + if (_snap == null || _snap.length < ocount) { + _snap = new Object[ocount]; + } + Object[] obs = toArray(_snap); for (int ii = 0; ii < ocount; ii++) { - if (!checkedApply(obop, obs[ii])) { - remove(obs[ii]); + if (!checkedApply(obop, _snap[ii])) { + remove(_snap[ii]); } } @@ -224,6 +227,10 @@ public class ObserverList extends ArrayList /** Whether to allow observers to observe more than once simultaneously. */ 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 add() variants. */ protected static final String UNSUPPORTED_ADD_MESSAGE = "Observers may only be added via ObserverList.add(Object).";