From c2a8375ae8bf960a9c1a5c2c73cc4bc5ad98b5db Mon Sep 17 00:00:00 2001 From: Johannes Link Date: Sat, 11 Mar 2023 11:12:44 +0100 Subject: [PATCH] Remove implementation dependency for MapBasedArbitrary subtypes --- .../arbitraries/base/MapBasedArbitrary.java | 44 ++++++------------- .../collection/map/VavrHashMapArbitrary.java | 9 ++-- .../map/VavrHashMultimapArbitrary.java | 9 ++-- .../map/VavrLinkedHashMapArbitrary.java | 9 ++-- .../map/VavrLinkedHashMultimapArbitrary.java | 9 ++-- .../collection/map/VavrTreeMapArbitrary.java | 8 ++-- .../map/VavrTreeMultimapArbitrary.java | 10 ++--- 7 files changed, 36 insertions(+), 62 deletions(-) diff --git a/src/main/java/net/jqwik/vavr/arbitraries/base/MapBasedArbitrary.java b/src/main/java/net/jqwik/vavr/arbitraries/base/MapBasedArbitrary.java index 992b772..ccb592b 100644 --- a/src/main/java/net/jqwik/vavr/arbitraries/base/MapBasedArbitrary.java +++ b/src/main/java/net/jqwik/vavr/arbitraries/base/MapBasedArbitrary.java @@ -1,18 +1,14 @@ package net.jqwik.vavr.arbitraries.base; import io.vavr.Tuple2; -import io.vavr.collection.List; import io.vavr.collection.Traversable; + import net.jqwik.api.Arbitrary; +import net.jqwik.api.Arbitraries; import net.jqwik.api.RandomDistribution; import net.jqwik.api.arbitraries.ArbitraryDecorator; -import net.jqwik.engine.properties.arbitraries.DefaultMapArbitrary; import net.jqwik.vavr.api.arbitraries.MapArbitrary; -import net.jqwik.engine.properties.FeatureExtractor; -import net.jqwik.engine.properties.arbitraries.randomized.RandomGenerators; -import java.util.HashSet; -import java.util.Set; import java.util.function.Function; /** @@ -26,72 +22,58 @@ public abstract class MapBasedArbitrary extends ArbitraryDecorator implements MapArbitrary { - private final Arbitrary keysArbitrary; - private final Arbitrary valuesArbitrary; - - private int minSize = 0; - private int maxSize = RandomGenerators.DEFAULT_COLLECTION_SIZE; -// private RandomDistribution sizeDistribution = null; - - private Set> keyUniquenessExtractors = new HashSet<>(); - private Set> valueUniquenessExtractors = new HashSet<>(); + private net.jqwik.api.arbitraries.MapArbitrary javaMapArbitrary; public MapBasedArbitrary(final Arbitrary keysArbitrary, final Arbitrary valuesArbitrary) { - this.keysArbitrary = keysArbitrary; - this.valuesArbitrary = valuesArbitrary; + this.javaMapArbitrary = Arbitraries.maps(keysArbitrary, valuesArbitrary); } @Override public MapArbitrary ofMinSize(final int minSize) { final MapBasedArbitrary clone = typedClone(); - clone.minSize = minSize; + clone.javaMapArbitrary = javaMapArbitrary.ofMinSize(minSize); return clone; } @Override public MapArbitrary ofMaxSize(final int maxSize) { final MapBasedArbitrary clone = typedClone(); - clone.maxSize = maxSize; + clone.javaMapArbitrary = javaMapArbitrary.ofMaxSize(maxSize); return clone; } @Override public MapArbitrary withSizeDistribution(final RandomDistribution distribution) { final MapBasedArbitrary clone = typedClone(); -// clone.sizeDistribution = distribution; // TODO + clone.javaMapArbitrary = javaMapArbitrary.withSizeDistribution(distribution); return clone; } @Override protected Arbitrary arbitrary() { - // Using list of generated Map.Entry does not work because of potential duplicate keys - final Arbitrary> keySets = this.keysArbitrary.set().ofMinSize(this.minSize).ofMaxSize(this.maxSize) - .map(List::ofAll); - return keySets.flatMap(keys -> this.valuesArbitrary.list().ofSize(keys.size()) - .map(values -> convertTupleListToVavrMap(keys.zip(values)))); + return javaMapArbitrary.map(this::convertJavaMapToVavrMap); } - protected abstract U convertTupleListToVavrMap(List> tuple2List); + protected abstract U convertJavaMapToVavrMap(java.util.Map javaMap); @Override public MapArbitrary uniqueKeys(final Function by) { final MapBasedArbitrary clone = typedClone(); - clone.keyUniquenessExtractors = new HashSet<>(this.keyUniquenessExtractors); - clone.keyUniquenessExtractors.add(by::apply); + clone.javaMapArbitrary = javaMapArbitrary.uniqueKeys(by); return clone; } @Override public MapArbitrary uniqueValues(final Function by) { final MapBasedArbitrary clone = typedClone(); - clone.valueUniquenessExtractors = new HashSet<>(this.valueUniquenessExtractors); - clone.valueUniquenessExtractors.add(by::apply); + clone.javaMapArbitrary = javaMapArbitrary.uniqueValues(by); return clone; } + @SuppressWarnings("unchecked") @Override public MapArbitrary uniqueValues() { - return uniqueValues(FeatureExtractor.identity()); + return uniqueValues((Function) Function.identity()); } } diff --git a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrHashMapArbitrary.java b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrHashMapArbitrary.java index ba18d34..33170b3 100644 --- a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrHashMapArbitrary.java +++ b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrHashMapArbitrary.java @@ -1,9 +1,9 @@ package net.jqwik.vavr.arbitraries.collection.map; +import java.util.Map; + import net.jqwik.vavr.arbitraries.base.MapBasedArbitrary; -import io.vavr.Tuple2; import io.vavr.collection.HashMap; -import io.vavr.collection.List; import net.jqwik.api.Arbitrary; /** @@ -16,8 +16,7 @@ public VavrHashMapArbitrary(final Arbitrary keysArbitrary, final Arbitrary } @Override - protected HashMap convertTupleListToVavrMap(final List> tuple2List) { - return HashMap.ofEntries(tuple2List); + protected HashMap convertJavaMapToVavrMap(Map javaMap) { + return HashMap.ofAll(javaMap); } - } diff --git a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrHashMultimapArbitrary.java b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrHashMultimapArbitrary.java index c865b70..708393a 100644 --- a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrHashMultimapArbitrary.java +++ b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrHashMultimapArbitrary.java @@ -1,9 +1,9 @@ package net.jqwik.vavr.arbitraries.collection.map; +import java.util.Map; + import net.jqwik.vavr.arbitraries.base.MapBasedArbitrary; -import io.vavr.Tuple2; import io.vavr.collection.HashMultimap; -import io.vavr.collection.List; import net.jqwik.api.Arbitrary; /** @@ -16,8 +16,7 @@ public VavrHashMultimapArbitrary(final Arbitrary keysArbitrary, final Arbitra } @Override - protected HashMultimap convertTupleListToVavrMap(final List> tuple2List) { - return HashMultimap.withSeq().ofEntries(tuple2List); + protected HashMultimap convertJavaMapToVavrMap(Map javaMap) { + return HashMultimap.withSeq().ofAll(javaMap); } - } diff --git a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrLinkedHashMapArbitrary.java b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrLinkedHashMapArbitrary.java index 66d5d9c..4aad075 100644 --- a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrLinkedHashMapArbitrary.java +++ b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrLinkedHashMapArbitrary.java @@ -1,9 +1,9 @@ package net.jqwik.vavr.arbitraries.collection.map; +import java.util.Map; + import net.jqwik.vavr.arbitraries.base.MapBasedArbitrary; -import io.vavr.Tuple2; import io.vavr.collection.LinkedHashMap; -import io.vavr.collection.List; import net.jqwik.api.Arbitrary; /** @@ -16,8 +16,7 @@ public VavrLinkedHashMapArbitrary(final Arbitrary keysArbitrary, final Arbitr } @Override - protected LinkedHashMap convertTupleListToVavrMap(final List> tuple2List) { - return LinkedHashMap.ofEntries(tuple2List); + protected LinkedHashMap convertJavaMapToVavrMap(Map javaMap) { + return LinkedHashMap.ofAll(javaMap); } - } diff --git a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrLinkedHashMultimapArbitrary.java b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrLinkedHashMultimapArbitrary.java index 3d931e2..2cd2f76 100644 --- a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrLinkedHashMultimapArbitrary.java +++ b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrLinkedHashMultimapArbitrary.java @@ -1,9 +1,9 @@ package net.jqwik.vavr.arbitraries.collection.map; +import java.util.Map; + import net.jqwik.vavr.arbitraries.base.MapBasedArbitrary; -import io.vavr.Tuple2; import io.vavr.collection.LinkedHashMultimap; -import io.vavr.collection.List; import net.jqwik.api.Arbitrary; /** @@ -16,8 +16,7 @@ public VavrLinkedHashMultimapArbitrary(final Arbitrary keysArbitrary, final A } @Override - protected LinkedHashMultimap convertTupleListToVavrMap(final List> tuple2List) { - return LinkedHashMultimap.withSeq().ofEntries(tuple2List); + protected LinkedHashMultimap convertJavaMapToVavrMap(Map javaMap) { + return LinkedHashMultimap.withSeq().ofAll(javaMap); } - } diff --git a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrTreeMapArbitrary.java b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrTreeMapArbitrary.java index 98312da..18e1156 100644 --- a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrTreeMapArbitrary.java +++ b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrTreeMapArbitrary.java @@ -2,12 +2,11 @@ import net.jqwik.vavr.NaturalComparator; import net.jqwik.vavr.arbitraries.base.MapBasedArbitrary; -import io.vavr.Tuple2; import io.vavr.collection.TreeMap; -import io.vavr.collection.List; import net.jqwik.api.Arbitrary; import java.util.Comparator; +import java.util.Map; /** * @author Benno Müller @@ -31,8 +30,7 @@ public VavrTreeMapArbitrary( } @Override - protected TreeMap convertTupleListToVavrMap(final List> tuple2List) { - return TreeMap.ofEntries(this.keyComparator, tuple2List); + protected TreeMap convertJavaMapToVavrMap(Map javaMap) { + return TreeMap.ofAll(this.keyComparator, javaMap); } - } diff --git a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrTreeMultimapArbitrary.java b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrTreeMultimapArbitrary.java index f585a61..0dba768 100644 --- a/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrTreeMultimapArbitrary.java +++ b/src/main/java/net/jqwik/vavr/arbitraries/collection/map/VavrTreeMultimapArbitrary.java @@ -2,19 +2,18 @@ import net.jqwik.vavr.NaturalComparator; import net.jqwik.vavr.arbitraries.base.MapBasedArbitrary; -import io.vavr.Tuple2; -import io.vavr.collection.List; import io.vavr.collection.TreeMultimap; import net.jqwik.api.Arbitrary; import java.util.Comparator; +import java.util.Map; /** * @author Benno Müller */ public class VavrTreeMultimapArbitrary extends MapBasedArbitrary> { - private Comparator keyComparator; + private final Comparator keyComparator; public VavrTreeMultimapArbitrary( final Arbitrary keysArbitrary, @@ -29,8 +28,7 @@ public VavrTreeMultimapArbitrary(final Arbitrary keysArbitrary, final Arbitra } @Override - protected TreeMultimap convertTupleListToVavrMap(final List> tuple2List) { - return TreeMultimap.withSeq().ofEntries(this.keyComparator, tuple2List); + protected TreeMultimap convertJavaMapToVavrMap(Map javaMap) { + return TreeMultimap.withSeq().ofAll(this.keyComparator, javaMap); } - }