### Question:
HashSharder implements Sharder { public int getShard(String id) throws SharderException { long a = hash(id); return (int) ((a % numShards + numShards) % numShards); } HashSharder(int numShards); int getShard(String id);  }### Answer:
@Test public void testBasics() throws IOException, NoSuchAlgorithmException, SharderException { HashSharder sharder = new HashSharder(3); assertEquals(1, sharder.getShard(String.valueOf("alpha"))); assertEquals(2, sharder.getShard(String.valueOf("beta"))); assertEquals(1, sharder.getShard(String.valueOf("gamma"))); assertEquals(2, sharder.getShard(String.valueOf("delta"))); }
@Test public void testIndexOutOfBounds() throws IOException, NoSuchAlgorithmException, SharderException { HashSharder sharder = new HashSharder(3); Random rg = new Random(); for (int i = 0; i < 100; i++) { int shard = sharder.getShard("foo" + rg.nextInt()); assertTrue("shard should be between 0 (inclusive) and 3 (exclusive)", shard >= 0 && shard < 3); } }