[concurrency-interest] Re: AtomicInteger and AtomicLong should implement Number

Riad Mohammed mohammed@intalio.com
Tue, 13 Jan 2004 09:53:31 -0800

Gregg Wonderly wrote:
> Dawid Kurzyniec wrote:
> > It is a very different situation; Numbers are safe as map keys, atomics
> > are definitely not. (See my response to Larry's e-mail).
> That is a single pattern which may or may not be the predominate use of
> these classes.  I think that its shortsighted to keep throwing this
> single issue up as THE ARGUMENT.

I have to agree.

There is no restriction on mutable objects being used as map keys beyond
general good programming practice. But it does not follow that if a mutable
object is used a key then it is always a bad programming practice.

The use case would be that at some point my internal AtomicX is no longer
being changed and I want to use it a key. One way is to make an immutable
object with AtomicX value and the other is to use the AtomicX object as the
key directly. The former is the safest but since my internal implementation
has the knowledge that the AtomicX is not being changed then the immutable
object creation is an unnecessary step.

If I receive a Number, the abstract class, from an external source and I
want to use that object as a map key then I have to make an immutable copy
anyway whether or not AtomicX implements requisite methods.