com.google.common.hash.HashCode.bits()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(13.3k)|赞(0)|评价(0)|浏览(90)

本文整理了Java中com.google.common.hash.HashCode.bits()方法的一些代码示例,展示了HashCode.bits()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HashCode.bits()方法的具体详情如下:
包路径:com.google.common.hash.HashCode
类名称:HashCode
方法名:bits

HashCode.bits介绍

[英]Returns the number of bits in this hash code; a positive multiple of 8.
[中]返回此哈希代码中的位数;8的正倍数。

代码示例

代码示例来源:origin: google/guava

/**
 * Returns {@code true} if {@code object} is a {@link HashCode} instance with the identical byte
 * representation to this hash code.
 *
 * <p><b>Security note:</b> this method uses a constant-time (not short-circuiting) implementation
 * to protect against <a href="http://en.wikipedia.org/wiki/Timing_attack">timing attacks</a>.
 */
@Override
public final boolean equals(@Nullable Object object) {
 if (object instanceof HashCode) {
  HashCode that = (HashCode) object;
  return bits() == that.bits() && equalsSameBits(that);
 }
 return false;
}

代码示例来源:origin: google/guava

/**
 * Returns a hash code, having the same bit length as each of the input hash codes, that combines
 * the information of these hash codes in an unordered fashion. That is, whenever two equal hash
 * codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
 * was computed from the <i>same</i> input hash codes in <i>some</i> order.
 *
 * @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
 *     have the same bit length
 */
public static HashCode combineUnordered(Iterable<HashCode> hashCodes) {
 Iterator<HashCode> iterator = hashCodes.iterator();
 checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
 byte[] resultBytes = new byte[iterator.next().bits() / 8];
 for (HashCode hashCode : hashCodes) {
  byte[] nextBytes = hashCode.asBytes();
  checkArgument(
    nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
  for (int i = 0; i < nextBytes.length; i++) {
   resultBytes[i] += nextBytes[i];
  }
 }
 return HashCode.fromBytesNoCopy(resultBytes);
}

代码示例来源:origin: google/guava

/**
 * Returns a hash code, having the same bit length as each of the input hash codes, that combines
 * the information of these hash codes in an ordered fashion. That is, whenever two equal hash
 * codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
 * was computed from the <i>same</i> input hash codes in the <i>same</i> order.
 *
 * @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
 *     have the same bit length
 */
public static HashCode combineOrdered(Iterable<HashCode> hashCodes) {
 Iterator<HashCode> iterator = hashCodes.iterator();
 checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
 int bits = iterator.next().bits();
 byte[] resultBytes = new byte[bits / 8];
 for (HashCode hashCode : hashCodes) {
  byte[] nextBytes = hashCode.asBytes();
  checkArgument(
    nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
  for (int i = 0; i < nextBytes.length; i++) {
   resultBytes[i] = (byte) (resultBytes[i] * 37 ^ nextBytes[i]);
  }
 }
 return HashCode.fromBytesNoCopy(resultBytes);
}

代码示例来源:origin: google/j2objc

/**
 * Returns {@code true} if {@code object} is a {@link HashCode} instance with the identical byte
 * representation to this hash code.
 *
 * <p><b>Security note:</b> this method uses a constant-time (not short-circuiting) implementation
 * to protect against <a href="http://en.wikipedia.org/wiki/Timing_attack">timing attacks</a>.
 */
@Override
public final boolean equals(@NullableDecl Object object) {
 if (object instanceof HashCode) {
  HashCode that = (HashCode) object;
  return bits() == that.bits() && equalsSameBits(that);
 }
 return false;
}

代码示例来源:origin: google/guava

@Override
HashCode makeHash(Hasher[] hashers) {
 byte[] bytes = new byte[bits() / 8];
 int i = 0;
 for (Hasher hasher : hashers) {
  HashCode newHash = hasher.hash();
  i += newHash.writeBytesTo(bytes, i, newHash.bits() / 8);
 }
 return HashCode.fromBytesNoCopy(bytes);
}

代码示例来源:origin: google/j2objc

/**
 * Returns a hash code, having the same bit length as each of the input hash codes, that combines
 * the information of these hash codes in an unordered fashion. That is, whenever two equal hash
 * codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
 * was computed from the <i>same</i> input hash codes in <i>some</i> order.
 *
 * @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
 *     have the same bit length
 */
public static HashCode combineUnordered(Iterable<HashCode> hashCodes) {
 Iterator<HashCode> iterator = hashCodes.iterator();
 checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
 byte[] resultBytes = new byte[iterator.next().bits() / 8];
 for (HashCode hashCode : hashCodes) {
  byte[] nextBytes = hashCode.asBytes();
  checkArgument(
    nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
  for (int i = 0; i < nextBytes.length; i++) {
   resultBytes[i] += nextBytes[i];
  }
 }
 return HashCode.fromBytesNoCopy(resultBytes);
}

代码示例来源:origin: google/j2objc

/**
 * Returns a hash code, having the same bit length as each of the input hash codes, that combines
 * the information of these hash codes in an ordered fashion. That is, whenever two equal hash
 * codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
 * was computed from the <i>same</i> input hash codes in the <i>same</i> order.
 *
 * @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
 *     have the same bit length
 */
public static HashCode combineOrdered(Iterable<HashCode> hashCodes) {
 Iterator<HashCode> iterator = hashCodes.iterator();
 checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
 int bits = iterator.next().bits();
 byte[] resultBytes = new byte[bits / 8];
 for (HashCode hashCode : hashCodes) {
  byte[] nextBytes = hashCode.asBytes();
  checkArgument(
    nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
  for (int i = 0; i < nextBytes.length; i++) {
   resultBytes[i] = (byte) (resultBytes[i] * 37 ^ nextBytes[i]);
  }
 }
 return HashCode.fromBytesNoCopy(resultBytes);
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns {@code true} if {@code object} is a {@link HashCode} instance with the identical byte
 * representation to this hash code.
 *
 * <p><b>Security note:</b> this method uses a constant-time (not short-circuiting) implementation
 * to protect against <a href="http://en.wikipedia.org/wiki/Timing_attack">timing attacks</a>.
 */
@Override
public final boolean equals(@NullableDecl Object object) {
 if (object instanceof HashCode) {
  HashCode that = (HashCode) object;
  return bits() == that.bits() && equalsSameBits(that);
 }
 return false;
}

代码示例来源:origin: google/guava

/**
 * Returns a "Java hash code" for this {@code HashCode} instance; this is well-defined (so, for
 * example, you can safely put {@code HashCode} instances into a {@code HashSet}) but is otherwise
 * probably not what you want to use.
 */
@Override
public final int hashCode() {
 // If we have at least 4 bytes (32 bits), just take the first 4 bytes. Since this is
 // already a (presumably) high-quality hash code, any four bytes of it will do.
 if (bits() >= 32) {
  return asInt();
 }
 // If we have less than 4 bytes, use them all.
 byte[] bytes = getBytesInternal();
 int val = (bytes[0] & 0xFF);
 for (int i = 1; i < bytes.length; i++) {
  val |= ((bytes[i] & 0xFF) << (i * 8));
 }
 return val;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns a hash code, having the same bit length as each of the input hash codes, that combines
 * the information of these hash codes in an unordered fashion. That is, whenever two equal hash
 * codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
 * was computed from the <i>same</i> input hash codes in <i>some</i> order.
 *
 * @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
 *     have the same bit length
 */
public static HashCode combineUnordered(Iterable<HashCode> hashCodes) {
 Iterator<HashCode> iterator = hashCodes.iterator();
 checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
 byte[] resultBytes = new byte[iterator.next().bits() / 8];
 for (HashCode hashCode : hashCodes) {
  byte[] nextBytes = hashCode.asBytes();
  checkArgument(
    nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
  for (int i = 0; i < nextBytes.length; i++) {
   resultBytes[i] += nextBytes[i];
  }
 }
 return HashCode.fromBytesNoCopy(resultBytes);
}

代码示例来源:origin: google/guava

private static void assertReadableBytes(HashCode hashCode) {
 assertTrue(hashCode.bits() >= 32); // sanity
 byte[] hashBytes = hashCode.asBytes();
 int totalBytes = hashCode.bits() / 8;
 for (int bytes = 0; bytes < totalBytes; bytes++) {
  byte[] bb = new byte[bytes];
  hashCode.writeBytesTo(bb, 0, bb.length);
  assertTrue(Arrays.equals(Arrays.copyOf(hashBytes, bytes), bb));
 }
}

代码示例来源:origin: google/j2objc

@Override
HashCode makeHash(Hasher[] hashers) {
 byte[] bytes = new byte[bits() / 8];
 int i = 0;
 for (Hasher hasher : hashers) {
  HashCode newHash = hasher.hash();
  i += newHash.writeBytesTo(bytes, i, newHash.bits() / 8);
 }
 return HashCode.fromBytesNoCopy(bytes);
}

代码示例来源:origin: google/j2objc

/**
 * Returns a "Java hash code" for this {@code HashCode} instance; this is well-defined (so, for
 * example, you can safely put {@code HashCode} instances into a {@code HashSet}) but is otherwise
 * probably not what you want to use.
 */
@Override
public final int hashCode() {
 // If we have at least 4 bytes (32 bits), just take the first 4 bytes. Since this is
 // already a (presumably) high-quality hash code, any four bytes of it will do.
 if (bits() >= 32) {
  return asInt();
 }
 // If we have less than 4 bytes, use them all.
 byte[] bytes = getBytesInternal();
 int val = (bytes[0] & 0xFF);
 for (int i = 1; i < bytes.length; i++) {
  val |= ((bytes[i] & 0xFF) << (i * 8));
 }
 return val;
}

代码示例来源:origin: wildfly/wildfly

@Override
HashCode makeHash(Hasher[] hashers) {
 byte[] bytes = new byte[bits() / 8];
 int i = 0;
 for (Hasher hasher : hashers) {
  HashCode newHash = hasher.hash();
  i += newHash.writeBytesTo(bytes, i, newHash.bits() / 8);
 }
 return HashCode.fromBytesNoCopy(bytes);
}

代码示例来源:origin: google/guava

/**
 * Copies bytes from this hash code into {@code dest}.
 *
 * @param dest the byte array into which the hash code will be written
 * @param offset the start offset in the data
 * @param maxLength the maximum number of bytes to write
 * @return the number of bytes written to {@code dest}
 * @throws IndexOutOfBoundsException if there is not enough room in {@code dest}
 */
@CanIgnoreReturnValue
public int writeBytesTo(byte[] dest, int offset, int maxLength) {
 maxLength = Ints.min(maxLength, bits() / 8);
 Preconditions.checkPositionIndexes(offset, offset + maxLength, dest.length);
 writeBytesToImpl(dest, offset, maxLength);
 return maxLength;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns a "Java hash code" for this {@code HashCode} instance; this is well-defined (so, for
 * example, you can safely put {@code HashCode} instances into a {@code HashSet}) but is otherwise
 * probably not what you want to use.
 */
@Override
public final int hashCode() {
 // If we have at least 4 bytes (32 bits), just take the first 4 bytes. Since this is
 // already a (presumably) high-quality hash code, any four bytes of it will do.
 if (bits() >= 32) {
  return asInt();
 }
 // If we have less than 4 bytes, use them all.
 byte[] bytes = getBytesInternal();
 int val = (bytes[0] & 0xFF);
 for (int i = 1; i < bytes.length; i++) {
  val |= ((bytes[i] & 0xFF) << (i * 8));
 }
 return val;
}

代码示例来源:origin: google/j2objc

/**
 * Copies bytes from this hash code into {@code dest}.
 *
 * @param dest the byte array into which the hash code will be written
 * @param offset the start offset in the data
 * @param maxLength the maximum number of bytes to write
 * @return the number of bytes written to {@code dest}
 * @throws IndexOutOfBoundsException if there is not enough room in {@code dest}
 */
@CanIgnoreReturnValue
public int writeBytesTo(byte[] dest, int offset, int maxLength) {
 maxLength = Ints.min(maxLength, bits() / 8);
 Preconditions.checkPositionIndexes(offset, offset + maxLength, dest.length);
 writeBytesToImpl(dest, offset, maxLength);
 return maxLength;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Copies bytes from this hash code into {@code dest}.
 *
 * @param dest the byte array into which the hash code will be written
 * @param offset the start offset in the data
 * @param maxLength the maximum number of bytes to write
 * @return the number of bytes written to {@code dest}
 * @throws IndexOutOfBoundsException if there is not enough room in {@code dest}
 */
@CanIgnoreReturnValue
public int writeBytesTo(byte[] dest, int offset, int maxLength) {
 maxLength = Ints.min(maxLength, bits() / 8);
 Preconditions.checkPositionIndexes(offset, offset + maxLength, dest.length);
 writeBytesToImpl(dest, offset, maxLength);
 return maxLength;
}

代码示例来源:origin: google/guava

/**
 * Checks that a Hasher returns the same HashCode when given the same input, and also that the
 * collision rate looks sane.
 */
static void assertInvariants(HashFunction hashFunction) {
 int objects = 100;
 Set<HashCode> hashcodes = Sets.newHashSetWithExpectedSize(objects);
 Random random = new Random(314159);
 for (int i = 0; i < objects; i++) {
  int value = random.nextInt();
  HashCode hashcode1 = hashFunction.hashInt(value);
  HashCode hashcode2 = hashFunction.hashInt(value);
  Assert.assertEquals(hashcode1, hashcode2); // idempotent
  Assert.assertEquals(hashFunction.bits(), hashcode1.bits());
  Assert.assertEquals(hashFunction.bits(), hashcode1.asBytes().length * 8);
  hashcodes.add(hashcode1);
 }
 Assert.assertTrue(hashcodes.size() > objects * 0.95); // quite relaxed test
 assertHashBytesThrowsCorrectExceptions(hashFunction);
 assertIndependentHashers(hashFunction);
 assertShortcutsAreEquivalent(hashFunction, 512);
}

代码示例来源:origin: google/guava

private static void assertExpectedHashCode(ExpectedHashCode expectedHashCode, HashCode hash) {
 assertTrue(Arrays.equals(expectedHashCode.bytes, hash.asBytes()));
 byte[] bb = new byte[hash.bits() / 8];
 hash.writeBytesTo(bb, 0, bb.length);
 assertTrue(Arrays.equals(expectedHashCode.bytes, bb));
 assertEquals(expectedHashCode.asInt, hash.asInt());
 if (expectedHashCode.asLong == null) {
  try {
   hash.asLong();
   fail();
  } catch (IllegalStateException expected) {
  }
 } else {
  assertEquals(expectedHashCode.asLong.longValue(), hash.asLong());
 }
 assertEquals(expectedHashCode.toString, hash.toString());
 assertSideEffectFree(hash);
 assertReadableBytes(hash);
}

相关文章