package au.id.zancanaro.javacheck; import java.util.List; import java.util.Random; import java.util.function.Function; public interface Generator { RoseTree generate(Random random, int size); static Generator pure(T value) { return (random, size) -> RoseTree.pure(value); } @SafeVarargs static Generator> tuple(Generator... generators) { return (random, size) -> { @SuppressWarnings("unchecked") RoseTree[] result = (RoseTree[]) new RoseTree[generators.length]; int index = 0; for (Generator generator : generators) { result[index++] = generator.generate(random, size); } return RoseTree.zip(Function.identity(), result); }; } static Generator> list(int count, Generator gen) { return (random, size) -> { @SuppressWarnings("unchecked") RoseTree[] result = (RoseTree[]) new RoseTree[count]; for (int i = 0; i < count; ++i) { result[i] = gen.generate(random, size); } return RoseTree.shrink(Function.identity(), result); }; } default Generator map(Function f) { return (random, size) -> this.generate(random, size).map(f); } default Generator flatMap(Function> action) { return (random, size) -> RoseTree.join(this.generate(random, size).map(action).map(g -> g.generate(random, size))); } }