So, "equals()" is a method that anyone can override. Obviously, someone
did override it, and botched the job. (Or your class derives from a
class that overrides the equals() method.)
What is the list element that you are dealing with here? Can we have a
look at its code?
sure:
public class RemoteEventListenerList<T extends RemoteEventListener>
implements
InvocationHandler {
private List<T> list = new LinkedList<T>();
private List<T> toAdd = new ArrayList<T>();
private final Class<T> type;
private T proxy = null;
private List<T> toRemove = new ArrayList<T>();
public RemoteEventListenerList(Class<T> type) {
this.type = type;
}
public Object invoke(Object o, Method method, Object[] args)
throws Throwable {
updateList();
dispatchList(list, method, args);
while (moreAdded()) {
final List<T> temp = new ArrayList<T>(toAdd);
updateList();
dispatchList(temp, method, args);
}
return null;
}
private void updateList() {
doRemove();
doAdd();
}
private boolean moreAdded() {
return !toAdd.isEmpty();
}
private void dispatchList(List<T> dispList, Method method,
Object[] args)
throws IllegalAccessException {
for (Object t : dispList.toArray()) {
dispatch(method, type.cast(t), args);
}
}
private synchronized void doRemove() {
removeAll(list, toRemove);
//removeAll(list, toRemove);
toRemove.clear();
}
private void doAdd() {
list.addAll(toAdd);
toAdd.clear();
}
public void removeAll(List<T> l, List<T> c) {
Iterator<T> e = l.iterator();
while (e.hasNext()) {
if (contains(l, e.next())) {//when it reaches the element,
it throws a null exception
e.remove(); // even though it DOES contain
it.
}
}
}
// I was experimenting here.
/*
public void removeAll(List<T> l, List<T> c) {
for (T e: l) {
if (contains(l, e)) {
l.remove(e);
}
}
}
*/
public boolean contains(List<T> c, T l) {
for (T e: c) {
if (e == l) return true;
}
return false;
}
private void dispatch(Method method, T t, Object[] args)
throws IllegalAccessException {
try {
method.invoke(t, args);
} catch (InvocationTargetException e) {
if (!(e.getTargetException() instanceof
DisconnectedException)) {
e.getTargetException().printStackTrace();
remove(t);
} else {
e.printStackTrace();
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
public T getProxy() {
if (proxy == null) {
proxy = type.cast(Proxy
.newProxyInstance(type.getClassLoader(), new
Class[]{type}, this)
);
}
return proxy;
}
public void add(T t) {
toAdd.add(t);
}
private void remove(T t) {
toRemove.add(t);
}
public void removeToAdd(T t) {
toAdd.remove(t);
remove(t); // I added this later, not sure about it
}
}
</code>