From 0e1baccf4b9f9796b26257e79eaa08c1b1a64568 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 8 Apr 2011 17:48:07 -0700 Subject: [PATCH] Allow duplicate checking to be disabled for performance reasons. The profiler has spoken. --- .../com/samskivert/util/ObserverList.java | 19 ++++++++++++++----- .../com/samskivert/util/WeakObserverList.java | 6 ++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/samskivert/util/ObserverList.java b/src/main/java/com/samskivert/util/ObserverList.java index a5f754d8..b7d015ef 100644 --- a/src/main/java/com/samskivert/util/ObserverList.java +++ b/src/main/java/com/samskivert/util/ObserverList.java @@ -177,6 +177,12 @@ public abstract class ObserverList */ public abstract void clear (); + /** + * Configures this observer list to check duplicates when adding observers, or not. By default + * duplicates are checked, but for performance reasons, one may wish to disable this check. + */ + public abstract ObserverList setCheckDuplicates (boolean checkDuplicates); + /** * Returns true if this list has no observers, false otherwise. */ @@ -194,14 +200,14 @@ public abstract class ObserverList @Override public boolean add (int index, T element) { if (element == null) { throw new NullPointerException("Null observers not allowed."); } - if (isDuplicate(element)) { return false; } + if (_checkDups && isDuplicate(element)) { return false; } _list.add(index, element); return true; } @Override public boolean add (T element) { if (element == null) { throw new NullPointerException("Null observers not allowed."); } - if (isDuplicate(element)) { return false; } + if (_checkDups && isDuplicate(element)) { return false; } _list.add(element); return true; } @@ -249,6 +255,11 @@ public abstract class ObserverList _list.clear(); } + @Override public ObserverList setCheckDuplicates (boolean checkDuplicates) { + _checkDups = checkDuplicates; + return this; + } + /** Used to determine whether an element is in the list. */ protected int indexOf (T element) { for (int ii = 0, ll = _list.size(); ii < ll; ii++) { @@ -269,11 +280,9 @@ public abstract class ObserverList return false; } - /** The notification policy. */ protected Policy _policy; - - /** Our list of observers. */ protected List _list; + protected boolean _checkDups = true; } /** diff --git a/src/main/java/com/samskivert/util/WeakObserverList.java b/src/main/java/com/samskivert/util/WeakObserverList.java index 0eadf00b..2aa1be67 100644 --- a/src/main/java/com/samskivert/util/WeakObserverList.java +++ b/src/main/java/com/samskivert/util/WeakObserverList.java @@ -68,6 +68,12 @@ public class WeakObserverList extends ObserverList _delegate.clear(); } + @Override public WeakObserverList setCheckDuplicates (boolean checkDuplicates) + { + _delegate.setCheckDuplicates(checkDuplicates); + return this; + } + /** * Removes all garbage-collected observers from the list. */