Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 338087 - CollectionAnnotator must use WeakHashMap
Summary: CollectionAnnotator must use WeakHashMap
Status: CLOSED FIXED
Alias: None
Product: Epsilon
Classification: Modeling
Component: Core (show other bugs)
Version: unspecified   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Dimitris Kolovos CLA
QA Contact:
URL:
Whiteboard: interim
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-24 09:11 EST by Dimitris Kolovos CLA
Modified: 2012-02-06 10:59 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dimitris Kolovos CLA 2011-02-24 09:11:41 EST
CollectionAnnotator must use WeakHashMap instead of HashMap<WeakReference, Object> to simplify interface and reduce memory usage.
Comment 1 Dimitris Kolovos CLA 2011-02-24 09:12:47 EST
Fixed in the SVN
Comment 2 Antonio Garcia-Dominguez CLA 2011-02-24 11:27:36 EST
It's not so simple, I'm afraid. After debugging for two hours the test that started failing since this morning, I've found this paragraph in the WeakHashMap javadoc:

"This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap at some later time and be surprised that its entry has been removed. This class will work perfectly well with key objects whose equals methods are not based upon object identity, such as String instances. With such recreatable key objects, however, the automatic removal of WeakHashMap entries whose keys have been discarded may prove to be confusing."

The problem is that ArrayList (e.g. EolSequence) uses AbstractList's equals() method, which is based on content. For this reason, we will lose whatever annotations we had when we add an element to a collection.

We will probably need to wrap the objects in a type whose equals() method uses object identity, I'm afraid. I'll try this and report back in a while.
Comment 3 Antonio Garcia-Dominguez CLA 2011-02-24 11:55:01 EST
I'm thinking of several options:

- using a wrapper doesn't really work, as it'd be freed automatically (nobody holds a strong reference to the wrapper)

- redefining equals() in Eol* can break existing code

We could define our own equivalent of WeakHashMap which ignores the equals() method of the keys, but that sounds pretty difficult :-/.

I'll keep on looking.
Comment 4 Antonio Garcia-Dominguez CLA 2011-02-24 12:16:36 EST
There are alternative maps which do have the desired semantics, according to this Stack Exchange article:

http://stackoverflow.com/questions/1709965/java-implementation-of-notification-provider-vs-hashcode-driven-map

In particular, Apache Commons has the ReferenceIdentityMap:

http://commons.apache.org/collections/api-release/org/apache/commons/collections/map/ReferenceIdentityMap.html

And Google Collections has its MapMaker:

http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/MapMaker.html

Both are under the Apache License 2.0. The Apache Commons class seems to be the easiest to use. I think there would be no legal issues if we included the Apache Commons Collections .jar as a dependency. Galileo has a bunch of Apache License third-party dependencies itself:

http://www.eclipse.org/eclipse/development/project-log-files/eclipse_project_3_5_log.html

What do you think?
Comment 5 Antonio Garcia-Dominguez CLA 2011-02-24 12:43:00 EST
The issue seems to have been resolved with the ReferenceIdentityMap from Apache Commons. Here is a sequence of patches: the first one adds Apache Commons as a dependency, and the next one changes CollectionAnnotator to use ReferenceIdentityMap.

The first patch may probably not apply correctly if you're not using Git: just create a project from the latest JAR and add it to the dependencies and the eol.engine projects:

http://mirror.fubra.com/ftp.apache.org//commons/collections/binaries/commons-collections-3.2.1-bin.tar.gz

The second patch should apply cleanly, though.
Comment 6 Dimitris Kolovos CLA 2011-07-25 08:16:40 EDT
Fixed in 0.9.1