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
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/lang/Double.java
Expand Up @@ -877,8 +877,7 @@ public int hashCode() {
* @since 1.8
*/
public static int hashCode(double value) {
long bits = doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
return Long.hashCode(doubleToLongBits(value));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/time/LocalTime.java
Expand Up @@ -1596,8 +1596,7 @@ public boolean equals(Object obj) {
*/
@Override
public int hashCode() {
long nod = toNanoOfDay();
return (int) (nod ^ (nod >>> 32));
return Long.hashCode(toNanoOfDay());
}

//-----------------------------------------------------------------------
Expand Down
Expand Up @@ -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);
}

//-----------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/util/BitSet.java
Expand Up @@ -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
Expand Down Expand Up @@ -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

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

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

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

}

/**
Expand Down
Expand Up @@ -111,8 +111,7 @@ public boolean equals(final Object obj) {
}

public int hashCode() {
final long bits = Double.doubleToLongBits(doubleValue);
return (int)(bits ^ (bits >>> 32));
return Double.hashCode(doubleValue);
}

public String toString() {
Expand Down
Expand Up @@ -123,8 +123,7 @@ public int hashCode() {
if (value == 0d) {
return 0;
}
long v = Double.doubleToLongBits(value);
return (int) (v ^ (v >>> 32));
return Double.hashCode(value);
}

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

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

/**
Expand Down