Allow duplicate checking to be disabled for performance reasons. The profiler

has spoken.
This commit is contained in:
Michael Bayne
2011-04-08 17:48:07 -07:00
parent 5c99fd31df
commit 0e1baccf4b
2 changed files with 20 additions and 5 deletions
@@ -177,6 +177,12 @@ public abstract class ObserverList<T>
*/
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<T>
@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<T>
_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<T>
return false;
}
/** The notification policy. */
protected Policy _policy;
/** Our list of observers. */
protected List<T> _list;
protected boolean _checkDups = true;
}
/**
@@ -68,6 +68,12 @@ public class WeakObserverList<T> extends ObserverList<T>
_delegate.clear();
}
@Override public WeakObserverList setCheckDuplicates (boolean checkDuplicates)
{
_delegate.setCheckDuplicates(checkDuplicates);
return this;
}
/**
* Removes all garbage-collected observers from the list.
*/