From 57b795b6ab80319e874ae70d6ede66a73399cb78 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 10 Oct 2006 20:49:13 +0000 Subject: [PATCH] Allow elements to be added at particular positions (e.g. the front) to an observer list. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1940 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/ObserverList.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/java/com/samskivert/util/ObserverList.java b/src/java/com/samskivert/util/ObserverList.java index dba8d76a..e5df8582 100644 --- a/src/java/com/samskivert/util/ObserverList.java +++ b/src/java/com/samskivert/util/ObserverList.java @@ -139,23 +139,15 @@ public class ObserverList extends ArrayList @Override public void add (int index, T element) { - throw new UnsupportedOperationException(UNSUPPORTED_ADD_MESSAGE); + if (!isDuplicate(element)) { + super.add(index, element); + } } @Override - public boolean add (T o) + public boolean add (T element) { - // make sure we're not violating the list constraints - if (!_allowDups && contains(o)) { - Log.warning("Observer attempted to observe list it's already " + - "observing! [obs=" + o + "]."); - Thread.dumpStack(); - return false; - - } else { - // go ahead and add the observer - return super.add(o); - } + return isDuplicate(element) ? false : super.add(element); } @Override @@ -246,6 +238,23 @@ public class ObserverList extends ArrayList } } + /** + * Returns true and issues a warning if this list does not allow duplicates + * and the supplied observer is already in the list. Returns false if the + * supplied observer is not a duplicate. + */ + protected boolean isDuplicate (T obs) + { + // make sure we're not violating the list constraints + if (!_allowDups && contains(obs)) { + Log.warning("Observer attempted to observe list it's already " + + "observing! [obs=" + obs + "]."); + Thread.dumpStack(); + return true; + } + return false; + } + /** * Applies the operation to the observer, catching and logging any * exceptions thrown in the process.