package au.id.zancanaro; import java.util.Iterator; import java.util.function.Predicate; public class Generators { public static Generator suchThat(Generator gen, Predicate pred) { return (random, size) -> { RoseTree result = gen.generate(random, size); if (pred.test(result.getValue())) { return result.filter(pred); } else { return suchThat(gen, pred).generate(random, size); } }; } public static Generator integer() { return (random, size) -> { int value = random.nextInt(size); return new RoseTree<>(value, intShrinkingIterable(value)); }; } private static Iterable> intShrinkingIterable(final int value) { return () -> new Iterator>() { int curr = value; @Override public boolean hasNext() { return curr > 0; } @Override public RoseTree next() { int prevCurr = curr; curr = curr / 2; return new RoseTree<>(value - prevCurr, intShrinkingIterable(value - prevCurr)); } }; } }