All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.engine.properties.arbitraries.DefaultMapArbitrary Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.jqwik.engine.properties.arbitraries;

import java.util.*;
import java.util.function.*;

import net.jqwik.api.*;
import net.jqwik.api.arbitraries.*;
import net.jqwik.engine.properties.*;
import net.jqwik.engine.properties.arbitraries.randomized.*;

public class DefaultMapArbitrary 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<>();

	public DefaultMapArbitrary(Arbitrary keysArbitrary, Arbitrary valuesArbitrary) {
		this.keysArbitrary = keysArbitrary;
		this.valuesArbitrary = valuesArbitrary;
	}

	@Override
	public MapArbitrary ofMinSize(int minSize) {
		DefaultMapArbitrary clone = typedClone();
		clone.minSize = minSize;
		return clone;
	}

	@Override
	public MapArbitrary ofMaxSize(int maxSize) {
		DefaultMapArbitrary clone = typedClone();
		clone.maxSize = maxSize;
		return clone;
	}

	@Override
	public MapArbitrary withSizeDistribution(RandomDistribution distribution) {
		DefaultMapArbitrary clone = typedClone();
		clone.sizeDistribution = distribution;
		return clone;
	}

	@Override
	protected Arbitrary> arbitrary() {
		// Using list of generated Map.Entry does not work because of potential duplicate keys
		SetArbitrary keySetArbitrary = createKeySetArbitrary();
		Arbitrary> keySets = keySetArbitrary.map(ArrayList::new);
		return keySets.flatMap(keys -> {
			int mapSize = keys.size();
			ListArbitrary valueListArbitrary = createValueListArbitrary(mapSize);
			return valueListArbitrary.map(
					values -> {
						HashMap map = new HashMap<>();
						for (int i = 0; i < mapSize; i++) {
							K key = keys.get(i);
							V value = values.get(i);
							map.put(key, value);
						}
						return map;
					});
		});
	}

	private ListArbitrary createValueListArbitrary(int size) {
		ListArbitrary valueListArbitrary = valuesArbitrary.list().ofSize(size);
		for (FeatureExtractor extractor : valueUniquenessExtractors) {
			valueListArbitrary = valueListArbitrary.uniqueElements(extractor);
		}
		return valueListArbitrary;
	}

	private SetArbitrary createKeySetArbitrary() {
		SetArbitrary keySetArbitrary = keysArbitrary.set().ofMinSize(minSize).ofMaxSize(maxSize).withSizeDistribution(sizeDistribution);
		for (FeatureExtractor extractor : keyUniquenessExtractors) {
			keySetArbitrary = keySetArbitrary.uniqueElements(extractor);
		}
		return keySetArbitrary;
	}

	@Override
	public MapArbitrary uniqueKeys(Function by) {
		DefaultMapArbitrary clone = typedClone();
		clone.keyUniquenessExtractors = new HashSet<>(keyUniquenessExtractors);
		clone.keyUniquenessExtractors.add(by::apply);
		return clone;
	}

	@Override
	public MapArbitrary uniqueValues(Function by) {
		DefaultMapArbitrary clone = typedClone();
		clone.valueUniquenessExtractors = new HashSet<>(valueUniquenessExtractors);
		clone.valueUniquenessExtractors.add(by::apply);
		return clone;
	}

	@Override
	public MapArbitrary uniqueValues() {
		return uniqueValues(FeatureExtractor.identity());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy