what-the-bank/sources/com/google/common/hash/HashCode.java

281 lines
9.1 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.hash;
import com.google.common.base.Preconditions;
import com.google.common.primitives.Ints;
import com.google.common.primitives.UnsignedBytes;
import com.google.common.primitives.UnsignedInts;
import java.io.Serializable;
/* loaded from: classes2.dex */
public abstract class HashCode {
private static final char[] hexDigits = "0123456789abcdef".toCharArray();
public abstract byte[] asBytes();
public abstract int asInt();
public abstract long asLong();
public abstract int bits();
abstract boolean equalsSameBits(HashCode hashCode);
public abstract long padToLong();
abstract void writeBytesToImpl(byte[] bArr, int i, int i2);
HashCode() {
}
public int writeBytesTo(byte[] bArr, int i, int i2) {
int min = Ints.min(i2, bits() / 8);
Preconditions.checkPositionIndexes(i, i + min, bArr.length);
writeBytesToImpl(bArr, i, min);
return min;
}
/* JADX INFO: Access modifiers changed from: package-private */
public byte[] getBytesInternal() {
return asBytes();
}
public static HashCode fromInt(int i) {
return new IntHashCode(i);
}
/* loaded from: classes2.dex */
static final class IntHashCode extends HashCode implements Serializable {
private static final long serialVersionUID = 0;
final int hash;
@Override // com.google.common.hash.HashCode
public final int bits() {
return 32;
}
IntHashCode(int i) {
this.hash = i;
}
@Override // com.google.common.hash.HashCode
public final long asLong() {
throw new IllegalStateException("this HashCode only has 32 bits; cannot create a long");
}
@Override // com.google.common.hash.HashCode
public final long padToLong() {
return UnsignedInts.toLong(this.hash);
}
@Override // com.google.common.hash.HashCode
final void writeBytesToImpl(byte[] bArr, int i, int i2) {
for (int i3 = 0; i3 < i2; i3++) {
bArr[i + i3] = (byte) (this.hash >> (i3 << 3));
}
}
@Override // com.google.common.hash.HashCode
final boolean equalsSameBits(HashCode hashCode) {
return this.hash == hashCode.asInt();
}
@Override // com.google.common.hash.HashCode
public final int asInt() {
return this.hash;
}
@Override // com.google.common.hash.HashCode
public final byte[] asBytes() {
int i = this.hash;
return new byte[]{(byte) i, (byte) (i >> 8), (byte) (i >> 16), (byte) (i >> 24)};
}
}
public static HashCode fromLong(long j) {
return new LongHashCode(j);
}
/* loaded from: classes2.dex */
static final class LongHashCode extends HashCode implements Serializable {
private static final long serialVersionUID = 0;
final long hash;
@Override // com.google.common.hash.HashCode
public final int bits() {
return 64;
}
LongHashCode(long j) {
this.hash = j;
}
@Override // com.google.common.hash.HashCode
final void writeBytesToImpl(byte[] bArr, int i, int i2) {
for (int i3 = 0; i3 < i2; i3++) {
bArr[i + i3] = (byte) (this.hash >> (i3 << 3));
}
}
@Override // com.google.common.hash.HashCode
final boolean equalsSameBits(HashCode hashCode) {
return this.hash == hashCode.asLong();
}
@Override // com.google.common.hash.HashCode
public final long padToLong() {
return this.hash;
}
@Override // com.google.common.hash.HashCode
public final long asLong() {
return this.hash;
}
@Override // com.google.common.hash.HashCode
public final int asInt() {
return (int) this.hash;
}
@Override // com.google.common.hash.HashCode
public final byte[] asBytes() {
return new byte[]{(byte) this.hash, (byte) (r0 >> 8), (byte) (r0 >> 16), (byte) (r0 >> 24), (byte) (r0 >> 32), (byte) (r0 >> 40), (byte) (r0 >> 48), (byte) (r0 >> 56)};
}
}
public static HashCode fromBytes(byte[] bArr) {
Preconditions.checkArgument(bArr.length > 0, "A HashCode must contain at least 1 byte.");
return fromBytesNoCopy((byte[]) bArr.clone());
}
/* JADX INFO: Access modifiers changed from: package-private */
public static HashCode fromBytesNoCopy(byte[] bArr) {
return new BytesHashCode(bArr);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class BytesHashCode extends HashCode implements Serializable {
private static final long serialVersionUID = 0;
final byte[] bytes;
BytesHashCode(byte[] bArr) {
this.bytes = (byte[]) Preconditions.checkNotNull(bArr);
}
@Override // com.google.common.hash.HashCode
public final int bits() {
return this.bytes.length << 3;
}
@Override // com.google.common.hash.HashCode
public final byte[] asBytes() {
return (byte[]) this.bytes.clone();
}
@Override // com.google.common.hash.HashCode
public final int asInt() {
byte[] bArr = this.bytes;
Preconditions.checkState(bArr.length >= 4, "HashCode#asInt() requires >= 4 bytes (it only has %s bytes).", bArr.length);
byte[] bArr2 = this.bytes;
return ((bArr2[3] & UnsignedBytes.MAX_VALUE) << 24) | (bArr2[0] & UnsignedBytes.MAX_VALUE) | ((bArr2[1] & UnsignedBytes.MAX_VALUE) << 8) | ((bArr2[2] & UnsignedBytes.MAX_VALUE) << 16);
}
@Override // com.google.common.hash.HashCode
public final long asLong() {
byte[] bArr = this.bytes;
Preconditions.checkState(bArr.length >= 8, "HashCode#asLong() requires >= 8 bytes (it only has %s bytes).", bArr.length);
return padToLong();
}
@Override // com.google.common.hash.HashCode
public final long padToLong() {
long j = this.bytes[0] & UnsignedBytes.MAX_VALUE;
for (int i = 1; i < Math.min(this.bytes.length, 8); i++) {
j |= (this.bytes[i] & 255) << (i << 3);
}
return j;
}
@Override // com.google.common.hash.HashCode
final void writeBytesToImpl(byte[] bArr, int i, int i2) {
System.arraycopy(this.bytes, 0, bArr, i, i2);
}
@Override // com.google.common.hash.HashCode
final boolean equalsSameBits(HashCode hashCode) {
if (this.bytes.length != hashCode.getBytesInternal().length) {
return false;
}
boolean z = true;
int i = 0;
while (true) {
byte[] bArr = this.bytes;
if (i >= bArr.length) {
return z;
}
z &= bArr[i] == hashCode.getBytesInternal()[i];
i++;
}
}
@Override // com.google.common.hash.HashCode
final byte[] getBytesInternal() {
return this.bytes;
}
}
public static HashCode fromString(String str) {
Preconditions.checkArgument(str.length() >= 2, "input string (%s) must have at least 2 characters", str);
Preconditions.checkArgument(str.length() % 2 == 0, "input string (%s) must have an even number of characters", str);
byte[] bArr = new byte[str.length() / 2];
for (int i = 0; i < str.length(); i += 2) {
bArr[i / 2] = (byte) ((decode(str.charAt(i)) << 4) + decode(str.charAt(i + 1)));
}
return fromBytesNoCopy(bArr);
}
private static int decode(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'a' && c <= 'f') {
return c - 'W';
}
StringBuilder sb = new StringBuilder(32);
sb.append("Illegal hexadecimal character: ");
sb.append(c);
throw new IllegalArgumentException(sb.toString());
}
public final boolean equals(Object obj) {
if (!(obj instanceof HashCode)) {
return false;
}
HashCode hashCode = (HashCode) obj;
return bits() == hashCode.bits() && equalsSameBits(hashCode);
}
public final int hashCode() {
if (bits() >= 32) {
return asInt();
}
byte[] bytesInternal = getBytesInternal();
int i = bytesInternal[0] & UnsignedBytes.MAX_VALUE;
for (int i2 = 1; i2 < bytesInternal.length; i2++) {
i |= (bytesInternal[i2] & UnsignedBytes.MAX_VALUE) << (i2 << 3);
}
return i;
}
public final String toString() {
byte[] bytesInternal = getBytesInternal();
StringBuilder sb = new StringBuilder(bytesInternal.length << 1);
for (byte b : bytesInternal) {
char[] cArr = hexDigits;
sb.append(cArr[(b >> 4) & 15]);
sb.append(cArr[b & 15]);
}
return sb.toString();
}
}