I was trying to check the presence of an EntityProxy in a
Set<EntityProxy> [i.e. set.contains(proxy)]. This returned false even
though the proxy was an element of the set. As it turns out
EntityProxy.equals() doesn't work, or, more precisly, it seems to
behave as the default equals() and check reference equality instead of
value equality.
So how do I get a working value checking equals? Without it,
Set<EntityProxy> is just about useless. I didn't see anything relevant
in [1] and Google was no help either. (I did find a similar question
but it went unanswered.)
Cheers,
Hilco
[1] http://code.google.com/webtoolkit/doc/latest/DevGuideRequestFactory.html
Firstly, I should have been more concerned about hashCode when working
with Sets but hashCode and equals go hand in hand and hashCode is part
of EntityProxyCategory as well.
The EntityProxy-s might be from different contexts but they are not
editable and all fields (including stableId) are the same. I checked
this. They all come from the database, through RF, so I have no
control over how many instances are created, AFAICT.
But I'm really confused now. I can see that equals/hashCode do what I
need them to do (i.e. compare stableId) but it's not working. Very
strange.
Okay, so Robert was spot on. The problem is with RequestContexts. The
set has a RequestContext (although I'm not sure why, AFAICT its
onSuccess method has already run) and the proxy does not (i.e.
BaseProxyCategory.requestContext(proxy) returns NULL).
I'm sure there is a good reason for the RequestContext check in
EntityProxyCategory.equals() but it seems more like a bug than a
feature to me. Because of this extra check sets and maps no longer
work as one would expect them to.
How do I remove an EntityProxy from its RequestContext? There doesn't
seem to be a close() method or similar on RequestContext. When is the
RequestContext set to NULL? How do I get sets/maps to work again?
That just doesn't seem feasible, I'm afraid.
Basically what I do is the following:
1) request a list of proxies from the database (List<EntityProxy>, "list");
2) unrelated, at a later time in another place, new RequestContext for "entity";
3) add one proxy ("proxy") from "list" to a Set<EntityProxy> ("set");
(this "set" is part of "entity")
4) RequestContext.fire();
5) onSuccess();
6) unrelated, at a later time in another place, check that
entity.set.contains(proxy).
Is this really such a strange scenario?
Why would EntityProxyCategory.equals() include requestContext()? What
would be the impact of removing that particular check?
How do I get an EntityProxy to have a NULL RequestContext?