aherbert commented on code in PR #227:
URL: https://github.com/apache/commons-geometry/pull/227#discussion_r1457926790
##########
commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/GreatCircle.java:
##########
@@ -338,7 +338,17 @@ public boolean eq(final GreatCircle other, final
Precision.DoubleEquivalence pre
/** {@inheritDoc} */
@Override
public int hashCode() {
- return Objects.hash(pole, u, v, getPrecision());
+ int result = 1;
+ long temp;
Review Comment:
`temp` does not have to be a `long` here since all the `hashCode()` methods
will return an `int`. As such all the xor shift folding is effectively just
randomly flipping the bits based on the sign since `(temp >>> 32) == 0` for
positive or `-1` (32-bits set) for negative `int` hash codes. The xor operation
then flips the original int bits if they were negative.
The fact that this works over using Object.hashCode is that some of the
hashCodes are negative. So your implementation uses these bit flipped before
the addition and the hash is different, but still random.
I think the current implementation is fine. What should be changed is the
test. This is assuming that non equal objects will have different hash codes.
This is not true. The only contract is that equal objects have equal hash codes.
Since the hash code for the 3-D vectors use the xyx coordinates, using unit
vectors results in some of the hash codes being the same. Perhaps try updating
the vectors for objects b, c, d to use random xyz coords. The following lines
should then pass (unless you are very unlucky with your random vectors).
```java
// Probably true (unless we are unlucky with the hashes of the
vectors)
Assertions.assertNotEquals(hash, b.hashCode());
Assertions.assertNotEquals(hash, c.hashCode());
Assertions.assertNotEquals(hash, d.hashCode());
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]