ByteKey implements Comparable<ByteKey>, Serializable { @Override public int hashCode() { return value.hashCode(); } private  ByteKey(ByteString value); static ByteKey copyFrom(ByteBuffer value); static ByteKey copyFrom(byte[] bytes); static ByteKey of(int... bytes); ByteBuffer getValue(); byte[] getBytes(); boolean isEmpty(); @Override int compareTo(@Nonnull ByteKey other); @Override String toString(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); static final ByteKey EMPTY; }
@Test public void testHashCode() { int collisions = 0; for (int i = 0; i < TEST_KEYS.length; ++i) { int left = TEST_KEYS[i].hashCode(); int leftClone = ByteKey.copyFrom(TEST_KEYS[i].getValue()).hashCode(); assertEquals( String.format("Expected same hash code for %s and a copy of itself", TEST_KEYS[i]), left, leftClone); for (int j = i + 1; j < TEST_KEYS.length; ++j) { int right = TEST_KEYS[j].hashCode(); if (left == right) { ++collisions; } } } int totalUnequalTests = TEST_KEYS.length * (TEST_KEYS.length - 1) / 2; assertThat("Too many hash collisions", collisions, lessThan(totalUnequalTests / 2)); }