RFR: 8310813: Simplify and modernize equals, hashCode, and compareTo for BigInteger

Raffaello Giulietti rgiulietti at openjdk.org
Mon Jun 26 14:18:06 UTC 2023


On Fri, 23 Jun 2023 17:27:00 GMT, Pavel Rappo <prappo at openjdk.org> wrote:

> Please review this PR to use modern APIs and language features to simplify equals, hashCode, and compareTo for BigInteger. If you have any performance concerns, please raise them.
> 
> This PR is cherry-picked from a bigger, not-yet-published PR, to test the waters. That latter PR will be published soon.

src/java.base/share/classes/java/math/BigInteger.java line 3955:

> 3953:         int i = Arrays.mismatch(m1, 0, len1, m2, 0, len2);
> 3954:         if (i != -1)
> 3955:             return ((m1[i] & LONG_MASK) < (m2[i] & LONG_MASK)) ? -1 : 1;

return Integer.compareUnsigned(m1[i], m2[i]);

or, if you want to be on the safe side

            return Integer.compareUnsigned(m1[i], m2[i]) < 0 ? -1 : 1;

While the spec of `compareUnsigned` speaks of negative (<0), zero (==0), and positive (>0) return values (but in fact returns -1, 0, 1), `BigInteger.compareTo()` is more specific for some reason: -1, 0, 1.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/14630#discussion_r1242245875


More information about the core-libs-dev mailing list