[concurrency-interest] AtomicInteger and AtomicLong extend Number

Doug Lea dl@cs.oswego.edu
Sat, 10 Jan 2004 20:28:16 -0500

Since the aesthetic and logical arguments continue to be split, I
decided to go the empirical route.  I spent a while googling usages of
Number out there.  This made it a surprisingly easy call (even given
my resistance to it). AtomicInteger and AtomicLong should both extend

* There are at least dozens of other numerically-based classes out
there that extend Number including mutable classes, and including
carefully constructed, useful-looking classes. So there is clear

* There is a lot more code out there than I would have thought that
probes for "instanceof Number" in order to extract numbers from
arbitrary classes. Most of this looks useful enough to not get in the
way of.

* While I saw mistakes here and there in code that probes for
instanceof Number, I didn't seem any that would be any more likely to
trigger for atomics than for most any other Number class.  (The most
common mistake was trying to distinguish floating vs integral Number
types. There doesn't seem to be a correct way to do this given only
what Number provides.  It is possible though to determine whether a
Number holds an integral (<64bit) value, which is probably what people
want to check but don't. Also, there are mistakes due to forgetting
that not all longs will fit exactly into doubles. It would be nice if
the Number class provided some static utility methods to help avoid
people making errors along these lines.)

* The Atomic classes still should not, and will not, implement
Comparable, or define value-based hashCode and equals methods.  The
most prevalant non-JDK Number classes out there seem to be the Mutable
(MuShort, MuInteger etc) classes used by Apache and JBoss. They do
additionally support Comparable and define value-based hashCodes and
equals methods. But all are defined in ways that work as naively
expected in at best a very narrow range of contexts.  As we've noted,
such problems would be compounded for atomics, and usages would almost
never be correct.