From f26d3214e2002fa5c81bafd43d9dce2ac3fd61e0 Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 25 Aug 2005 22:10:18 +0000 Subject: [PATCH] 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 --- .../java/com/samskivert/util/ObserverList.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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).";