RandomSampler implements Sampler<E> { private Set<E> sample(E[] array, int size, int consider) { if (size == 0) { return new HashSet<>(); } final Set<E> sample = sample(array, size - 1, consider - 1); E chosen = array[new Random().nextInt(consider)]; final E last = array[consider - 1]; if (sample.contains(chosen) && !chosen.equals(last)) { sample.add(last); } else { sample.add(chosen); } return sample; }  @Override E[] sample(E[] array, int size);  }
@Test public void assertThatSamplerSamplesSubsetOfInput() { final int size = input.length - 5; final Integer[] output = sampler.sample(input, size); assertThat(output, is(notNullValue())); assertThat(output.length, is(size)); assertThat(Arrays.asList(output), everyItem(isIn(input))); }
@Test(expectedExceptions = IllegalArgumentException.class) public void assertThatSampleSizeCannotExceedInputSize() { final int size = input.length + 1; sampler.sample(input, size); }
@Test(expectedExceptions = IllegalArgumentException.class) public void assertThatSampleCanOnlySampleSubsetOfTheInput() { final int size = input.length; sampler.sample(input, size); }