io.envoyproxy.pgv.RepeatedValidation Maven / Gradle / Ivy
The newest version!
package io.envoyproxy.pgv;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* {@code RepeatedValidation} implements PGV validators for collection-type validators.
*/
public final class RepeatedValidation {
private RepeatedValidation() {
}
public static void minItems(String field, List values, int expected) throws ValidationException {
if (values.size() < expected) {
throw new ValidationException(field, values, "must have at least " + expected + " items");
}
}
public static void maxItems(String field, List values, int expected) throws ValidationException {
if (values.size() > expected) {
throw new ValidationException(field, values, "must have at most " + expected + " items");
}
}
public static void unique(String field, List values) throws ValidationException {
Set seen = new HashSet<>();
for (T value : values) {
// Abort at the first sign of a duplicate
if (!seen.add(value)) {
throw new ValidationException(field, values, "must have all unique values");
}
}
}
@FunctionalInterface
public interface ValidationConsumer {
void accept(T value) throws ValidationException;
}
public static void forEach(List values, ValidationConsumer consumer) throws ValidationException {
for (T value : values) {
consumer.accept(value);
}
}
}