net.jqwik.engine.properties.Range Maven / Gradle / Ivy
package net.jqwik.engine.properties;
import java.util.function.*;
public class Range> {
public static > Range of(T min, T max) {
return Range.of(min, true, max, true);
}
public static > Range of(T min, boolean minIncluded, T max, boolean maxIncluded) {
if (min.compareTo(max) > 0)
throw new IllegalArgumentException(String.format("Min value [%s] must not be greater than max value [%s].", min, max));
if (min.compareTo(max) == 0 && (!minIncluded || !maxIncluded))
throw new IllegalArgumentException(
String.format("If min value [%s] is equal to max value [%s] borders must be included.", min, max)
);
return new Range<>(min, minIncluded, max, maxIncluded);
}
public final T min;
public final boolean minIncluded;
public final T max;
public final boolean maxIncluded;
private Range(T min, boolean minIncluded, T max, boolean maxIncluded) {
this.min = min;
this.minIncluded = minIncluded;
this.max = max;
this.maxIncluded = maxIncluded;
}
public boolean isSingular() {
return min.compareTo(max) == 0 && minIncluded && maxIncluded;
}
public boolean includes(T value) {
int minCompare = value.compareTo(min);
int maxCompare = value.compareTo(max);
if (minCompare < 0 || maxCompare > 0) {
return false;
}
if (!minIncluded && minCompare == 0) {
return false;
}
return maxIncluded || maxCompare != 0;
}
public void ifIncluded(T value, Consumer consumer) {
if (includes(value)) {
consumer.accept(value);
}
}
public > Range map(Function mapper) {
return Range.of(mapper.apply(min), minIncluded, mapper.apply(max), maxIncluded);
}
public Range withMin(T newMin, boolean newMinIncluded) {
return Range.of(newMin, newMinIncluded, max, maxIncluded);
}
public Range withMax(T newMax, boolean newMaxIncluded) {
return Range.of(min, minIncluded, newMax, newMaxIncluded);
}
@Override
public String toString() {
char leftBracket = minIncluded ? '[' : ']';
char rightBracket = maxIncluded ? ']' : '[';
return String.format("%s%s..%s%s", leftBracket, min, max, rightBracket);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy