8312019: Simplify and modernize java.util.BitSet.equals

Reviewed-by: rriggs, martin
This commit is contained in:
Pavel Rappo 2023-07-20 13:54:45 +00:00
parent fe41910141
commit 9fa944e63f

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -33,6 +33,8 @@ import java.util.function.IntConsumer;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
/** /**
* This class implements a vector of bits that grows as needed. Each * This class implements a vector of bits that grows as needed. Each
* component of the bit set has a {@code boolean} value. The * component of the bit set has a {@code boolean} value. The
@ -1015,8 +1017,10 @@ public class BitSet implements Cloneable, java.io.Serializable {
} }
/** /**
* Returns the hash code value for this bit set. The hash code depends * {@return the hash code value for this bit set}
* only on which bits are set within this {@code BitSet}. *
* The hash code depends only on which bits are set within this
* {@code BitSet}.
* *
* <p>The hash code is defined to be the result of the following * <p>The hash code is defined to be the result of the following
* calculation: * calculation:
@ -1029,9 +1033,8 @@ public class BitSet implements Cloneable, java.io.Serializable {
* return (int)((h >> 32) ^ h); * return (int)((h >> 32) ^ h);
* }}</pre> * }}</pre>
* Note that the hash code changes if the set of bits is altered. * Note that the hash code changes if the set of bits is altered.
*
* @return the hash code value for this bit set
*/ */
@Override
public int hashCode() { public int hashCode() {
long h = 1234; long h = 1234;
for (int i = wordsInUse; --i >= 0; ) for (int i = wordsInUse; --i >= 0; )
@ -1052,7 +1055,7 @@ public class BitSet implements Cloneable, java.io.Serializable {
} }
/** /**
* Compares this object against the specified object. * Compares this bit set against the specified object.
* The result is {@code true} if and only if the argument is * The result is {@code true} if and only if the argument is
* not {@code null} and is a {@code BitSet} object that has * not {@code null} and is a {@code BitSet} object that has
* exactly the same set of bits set to {@code true} as this bit * exactly the same set of bits set to {@code true} as this bit
@ -1065,11 +1068,12 @@ public class BitSet implements Cloneable, java.io.Serializable {
* {@code false} otherwise * {@code false} otherwise
* @see #size() * @see #size()
*/ */
@Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (!(obj instanceof BitSet set))
return false;
if (this == obj) if (this == obj)
return true; return true;
if (!(obj instanceof BitSet set))
return false;
checkInvariants(); checkInvariants();
set.checkInvariants(); set.checkInvariants();
@ -1078,11 +1082,7 @@ public class BitSet implements Cloneable, java.io.Serializable {
return false; return false;
// Check words in use by both BitSets // Check words in use by both BitSets
for (int i = 0; i < wordsInUse; i++) return ArraysSupport.mismatch(words, 0, set.words, 0, wordsInUse) == -1;
if (words[i] != set.words[i])
return false;
return true;
} }
/** /**