8300647: Miscellaneous hashCode improvements in java.base

Reviewed-by: stsypanov, rriggs
This commit is contained in:
Claes Redestad 2023-01-19 16:50:07 +00:00
parent 453dbd12ee
commit d85243f02b
2 changed files with 6 additions and 36 deletions

View file

@ -145,11 +145,11 @@ final class ProcessEnvironment
public boolean equals(Object o) {
return o instanceof ExternalData
&& arrayEquals(getBytes(), ((ExternalData) o).getBytes());
&& Arrays.equals(getBytes(), ((ExternalData) o).getBytes());
}
public int hashCode() {
return arrayHash(getBytes());
return Arrays.hashCode(getBytes());
}
}
@ -178,7 +178,7 @@ final class ProcessEnvironment
}
public int compareTo(Variable variable) {
return arrayCompare(getBytes(), variable.getBytes());
return Arrays.compare(getBytes(), variable.getBytes());
}
public boolean equals(Object o) {
@ -211,7 +211,7 @@ final class ProcessEnvironment
}
public int compareTo(Value value) {
return arrayCompare(getBytes(), value.getBytes());
return Arrays.compare(getBytes(), value.getBytes());
}
public boolean equals(Object o) {
@ -412,31 +412,4 @@ final class ProcessEnvironment
}
}
// Replace with general purpose method someday
private static int arrayCompare(byte[]x, byte[] y) {
int min = x.length < y.length ? x.length : y.length;
for (int i = 0; i < min; i++)
if (x[i] != y[i])
return x[i] - y[i];
return x.length - y.length;
}
// Replace with general purpose method someday
private static boolean arrayEquals(byte[] x, byte[] y) {
if (x.length != y.length)
return false;
for (int i = 0; i < x.length; i++)
if (x[i] != y[i])
return false;
return true;
}
// Replace with general purpose method someday
private static int arrayHash(byte[] x) {
int hash = 0;
for (int i = 0; i < x.length; i++)
hash = 31 * hash + x[i];
return hash;
}
}