HashingIdentifierSource implements IdentifierSource { @Override public int calculateId(String name, Set<Integer> usedIds) { if (usedIds.size() == this.allowedIdentifierCount) { throw new IllegalStateException("All identifiers in use; cannot assign another"); } int value = name.hashCode(); if (value == Integer.MIN_VALUE) { value = 0; } value = Math.abs(value) % allowedIdentifierCount; while (usedIds.contains(value)) { value++; if (value == allowedIdentifierCount) { value = 0; } } return value; } HashingIdentifierSource(int allowedIdentifierCount); @Override int calculateId(String name, Set<Integer> usedIds);  }
@Test(expected = IllegalStateException.class) public void hashWithAllValuesUsedShouldThrow() { System.out.println(new HashingIdentifierSource(3).calculateId(stringWhichHashesTo(2), Sets.newHashSet(0, 1, 2))); }