SHA256UniformMapper implements UniformMapper<K,V> { @Override public double map(K key, V val) { int h = Math.abs(key.hashCode()); String str = String.valueOf(key.hashCode()); byte[] hash = getDigest().digest(str.getBytes(StandardCharsets.UTF_8)); byte hashByte = hash[h % hash.length]; byte resByte = 0; for (int i = 0; i < 8; i++) resByte = (byte)(resByte << 1 | ((hashByte >> shuffleStgy.get(i)) & 0x1)); return 1.0 * (resByte & 0xFF) / 256; } SHA256UniformMapper();  SHA256UniformMapper(Random random); @Override double map(K key, V val);  }
@Test public void testMap() { UniformMapper<Integer, Integer> mapper = new SHA256UniformMapper<>(new Random(42)); int cnt = 0; for (int i = 0; i < 100_000; i++) { double pnt = mapper.map(i, i); if (pnt < 0.2) cnt++; } double err = 1.0 * Math.abs(cnt - 20_000) / 20_000; assertTrue(err < 0.02); }
@Test public void testMapAndMapAgain() { UniformMapper<Integer, Integer> firstMapper = new SHA256UniformMapper<>(new Random(42)); UniformMapper<Integer, Integer> secondMapper = new SHA256UniformMapper<>(new Random(21)); int cnt = 0; for (int i = 0; i < 100_000; i++) { double firstPnt = firstMapper.map(i, i); double secondPnt = secondMapper.map(i, i); if (firstPnt < 0.5 && secondPnt < 0.5) cnt++; } double err = 1.0 * Math.abs(cnt - 25_000) / 25_000; assertTrue(err < 0.02); }