diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-04 23:35:23 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-04 23:35:23 +1000 |
commit | 405af698aa80e22b7f1d1596dfbb796a3c882011 (patch) | |
tree | 2dad363b47cf9194d110e2f7bd460e3b074f06a4 | |
parent | 520f233695f5a9bd27d3d32b974e7d4cc7839ab3 (diff) |
Fix up generic types by adding bounds where possible instead of rigid constraints
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/Generator.java | 6 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java | 17 |
2 files changed, 12 insertions, 11 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/Generator.java b/src/main/java/au/id/zancanaro/javacheck/Generator.java index 5a8fec3..24c23ca 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generator.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generator.java @@ -111,7 +111,7 @@ public interface Generator<T> { * @param <R> The result of the transformation * @return A new generator resulting from mapping f over this */ - default <R> Generator<R> map(Function<T, R> f) { + default <R> Generator<R> map(Function<? super T, ? extends R> f) { return (random, size) -> this.generate(random, size).map(f); } @@ -127,7 +127,7 @@ public interface Generator<T> { * @return A new generator resulting from calling the provided action on the * result of this */ - default <R> Generator<R> flatMap(Function<T, Generator<R>> action) { + default <R> Generator<R> flatMap(Function<? super T, ? extends Generator<R>> action) { return (random, size) -> ShrinkTree.join( this.generate(random, size) .map(action @@ -148,7 +148,7 @@ public interface Generator<T> { * @return A new generator resulting from filtering this generator to only * terms which match the given predicate */ - default Generator<T> suchThat(Predicate<T> predicate) { + default Generator<T> suchThat(Predicate<? super T> predicate) { return (random, size) -> { ShrinkTree<T> result = this.generate(random, size); if (predicate.test(result.getValue())) { diff --git a/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java b/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java index 4337c14..4f4eb62 100644 --- a/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java +++ b/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java @@ -53,7 +53,7 @@ public class ShrinkTree<T> { } @SuppressWarnings("unchecked") - public static <T> Stream<ShrinkTree<T>[]> promoteChildren(ShrinkTree<T>[] trees) { + public static <T> Stream<ShrinkTree<T>[]> promoteChildren(ShrinkTree<? super T>[] trees) { return IntStream.range(0, trees.length) .mapToObj(index -> trees[index].getChildren().map(child -> IntStream.range(0, trees.length) @@ -63,7 +63,7 @@ public class ShrinkTree<T> { .map(x -> (ShrinkTree<T>[]) x); } - public static <T> Stream<ShrinkTree<T>[]> removeChildren(ShrinkTree<T>[] trees) { + public static <T> Stream<ShrinkTree<T>[]> removeChildren(ShrinkTree<? super T>[] trees) { return IntStream.range(0, trees.length) .mapToObj(index -> IntStream.range(0, trees.length) .filter(i -> i != index) @@ -71,7 +71,7 @@ public class ShrinkTree<T> { .toArray(ShrinkTree[]::new)); } - public static <T> Stream<ShrinkTree<T>[]> removeAndPromoteChildren(ShrinkTree<T>[] trees) { + public static <T> Stream<ShrinkTree<T>[]> removeAndPromoteChildren(ShrinkTree<? super T>[] trees) { return Stream.concat(removeChildren(trees), promoteChildren(trees)); } @@ -84,17 +84,17 @@ public class ShrinkTree<T> { .map(shrinks -> combine(shrinks, processChildren))); } - public <R> ShrinkTree<R> map(Function<T, R> f) { + public <R> ShrinkTree<R> map(Function<? super T, ? extends R> f) { return new ShrinkTree<>( f.apply(this.value), () -> this.getChildren().map(tree -> tree.map(f))); } - public <R> ShrinkTree<R> flatMap(Function<T, ShrinkTree<R>> f) { + public <R> ShrinkTree<R> flatMap(Function<?super T, ? extends ShrinkTree<R>> f) { return ShrinkTree.join(this.map(f)); } - public ShrinkTree<T> filter(Predicate<T> predicate) { + public ShrinkTree<T> filter(Predicate<? super T> predicate) { if (predicate.test(this.getValue())) { return new ShrinkTree<>( this.getValue(), @@ -111,14 +111,15 @@ public class ShrinkTree<T> { } private static <T> Stream<ShrinkTree<T>> strategyStream(final T value, final ShrinkStrategy<T> strategy) { - return strategy.shrink(value).map(v -> new ShrinkTree<>(v, () -> strategyStream(v, strategy))); + return strategy.shrink(value) + .map(v -> new ShrinkTree<T>(v, () -> strategyStream(v, strategy))); } public void print(Writer output) throws IOException { print(output, Object::toString); } - public void print(Writer output, Function<T, String> toString) throws IOException { + public void print(Writer output, Function<? super T, ? extends String> toString) throws IOException { output.write(toString.apply(this.getValue())); output.write('['); Iterator<ShrinkTree<T>> iterator = children.get().iterator(); |