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.api.support.*;
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 Integer maxSize = null;
	private RandomDistribution sizeDistribution = null;

	private Set> keyUniquenessExtractors = new LinkedHashSet<>();
	private Set> valueUniquenessExtractors = new LinkedHashSet<>();

	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 -> {
						Map map = new LinkedHashMap<>();
						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;
	}

	private int maxSize() {
		return RandomGenerators.collectionMaxSize(minSize, maxSize);
	}

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

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

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

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		DefaultMapArbitrary that = (DefaultMapArbitrary) o;
		if (minSize != that.minSize) return false;
		if (!Objects.equals(maxSize, that.maxSize)) return false;
		if (!keysArbitrary.equals(that.keysArbitrary)) return false;
		if (!valuesArbitrary.equals(that.valuesArbitrary)) return false;
		if (!Objects.equals(sizeDistribution, that.sizeDistribution)) return false;
		if (!keyUniquenessExtractors.equals(that.keyUniquenessExtractors)) return false;
		return valueUniquenessExtractors.equals(that.valueUniquenessExtractors);
	}

	@Override
	public int hashCode() {
		return HashCodeSupport.hash(minSize, maxSize, keysArbitrary, valuesArbitrary);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy