commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian S O'Neill <>
Subject WeakIdentityMap
Date Fri, 20 Feb 2004 03:54:59 GMT
I've been maintaining an open source project for some time that I'd like 
to break up, and move the more useful components into other, more 
appropriate projects. The TeaTrove project 
( contains a few lonely collections 
classes, which I think fit quite nicely in the Jakarta Commons project.

The first class I'd like to submit is named IdentityMap in TeaTrove, 
which is better named as WeakIdentityMap. This name change also resolve 
a name conflict. There are a few key differences between WeakIdentityMap 
and (commons) IdentityMap:

First, keys are weakly referenced. If IdentityMap is being used only for 
key-value lookups, then swapping in a WeakIdentityMap will not change 
behavior. If the key object is gone, you can't look it up, so the entry 
might as well be garbage collected. WeakIdentityMap provides the 
behavior that WeakHashMap should, because it enforces the identity 
lookup that WeakHashMap merely encourages.

Second, values are compared by the equals method, much like ordinary 
maps do. IdentityMap compares values by identity only. I'm not sure why 
this decision was made, but WeakIdentityMap can be modified to follow 
this behavior for the sake of consistency.

I've also made a few changes to WeakIdentityMap from its original source 
in TeaTrove. It now supports null keys. Also, the TeaTrove version used 
to remove cleared entries even during accessor operations, which 
sometimes caused problems. WeakIdentityMap only removes cleared entries 
when calling put or remove.

Although WeakIdentityMap is essentially a hashed map, I was not able to 
make it extend AbstractHashedMap. The put and remove operations need to 
perform special operations to detect and remove cleared entries, and the 
entry class has special methods and also extends WeakReference. The test 
case for WeakIdentityMap can probably extend the IdentityMap test case, 
but some minor changes are required in TestIdentityMap.

Is it best for me to attach the WeakIdentityMap source(s) as an 
attachment and send it to the developers list or is there a better way 
to make submissions?

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message