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

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

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

import java.lang.reflect.*;
import java.util.*;

import net.jqwik.api.*;
import net.jqwik.api.arbitraries.*;
import net.jqwik.api.configurators.*;
import net.jqwik.api.providers.*;
import net.jqwik.engine.properties.arbitraries.exhaustive.*;

public class ArrayArbitrary extends MultivalueArbitraryBase implements SizableArbitrary, SelfConfiguringArbitrary {

	private final Class arrayClass;

	public ArrayArbitrary(Arbitrary elementArbitrary, Class arrayClass) {
		super(elementArbitrary);
		this.arrayClass = arrayClass;
	}

	@Override
	public RandomGenerator generator(int genSize) {
		return createListGenerator(genSize).map(this::toArray);
	}

	@Override
	public Optional> exhaustive() {
		return ExhaustiveGenerators.list(elementArbitrary, minSize, maxSize).map(generator -> generator.map(this::toArray));
	}

	@SuppressWarnings("unchecked")
	private A toArray(List from) {
		A array = (A) Array.newInstance(arrayClass.getComponentType(), from.size());
		for (int i = 0; i < from.size(); i++) {
			Array.set(array, i, from.get(i));
		}
		return array;
	}

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

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

	@Override
	public Arbitrary configure(ArbitraryConfigurator configurator, TypeUsage targetType) {
		elementArbitrary = configurator.configure(elementArbitrary, targetType);
		return configurator.configure(this, targetType);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy