8311170: Simplify and modernize equals and hashCode in security area

Reviewed-by: djelinski, rriggs, valeriep
This commit is contained in:
Pavel Rappo 2023-08-09 12:34:40 +00:00
parent e9f751ab16
commit 19ae62ae2c
96 changed files with 567 additions and 951 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,7 @@
package sun.security.util;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import jdk.internal.util.Preconditions;
@ -68,7 +69,7 @@ public class BitArray {
* Creates a BitArray of the specified size, initialized from the
* specified byte array. The most significant bit of {@code a[0]} gets
* index zero in the BitArray. The array must be large enough to specify
* a value for every bit of the BitArray. i.e. {@code 8*a.length <= length}.
* a value for every bit of the BitArray, i.e. {@code 8*a.length >= length}.
*/
public BitArray(int length, byte[] a) throws IllegalArgumentException {
this(length, a, 0);
@ -79,7 +80,7 @@ public class BitArray {
* specified byte array starting at the specified offset. The most
* significant bit of {@code a[ofs]} gets index zero in the BitArray.
* The array must be large enough to specify a value for every bit of
* the BitArray, i.e. {@code 8*(a.length - ofs) <= length}.
* the BitArray, i.e. {@code 8*(a.length - ofs) >= length}.
*/
public BitArray(int length, byte[] a, int ofs)
throws IllegalArgumentException {
@ -177,16 +178,13 @@ public class BitArray {
return repn.clone();
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof BitArray ba)) return false;
if (ba.length != length) return false;
for (int i = 0; i < repn.length; i += 1) {
if (repn[i] != ba.repn[i]) return false;
}
return true;
return obj instanceof BitArray other
&& length == other.length
&& Arrays.equals(repn, other.repn);
}
/**
@ -202,17 +200,11 @@ public class BitArray {
}
/**
* Returns a hash code value for this bit array.
*
* @return a hash code value for this bit array.
* {@return a hash code value for this bit array}
*/
@Override
public int hashCode() {
int hashCode = 0;
for (int i = 0; i < repn.length; i++)
hashCode = 31*hashCode + repn[i];
return hashCode ^ length;
return Arrays.hashCode(repn) ^ length;
}