Skip to content

Commit

Permalink
Remove implementation dependency for MapBasedArbitrary subtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Mar 11, 2023
1 parent bbf73b9 commit c2a8375
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -26,72 +22,58 @@ public abstract class MapBasedArbitrary<K, V, U extends Traversable<Tuple2<K, V>
extends ArbitraryDecorator<U>
implements MapArbitrary<K, V, U> {

private final Arbitrary<K> keysArbitrary;
private final Arbitrary<V> valuesArbitrary;

private int minSize = 0;
private int maxSize = RandomGenerators.DEFAULT_COLLECTION_SIZE;
// private RandomDistribution sizeDistribution = null;

private Set<FeatureExtractor<K>> keyUniquenessExtractors = new HashSet<>();
private Set<FeatureExtractor<V>> valueUniquenessExtractors = new HashSet<>();
private net.jqwik.api.arbitraries.MapArbitrary<K, V> javaMapArbitrary;

public MapBasedArbitrary(final Arbitrary<K> keysArbitrary, final Arbitrary<V> valuesArbitrary) {
this.keysArbitrary = keysArbitrary;
this.valuesArbitrary = valuesArbitrary;
this.javaMapArbitrary = Arbitraries.maps(keysArbitrary, valuesArbitrary);
}

@Override
public MapArbitrary<K, V, U> ofMinSize(final int minSize) {
final MapBasedArbitrary<K, V, U> clone = typedClone();
clone.minSize = minSize;
clone.javaMapArbitrary = javaMapArbitrary.ofMinSize(minSize);
return clone;
}

@Override
public MapArbitrary<K, V, U> ofMaxSize(final int maxSize) {
final MapBasedArbitrary<K, V, U> clone = typedClone();
clone.maxSize = maxSize;
clone.javaMapArbitrary = javaMapArbitrary.ofMaxSize(maxSize);
return clone;
}

@Override
public MapArbitrary<K, V, U> withSizeDistribution(final RandomDistribution distribution) {
final MapBasedArbitrary<K, V, U> clone = typedClone();
// clone.sizeDistribution = distribution; // TODO
clone.javaMapArbitrary = javaMapArbitrary.withSizeDistribution(distribution);
return clone;
}

@Override
protected Arbitrary<U> arbitrary() {
// Using list of generated Map.Entry does not work because of potential duplicate keys
final Arbitrary<List<K>> 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<Tuple2<K, V>> tuple2List);
protected abstract U convertJavaMapToVavrMap(java.util.Map<K, V> javaMap);

@Override
public MapArbitrary<K, V, U> uniqueKeys(final Function<K, Object> by) {
final MapBasedArbitrary<K, V, U> clone = typedClone();
clone.keyUniquenessExtractors = new HashSet<>(this.keyUniquenessExtractors);
clone.keyUniquenessExtractors.add(by::apply);
clone.javaMapArbitrary = javaMapArbitrary.uniqueKeys(by);
return clone;
}

@Override
public MapArbitrary<K, V, U> uniqueValues(final Function<V, Object> by) {
final MapBasedArbitrary<K, V, U> 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<K, V, U> uniqueValues() {
return uniqueValues(FeatureExtractor.identity());
return uniqueValues((Function<V, Object>) Function.identity());
}

}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -16,8 +16,7 @@ public VavrHashMapArbitrary(final Arbitrary<K> keysArbitrary, final Arbitrary<V>
}

@Override
protected HashMap<K, V> convertTupleListToVavrMap(final List<Tuple2<K, V>> tuple2List) {
return HashMap.ofEntries(tuple2List);
protected HashMap<K, V> convertJavaMapToVavrMap(Map<K, V> javaMap) {
return HashMap.ofAll(javaMap);
}

}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -16,8 +16,7 @@ public VavrHashMultimapArbitrary(final Arbitrary<K> keysArbitrary, final Arbitra
}

@Override
protected HashMultimap<K, V> convertTupleListToVavrMap(final List<Tuple2<K, V>> tuple2List) {
return HashMultimap.withSeq().ofEntries(tuple2List);
protected HashMultimap<K, V> convertJavaMapToVavrMap(Map<K, V> javaMap) {
return HashMultimap.withSeq().ofAll(javaMap);
}

}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -16,8 +16,7 @@ public VavrLinkedHashMapArbitrary(final Arbitrary<K> keysArbitrary, final Arbitr
}

@Override
protected LinkedHashMap<K, V> convertTupleListToVavrMap(final List<Tuple2<K, V>> tuple2List) {
return LinkedHashMap.ofEntries(tuple2List);
protected LinkedHashMap<K, V> convertJavaMapToVavrMap(Map<K, V> javaMap) {
return LinkedHashMap.ofAll(javaMap);
}

}
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -16,8 +16,7 @@ public VavrLinkedHashMultimapArbitrary(final Arbitrary<K> keysArbitrary, final A
}

@Override
protected LinkedHashMultimap<K, V> convertTupleListToVavrMap(final List<Tuple2<K, V>> tuple2List) {
return LinkedHashMultimap.withSeq().ofEntries(tuple2List);
protected LinkedHashMultimap<K, V> convertJavaMapToVavrMap(Map<K, V> javaMap) {
return LinkedHashMultimap.withSeq().ofAll(javaMap);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,8 +30,7 @@ public VavrTreeMapArbitrary(
}

@Override
protected TreeMap<K, V> convertTupleListToVavrMap(final List<Tuple2<K, V>> tuple2List) {
return TreeMap.ofEntries(this.keyComparator, tuple2List);
protected TreeMap<K, V> convertJavaMapToVavrMap(Map<K, V> javaMap) {
return TreeMap.ofAll(this.keyComparator, javaMap);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V> extends MapBasedArbitrary<K, V, TreeMultimap<K, V>> {

private Comparator<K> keyComparator;
private final Comparator<K> keyComparator;

public VavrTreeMultimapArbitrary(
final Arbitrary<K> keysArbitrary,
Expand All @@ -29,8 +28,7 @@ public VavrTreeMultimapArbitrary(final Arbitrary<K> keysArbitrary, final Arbitra
}

@Override
protected TreeMultimap<K, V> convertTupleListToVavrMap(final List<Tuple2<K, V>> tuple2List) {
return TreeMultimap.withSeq().ofEntries(this.keyComparator, tuple2List);
protected TreeMultimap<K, V> convertJavaMapToVavrMap(Map<K, V> javaMap) {
return TreeMultimap.withSeq().ofAll(this.keyComparator, javaMap);
}

}

0 comments on commit c2a8375

Please sign in to comment.