You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Brian S O'Neill <br...@earthlink.net> on 2004/02/20 04:54:59 UTC

WeakIdentityMap

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 
(http://teatrove.sourceforge.net/) 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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] WeakIdentityMap

Posted by Stephen Colebourne <sc...@btopenworld.com>.
I've changed the implementation of ReferenceMap to extend AbstractHashedMap.
This may should improve the possibilities for subclassing. The next step is
to create a subclass IdentityReferenceMap where identity is used instead of
equals, as per IdentityMap vs HashedMap.

There are still differences between implementations, IIUC, as ReferenceMap
still uses an ordinary object for its map entry (due to not making HashEntry
an interface). Besides, I still don't grok all of the implementation anyway,
so I don't want to change it too much.

Stephen

----- Original Message -----
From: "Brian S O'Neill" <br...@earthlink.net>
> 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
> (http://teatrove.sourceforge.net/) 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: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: WeakIdentityMap

Posted by Brian S O'Neill <br...@earthlink.net>.
WeakIdentityMap cannot extend ReferenceMap becauase it isn't extensible 
enough. Specifically, supporting keys that use System.identityHashCode() 
and ==. Also, ReferenceMap removes cleared references too aggressively.

Classes like ReferenceMap and AbstractHashedMap could probably have more 
extension points, but too many may impact performance. If 
AbstractHashMap had more extension points, and it didn't impact 
performance enough, then even ReferenceMap could extend it.

The new version of the class has the proper license, and I've adapted it 
to follow coding conventions I've seen in other commons code. I'll mail 
in the ASF license, write test cases, and then post to bugzilla.

Noel J. Bergman wrote:

>Brian,
>
>I'm just going to address the how part of your questions, and will leave the
>rest to others.
>
>If you decide to submit your classes, it would be best of you submitted your
>code with the ASF License (www.apache.org/licenses) already embedded.  And
>please also submit a software grant
>(http://www.apache.org/licenses/software-grant.txt) for them.
>
>I only skimmed what you wrote, but could your class be a subclass of
>ReferenceMap?
>
>	--- Noel
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


RE: WeakIdentityMap

Posted by "Noel J. Bergman" <no...@devtech.com>.
> Out of curiousity, how significant does a contribution need to be before
> completing and submitting the software grant form is required?

I wish I knew, and I'd like not to have to find out.  There is the concept
of an implied grant, there are changes in the Apache Software License v2 to
address it, and there is a proposed community notice ( that would make it
more explicit.

See: http://www.apache.org/licenses/proposed/community.txt

There are plans to incorporate the above into JIRA as soon as it is
approved.

Under the current climate, it is probably a good idea to error on the
conservative side.  I wouldn't worry about it for most patches, but if there
is an existing codebase, and the contributor is amenable, I would request
it.

	--- Noel


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


RE: WeakIdentityMap

Posted by Michael Heuer <he...@acm.org>.
On Fri, 20 Feb 2004, Noel J. Bergman wrote:

> If you decide to submit your classes, it would be best of you submitted your
> code with the ASF License (www.apache.org/licenses) already embedded.  And
> please also submit a software grant
> (http://www.apache.org/licenses/software-grant.txt) for them.

Out of curiousity, how significant does a contribution need to be before
completing and submitting the software grant form is required?

   michael


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


RE: WeakIdentityMap

Posted by "Noel J. Bergman" <no...@devtech.com>.
Brian,

I'm just going to address the how part of your questions, and will leave the
rest to others.

If you decide to submit your classes, it would be best of you submitted your
code with the ASF License (www.apache.org/licenses) already embedded.  And
please also submit a software grant
(http://www.apache.org/licenses/software-grant.txt) for them.

I only skimmed what you wrote, but could your class be a subclass of
ReferenceMap?

	--- Noel


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org