TArrays { public static ByteBuffer hash(Object... objects) { ByteBuffer bytes = ByteBuffer.allocate(8 * objects.length); for (Object object : objects) { bytes.putInt(object.hashCode()); bytes.putInt(object.getClass().getName().hashCode()); } bytes.rewind(); return bytes; }  static ByteBuffer hash(Object... objects);  }
@Test public void testConsistentHashing() { List<Integer> bytes = Lists.newArrayList(0, 1, -116, -58, -63, -90, 58, -104, 0, 67, 101, -107, 115, 59, 73, 102, 0, 0, 0, 1, -39, 33, 58, 40); Object[] data = { Text.wrap("foo"), Value.wrap(Convert.javaToThrift("bar")), PrimaryKey.wrap(1) }; ByteBuffer buf = TArrays.hash(data); System.out.println(buf); for (int i = 0; i < buf.capacity(); i++) { Assert.assertEquals(buf.get(), bytes.get(i).byteValue()); } }
@Test public void testEqualObjectsHaveEqualHash() { Object[] data = { TestData.getText(), TestData.getValue(), TestData.getPrimaryKey() }; ByteBuffer a = TArrays.hash(data); ByteBuffer b = TArrays.hash(data); Assert.assertEquals(a, b); }