提交 6cc90236 编写于 作者: M mduigou

7088913: Add compatible static hashCode(primitive) to primitive wrapper classes

Summary: Adds static utility methods to each primitive wrapper class to allow calculation of a hashCode value from an unboxed primitive.
Reviewed-by: darcy, smarks, dholmes
上级 ee4cb325
......@@ -196,7 +196,20 @@ public final class Boolean implements java.io.Serializable,
* {@code true}; returns the integer {@code 1237} if this
* object represents {@code false}.
*/
@Override
public int hashCode() {
return Boolean.hashCode(value);
}
/**
* Returns a hash code for a {@code boolean} value; compatible with
* {@code Boolean.hashCode()}.
*
* @since 1.8
*
* @return a hash code value for a {@code boolean} value.
*/
public static int hashCode(boolean value) {
return value ? 1231 : 1237;
}
......
......@@ -389,7 +389,20 @@ public final class Byte extends Number implements Comparable<Byte> {
*
* @return a hash code value for this {@code Byte}
*/
@Override
public int hashCode() {
return Byte.hashCode(value);
}
/**
* Returns a hash code for a {@code byte} value; compatible with
* {@code Byte.hashCode()}.
*
* @since 1.8
*
* @return a hash code value for a {@code byte} value.
*/
public static int hashCode(byte value) {
return (int)value;
}
......
......@@ -4588,7 +4588,20 @@ class Character implements java.io.Serializable, Comparable<Character> {
*
* @return a hash code value for this {@code Character}
*/
@Override
public int hashCode() {
return Character.hashCode(value);
}
/**
* Returns a hash code for a {@code char} value; compatible with
* {@code Character.hashCode()}.
*
* @since 1.8
*
* @return a hash code value for a {@code char} value.
*/
public static int hashCode(char value) {
return (int)value;
}
......
......@@ -740,7 +740,20 @@ public final class Double extends Number implements Comparable<Double> {
*
* @return a {@code hash code} value for this object.
*/
@Override
public int hashCode() {
return Double.hashCode(value);
}
/**
* Returns a hash code for a {@code double} value; compatible with
* {@code Double.hashCode()}.
*
* @since 1.8
*
* @return a hash code value for a {@code double} value.
*/
public static int hashCode(double value) {
long bits = doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
}
......
......@@ -648,7 +648,20 @@ public final class Float extends Number implements Comparable<Float> {
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
return Float.hashCode(value);
}
/**
* Returns a hash code for a {@code float} value; compatible with
* {@code Float.hashCode()}.
*
* @since 1.8
*
* @return a hash code value for a {@code float} value.
*/
public static int hashCode(float value) {
return floatToIntBits(value);
}
......
......@@ -918,7 +918,20 @@ public final class Integer extends Number implements Comparable<Integer> {
* primitive {@code int} value represented by this
* {@code Integer} object.
*/
@Override
public int hashCode() {
return Integer.hashCode(value);
}
/**
* Returns a hash code for a {@code int} value; compatible with
* {@code Integer.hashCode()}.
*
* @since 1.8
*
* @return a hash code value for a {@code int} value.
*/
public static int hashCode(int value) {
return value;
}
......
......@@ -1021,7 +1021,20 @@ public final class Long extends Number implements Comparable<Long> {
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
return Long.hashCode(value);
}
/**
* Returns a hash code for a {@code long} value; compatible with
* {@code Long.hashCode()}.
*
* @since 1.8
*
* @return a hash code value for a {@code long} value.
*/
public static int hashCode(long value) {
return (int)(value ^ (value >>> 32));
}
......
......@@ -394,7 +394,20 @@ public final class Short extends Number implements Comparable<Short> {
*
* @return a hash code value for this {@code Short}
*/
@Override
public int hashCode() {
return Short.hashCode(value);
}
/**
* Returns a hash code for a {@code short} value; compatible with
* {@code Short.hashCode()}.
*
* @since 1.8
*
* @return a hash code value for a {@code short} value.
*/
public static int hashCode(short value) {
return (int)value;
}
......
......@@ -23,18 +23,20 @@
/*
* @test
* @bug 4245470
* @bug 4245470 7088913
* @summary Test the primitive wrappers hashCode()
*/
import java.util.Objects;
import java.util.Random;
public class HashCode {
final Random rnd = new Random();
void test(String args[]) throws Exception {
int[] ints = {
void testOrdinals(String args[]) throws Exception {
long[] longs = {
Long.MIN_VALUE,
Integer.MIN_VALUE,
Short.MIN_VALUE,
Character.MIN_VALUE,
......@@ -44,20 +46,73 @@ public class HashCode {
Character.MAX_VALUE,
Short.MAX_VALUE,
Integer.MAX_VALUE,
Long.MAX_VALUE,
rnd.nextInt(),
};
for (int x : ints) {
for (long x : longs) {
check( new Long(x).hashCode() == (int)((long)x ^ (long)x>>>32));
check(Long.valueOf(x).hashCode() == (int)((long)x ^ (long)x>>>32));
check( new Integer(x).hashCode() == x);
check(Integer.valueOf(x).hashCode() == x);
check( (new Long(x)).hashCode() == Long.hashCode(x));
check( new Integer((int)x).hashCode() == (int) x);
check(Integer.valueOf((int)x).hashCode() == (int) x);
check( (new Integer((int)x)).hashCode() == Integer.hashCode((int)x));
check( new Short((short)x).hashCode() == (short) x);
check(Short.valueOf((short)x).hashCode() == (short) x);
check( (new Short((short)x)).hashCode() == Short.hashCode((short)x));
check( new Character((char) x).hashCode() == (char) x);
check(Character.valueOf((char) x).hashCode() == (char) x);
check( (new Character((char)x)).hashCode() == Character.hashCode((char)x));
check( new Byte((byte) x).hashCode() == (byte) x);
check(Byte.valueOf((byte) x).hashCode() == (byte) x);
check( (new Byte((byte)x)).hashCode() == Byte.hashCode((byte)x));
}
}
void testBoolean() {
check( Boolean.FALSE.hashCode() == 1237);
check( Boolean.TRUE.hashCode() == 1231);
check( Boolean.valueOf(false).hashCode() == 1237);
check( Boolean.valueOf(true).hashCode() == 1231);
check( (new Boolean(false)).hashCode() == 1237);
check( (new Boolean(true)).hashCode() == 1231);
check( Boolean.hashCode(false) == 1237);
check( Boolean.hashCode(true) == 1231);
}
void testFloat() {
float[] floats = {
Float.NaN,
Float.NEGATIVE_INFINITY,
-1f,
0f,
1f,
Float.POSITIVE_INFINITY
};
for(float f : floats) {
check( Float.hashCode(f) == Float.floatToIntBits(f));
check( Float.valueOf(f).hashCode() == Float.floatToIntBits(f));
check( (new Float(f)).hashCode() == Float.floatToIntBits(f));
}
}
void testDouble() {
double[] doubles = {
Double.NaN,
Double.NEGATIVE_INFINITY,
-1f,
0f,
1f,
Double.POSITIVE_INFINITY
};
for(double d : doubles) {
long bits = Double.doubleToLongBits(d);
int bitsHash = (int)(bits^(bits>>>32));
check( Double.hashCode(d) == bitsHash);
check( Double.valueOf(d).hashCode() == bitsHash);
check( (new Double(d)).hashCode() == bitsHash);
}
}
......@@ -69,12 +124,16 @@ public class HashCode {
void unexpected(Throwable t) {failed++; t.printStackTrace();}
void check(boolean cond) {if (cond) pass(); else fail();}
void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
if (Objects.equals(x,y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
new HashCode().instanceMain(args);}
public void instanceMain(String[] args) throws Throwable {
try {test(args);} catch (Throwable t) {unexpected(t);}
try { testOrdinals(args);
testBoolean();
testFloat();
testDouble();
} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册