LimitDataGenerator extends DataGenerator<A> { @Override public DataGenerator<A> apply() { if (remaining == 0) { return (DataGenerator<A>) EOS; } DataGenerator nextGen = source.apply(); if (nextGen.isClosed()) { return (DataGenerator<A>) EOS; } return new LimitDataGenerator<>(nextGen, remaining); } private  LimitDataGenerator(DataGenerator<A> source, int count); @Override DataGenerator<A> apply(); @Override Optional<A> getOptionalValue(); static DataGenerator<A> newInstance(DataGenerator<A> source, int count);  }
@Test public void testLimit() throws Exception { DataGenerator<Long> genWithLimit = DataGenerator.range(0, 9).limit(2); DataGenerator<List<Long>> batchGen = genWithLimit.batch(2); assertThat(batchGen.getValue()).isEqualTo(asList(0L, 1L)); assertThat(batchGen.apply().isClosed()).isTrue(); }