From 3351e803359faece2eabd0f213ac619f315d4688 Mon Sep 17 00:00:00 2001 From: ray Date: Fri, 25 Aug 2006 01:22:03 +0000 Subject: [PATCH] Implement indexOf and lastIndexOf using reference equality. For observer lists, we do not care if two observers happen to implement equals() such that they agree to be the same, we care only if they're the exact same observer. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1891 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/ObserverList.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/java/com/samskivert/util/ObserverList.java b/src/java/com/samskivert/util/ObserverList.java index deb8d593..bd6a89ec 100644 --- a/src/java/com/samskivert/util/ObserverList.java +++ b/src/java/com/samskivert/util/ObserverList.java @@ -136,13 +136,13 @@ public class ObserverList extends ArrayList _allowDups = allowDups; } - // documentation inherited + @Override public void add (int index, T element) { throw new UnsupportedOperationException(UNSUPPORTED_ADD_MESSAGE); } - // documentation inherited + @Override public boolean add (T o) { // make sure we're not violating the list constraints @@ -158,18 +158,44 @@ public class ObserverList extends ArrayList } } - // documentation inherited + @Override public boolean addAll (Collection c) { throw new UnsupportedOperationException(UNSUPPORTED_ADD_MESSAGE); } - // documentation inherited + @Override public boolean addAll (int index, Collection c) { throw new UnsupportedOperationException(UNSUPPORTED_ADD_MESSAGE); } + @Override + public int indexOf (Object element) + { + // indexOf and lastIndexOf are implemented using reference equality + // so that contains() also uses reference equality. + for (int ii = 0, nn = size(); ii < nn; ii++) { + if (element == get(ii)) { + return ii; + } + } + return -1; + } + + @Override + public int lastIndexOf (Object element) + { + // indexOf and lastIndexOf are implemented using reference equality + // so that contains() also uses reference equality. + for (int ii = size() - 1; ii >= 0; ii--) { + if (element == get(ii)) { + return ii; + } + } + return -1; + } + /** * Applies the supplied observer operation to all observers in the * list in a manner conforming to the notification ordering policy