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.