AbstractRandomKeyGenerator implements KeyGenerator<S> { @Override public final synchronized S generate() { S value; do { value = getNext(); } while (used.contains(value)); used.add(value); return value; }  @Override final synchronized S generate();  }
@Test public void testThatGeneratorProtectsUsFromDuplicates() { final Set<Integer> generatedKeys = new HashSet<>(); final KeyGenerator<Integer> generator = new DuplicatingKeyGenerator(); for (int i = 0; i < 1000; i++) { final Integer key = generator.generate(); assertThat(generatedKeys, not(hasItem(key))); generatedKeys.add(key); } }