Skip to content

Commit 9fa944e

Browse files
author
Pavel Rappo
committedJul 20, 2023
8312019: Simplify and modernize java.util.BitSet.equals
Reviewed-by: rriggs, martin
1 parent fe41910 commit 9fa944e

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed
 

‎src/java.base/share/classes/java/util/BitSet.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,6 +33,8 @@
3333
import java.util.stream.IntStream;
3434
import java.util.stream.StreamSupport;
3535

36+
import jdk.internal.util.ArraysSupport;
37+
3638
/**
3739
* This class implements a vector of bits that grows as needed. Each
3840
* component of the bit set has a {@code boolean} value. The
@@ -1015,8 +1017,10 @@ public void andNot(BitSet set) {
10151017
}
10161018

10171019
/**
1018-
* Returns the hash code value for this bit set. The hash code depends
1019-
* only on which bits are set within this {@code BitSet}.
1020+
* {@return the hash code value for this bit set}
1021+
*
1022+
* The hash code depends only on which bits are set within this
1023+
* {@code BitSet}.
10201024
*
10211025
* <p>The hash code is defined to be the result of the following
10221026
* calculation:
@@ -1029,9 +1033,8 @@ public void andNot(BitSet set) {
10291033
* return (int)((h >> 32) ^ h);
10301034
* }}</pre>
10311035
* Note that the hash code changes if the set of bits is altered.
1032-
*
1033-
* @return the hash code value for this bit set
10341036
*/
1037+
@Override
10351038
public int hashCode() {
10361039
long h = 1234;
10371040
for (int i = wordsInUse; --i >= 0; )
@@ -1052,7 +1055,7 @@ public int size() {
10521055
}
10531056

10541057
/**
1055-
* Compares this object against the specified object.
1058+
* Compares this bit set against the specified object.
10561059
* The result is {@code true} if and only if the argument is
10571060
* not {@code null} and is a {@code BitSet} object that has
10581061
* exactly the same set of bits set to {@code true} as this bit
@@ -1065,11 +1068,12 @@ public int size() {
10651068
* {@code false} otherwise
10661069
* @see #size()
10671070
*/
1071+
@Override
10681072
public boolean equals(Object obj) {
1069-
if (!(obj instanceof BitSet set))
1070-
return false;
10711073
if (this == obj)
10721074
return true;
1075+
if (!(obj instanceof BitSet set))
1076+
return false;
10731077

10741078
checkInvariants();
10751079
set.checkInvariants();
@@ -1078,11 +1082,7 @@ public boolean equals(Object obj) {
10781082
return false;
10791083

10801084
// Check words in use by both BitSets
1081-
for (int i = 0; i < wordsInUse; i++)
1082-
if (words[i] != set.words[i])
1083-
return false;
1084-
1085-
return true;
1085+
return ArraysSupport.mismatch(words, 0, set.words, 0, wordsInUse) == -1;
10861086
}
10871087

10881088
/**

0 commit comments

Comments
 (0)
Please sign in to comment.