RFR: 8290973: In AffineTransform, equals(Object) is inconsistent with hashCode()

Joe Darcy darcy at openjdk.org
Wed Aug 10 05:14:39 UTC 2022


On Fri, 10 Jun 2022 09:39:48 GMT, Martin Desruisseaux <duke at openjdk.org> wrote:

> `AffineTransform.equals(Object)` and `hashCode()` break two contracts:
> 
> * `A.equals(A)` returns `false` if at least one affine transform coefficient is NaN.
> * `A.equals(B)` should imply `A.hashCode() == B.hashCode()`, but it is not the case if a coefficient is zero with an opposite sign in A and B.
> 
> This patch preserves the current behaviour regarding 0 (i.e. -0 is considered equal to +0) for backward compatibility reason. Instead the `hashCode()` method is updated for being consistent with `equals(Object)` behaviour.

src/java.desktop/share/classes/java/awt/geom/AffineTransform.java line 3920:

> 3918:     private static long hash(double m) {
> 3919:         long h = Double.doubleToLongBits(m);
> 3920:         if (h == 0x8000000000000000L) h = 0;    // Replace -0 by +0.

Another idiom to accomplish this is 

return Double.doubleToLongBits(m + 0.0); // Turn -0.0 to +0.0

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

PR: https://git.openjdk.org/jdk/pull/9121



More information about the client-libs-dev mailing list