net.jqwik.engine.properties.shrinking.UniqueShrinkable Maven / Gradle / Ivy
package net.jqwik.engine.properties.shrinking;
import java.util.*;
import java.util.function.*;
import net.jqwik.api.*;
public class UniqueShrinkable implements Shrinkable {
private final Shrinkable toFilter;
private final Set usedValues;
private final T value;
public UniqueShrinkable(Shrinkable toFilter, Set usedValues) {
this.toFilter = toFilter;
this.usedValues = usedValues;
this.value = toFilter.value();
}
@Override
public T value() {
return value;
}
@Override
public ShrinkingSequence shrink(Falsifier falsifier) {
return new UniqueShrinkingSequence(falsifier);
}
@Override
public ShrinkingDistance distance() {
return toFilter.distance();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UniqueShrinkable> that = (UniqueShrinkable>) o;
return toFilter.equals(that.toFilter);
}
@Override
public int hashCode() {
return toFilter.hashCode();
}
@Override
public String toString() {
return String.format("Unique|%s", toFilter);
}
//TODO: Extract to ShrinkingSequence.unique() method
private class UniqueShrinkingSequence implements ShrinkingSequence {
private final ShrinkingSequence uniqueSequence;
private UniqueShrinkingSequence(Falsifier falsifier) {
Falsifier uniqueFalsifier = falsifier.withFilter(value -> !usedValues.contains(value));
uniqueSequence = toFilter.shrink(uniqueFalsifier);
}
@Override
public boolean next(Runnable count, Consumer> falsifiedReporter) {
T valueToShrink = uniqueSequence.current().value();
boolean hasNext = uniqueSequence.next(count, falsifiedReporter);
if (hasNext) {
usedValues.remove(valueToShrink);
usedValues.add(uniqueSequence.current().value());
}
return hasNext;
}
@Override
public void init(FalsificationResult initialCurrent) {
uniqueSequence.init(initialCurrent);
}
@Override
public FalsificationResult current() {
return uniqueSequence.current().map(shrinkable -> new UniqueShrinkable<>(shrinkable, usedValues));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy