Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8268113: Re-use Long.hashCode() where possible #4309

Closed
wants to merge 13 commits into from
@@ -878,7 +878,7 @@ public int hashCode() {
*/
public static int hashCode(double value) {
long bits = doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
return Long.hashCode(bits);
stsypanov marked this conversation as resolved.
Show resolved Hide resolved
}

/**
@@ -1597,7 +1597,7 @@ public boolean equals(Object obj) {
@Override
public int hashCode() {
long nod = toNanoOfDay();
return (int) (nod ^ (nod >>> 32));
return Long.hashCode(nod);
}

//-----------------------------------------------------------------------
@@ -410,7 +410,7 @@ public int hashCode() {
long hash = minSmallest + (minLargest << 16) + (minLargest >> 48) +
(maxSmallest << 32) + (maxSmallest >> 32) + (maxLargest << 48) +
(maxLargest >> 16);
return (int) (hash ^ (hash >>> 32));
return Long.hashCode(hash);
}

//-----------------------------------------------------------------------
@@ -96,13 +96,13 @@ public class BitSet implements Cloneable, java.io.Serializable {
/**
* The number of words in the logical size of this BitSet.
*/
private transient int wordsInUse = 0;
private transient int wordsInUse;

/**
* Whether the size of "words" is user-specified. If so, we assume
* the user knows what he's doing and try harder to preserve it.
*/
private transient boolean sizeIsSticky = false;
stsypanov marked this conversation as resolved.
Show resolved Hide resolved
private transient boolean sizeIsSticky;

/* use serialVersionUID from JDK 1.0.2 for interoperability */
@java.io.Serial
@@ -1037,7 +1037,7 @@ public int hashCode() {
for (int i = wordsInUse; --i >= 0; )
h ^= words[i] * (i + 1);

return (int)((h >> 32) ^ h);
return Long.hashCode(h);
Copy link
Contributor Author

@stsypanov stsypanov Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here >> instead of >>> in original code seems to be a typo

Copy link
Contributor

@szegedi szegedi Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is specified as >> in JavaDoc just above the implementation. As the algorithm is part of the public API and thus part of the specification, I don't think you can change it just here in the implementation; you'd need to at least submit a CSR for it.

Copy link
Contributor Author

@stsypanov stsypanov Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll then revert this change

}

/**
@@ -112,7 +112,7 @@ public boolean equals(final Object obj) {

public int hashCode() {
final long bits = Double.doubleToLongBits(doubleValue);
return (int)(bits ^ (bits >>> 32));
return Long.hashCode(bits);
stsypanov marked this conversation as resolved.
Show resolved Hide resolved
}

public String toString() {
@@ -124,7 +124,7 @@ public int hashCode() {
return 0;
}
long v = Double.doubleToLongBits(value);
return (int) (v ^ (v >>> 32));
return Long.hashCode(v);
}

// NOTE: 0.0 is equal but not identical to -0.0
@@ -276,7 +276,8 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return (int) (asBits() ^ (asBits() >>> 32));
long bits = asBits();
return Long.hashCode(bits);
}

/**