summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/javacheck/Generator.java
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-01 11:41:16 +1000
committerCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-01 11:41:16 +1000
commit8187f024bae57267af514c5dcb730de09e573e41 (patch)
treecba17e2e770de4972f57b60cdd443248fd68c458 /src/main/java/au/id/zancanaro/javacheck/Generator.java
parentedfce37bc21699042baf14ad6d172d3187fe530c (diff)
Move packages, make lists shrink in size, generate lists instead of arrays as the 'primitive' operation (issues with generics)
Diffstat (limited to 'src/main/java/au/id/zancanaro/javacheck/Generator.java')
-rw-r--r--src/main/java/au/id/zancanaro/javacheck/Generator.java36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/Generator.java b/src/main/java/au/id/zancanaro/javacheck/Generator.java
new file mode 100644
index 0000000..66ebf1b
--- /dev/null
+++ b/src/main/java/au/id/zancanaro/javacheck/Generator.java
@@ -0,0 +1,36 @@
+package au.id.zancanaro.javacheck;
+
+import java.util.List;
+import java.util.Random;
+import java.util.function.Function;
+
+public interface Generator<T> {
+ RoseTree<T> generate(Random random, int size);
+
+ static <T> Generator<T> pure(T value) {
+ return (random, size) -> RoseTree.pure(value);
+ }
+
+ @SafeVarargs
+ static <T> Generator<List<T>> tuple(Generator<T>... generators) {
+ return (random, size) -> {
+ @SuppressWarnings("unchecked")
+ RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[generators.length];
+ int index = 0;
+ for (Generator<T> generator : generators) {
+ result[index++] = generator.generate(random, size);
+ }
+ return RoseTree.zip(Function.identity(), result);
+ };
+ }
+
+ default <R> Generator<R> fmap(Function<T, R> f) {
+ return (random, size) -> this.generate(random, size).fmap(f);
+ }
+
+ default <R> Generator<R> flatMap(Function<T, Generator<R>> action) {
+ return (random, size) -> {
+ return RoseTree.join(this.generate(random, size).fmap(action).fmap(g -> g.generate(random, size)));
+ };
+ }
+}