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

net.jqwik.engine.properties.shrinking.ShrinkableContainer Maven / Gradle / Ivy

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

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

import net.jqwik.api.*;

abstract class ShrinkableContainer implements Shrinkable {
	private final List> elements;
	private final ListShrinkingCandidates> shrinkCandidates;
	private final C value;

	ShrinkableContainer(List> elements, int minSize) {
		this.elements = elements;
		this.shrinkCandidates = new ListShrinkingCandidates<>(minSize);
		this.value = createValue(elements);
	}

	private C createValue(List> shrinkables) {
		return shrinkables //
						   .stream() //
						   .map(Shrinkable::value) //
						   .collect(containerCollector());
	}

	@Override
	public C value() {
		return value;
	}

	@Override
	public ShrinkingSequence shrink(Falsifier falsifier) {
		return new DeepSearchShrinkingSequence<>(this, this::shrinkCandidatesFor, falsifier)
				   .andThen(shrinkableList -> {
					   List> elements = ((ShrinkableContainer) shrinkableList).elements;
					   Falsifier> listFalsifier = list -> falsifier.test(toContainer(list));
					   return new ContainerShrinkingSequence<>(elements, listFalsifier, ShrinkingDistance::forCollection, this::toContainerShrinkable);
				   }).andThen(shrinkableContainer ->
								  new DeepSearchShrinkingSequence<>(shrinkableContainer, this::shrinkCandidatesFor, falsifier)
			);
	}

	private C toContainer(List listOfE) {
		return listOfE.stream().collect(containerCollector());
	}

	private Shrinkable toContainerShrinkable(Shrinkable> listOfShrinkable) {
		List> shrinkableElements =
			listOfShrinkable.value().stream()
							.map(Shrinkable::unshrinkable)
							.collect(Collectors.toList());
		return createShrinkable(shrinkableElements);
	}

	private Set> shrinkCandidatesFor(Shrinkable shrinkable) {
		ShrinkableContainer listShrinkable = (ShrinkableContainer) shrinkable;
		return shrinkCandidates.candidatesFor(listShrinkable.elements) //
			.stream() //
			.map(this::createShrinkable) //
			.collect(Collectors.toSet());
	}

	@Override
	public ShrinkingDistance distance() {
		return ShrinkingDistance.forCollection(elements);
	}

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

	@Override
	public int hashCode() {
		return Objects.hash(elements);
	}

	@Override
	public String toString() {
		return String.format("%s<%s>(%s:%s)", //
			getClass().getSimpleName(), //
			value().getClass().getSimpleName(), //
			value(), distance());
	}

	abstract Shrinkable createShrinkable(List> shrunkElements);

	abstract Collector containerCollector();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy