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

net.jqwik.engine.properties.arbitraries.exhaustive.ExhaustiveGenerators Maven / Gradle / Ivy

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

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

import net.jqwik.api.*;

public class ExhaustiveGenerators {

	public static long MAXIMUM_ACCEPTED_MAX_COUNT = Integer.MAX_VALUE;

	public static  Optional> create(Supplier supplier) {
		return fromIterable(() -> new SupplierIterator<>(supplier), 1);
	}

	public static  Optional> choose(List values) {
		return fromIterable(values, values.size());
	}

	public static Optional> choose(char[] characters) {
		List validCharacters = new ArrayList<>(characters.length);
		for (char character : characters) {
			validCharacters.add(character);
		}
		return choose(validCharacters);
	}

	public static > Optional> choose(Class enumClass) {
		return choose(Arrays.asList(enumClass.getEnumConstants()));
	}

	public static  Optional> fromIterable(Iterable iterator, long maxCount) {
		return Optional.of(new IterableBasedExhaustiveGenerator<>(iterator, maxCount));
	}

	public static  Optional>> list(Arbitrary elementArbitrary, int minSize, int maxSize) {
		Optional optionalMaxCount = ListExhaustiveGenerator.calculateMaxCount(elementArbitrary, minSize, maxSize);
		return optionalMaxCount.map(
			maxCount ->
			{
				ListExhaustiveGenerator exhaustiveGenerator = new ListExhaustiveGenerator<>(elementArbitrary, maxCount, minSize, maxSize);

				// A hack to accommodate missing design idea for handling unique exhaustive generation:
				Optional> exhaustive = elementArbitrary.exhaustive();
				if (exhaustive.isPresent() && exhaustive.get().isUnique()) {
					Predicate> allElementsUnique = list -> list.size() == new HashSet<>(list).size();
					return exhaustiveGenerator.filter(allElementsUnique);
				} else {
					return exhaustiveGenerator;
				}
			}
		);
	}

	public static Optional> strings(
		Arbitrary characterArbitrary,
		int minLength,
		int maxLength
	) {
		return list(characterArbitrary, minLength, maxLength).map(
			listGenerator -> listGenerator.map(
				listOfChars -> listOfChars.stream()
										  .map(String::valueOf)
										  .collect(Collectors.joining())
			));
	}

	public static  Optional>> set(Arbitrary elementArbitrary, int minSize, int maxSize) {
		Optional optionalMaxCount = SetExhaustiveGenerator.calculateMaxCount(elementArbitrary, minSize, maxSize);
		return optionalMaxCount.map(
			maxCount -> new SetExhaustiveGenerator<>(elementArbitrary, maxCount, minSize, maxSize)
		);
	}

	public static  Optional> combine(List> arbitraries, Function, R> combinator) {
		Optional optionalMaxCount = CombinedExhaustiveGenerator.calculateMaxCount(arbitraries);
		return optionalMaxCount.map(maxCount -> new CombinedExhaustiveGenerator<>(maxCount, arbitraries, combinator));
	}

	public static  Optional>> shuffle(List values) {
		Optional optionalMaxCount = PermutationExhaustiveGenerator.calculateMaxCount(values);
		return optionalMaxCount.map(
			maxCount -> new PermutationExhaustiveGenerator<>(values, maxCount)
		);
	}

	public static  Optional> flatMap(ExhaustiveGenerator base, Function> mapper) {
		Optional optionalMaxCount = FlatMappedExhaustiveGenerator.calculateMaxCounts(base, mapper);
		return optionalMaxCount.map(
			maxCount -> new FlatMappedExhaustiveGenerator<>(base, maxCount, mapper)
		);
	}

	private static class SupplierIterator implements Iterator {

		private final Supplier supplier;
		private volatile boolean generated = false;

		private SupplierIterator(Supplier supplier) {
			this.supplier = supplier;
		}

		@Override
		public boolean hasNext() {
			return !generated;
		}

		@Override
		public T next() {
			if (generated) {
				throw new NoSuchElementException();
			}
			generated = true;
			return supplier.get();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy