org.solidcoding.validation.predicates.StringLengthConstraintPredicateBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solidcoding-validation Show documentation
Show all versions of solidcoding-validation Show documentation
A small package that enables validation of (business) rules through a fluent API.
package org.solidcoding.validation.predicates;
import org.solidcoding.validation.api.ChainingPredicate;
import java.util.function.Predicate;
final class StringLengthConstraintPredicateBuilder implements ChainingPredicate> {
private final PredicateContainer originalPredicate;
private final int first;
StringLengthConstraintPredicateBuilder(Integer first, PredicateContainer originalPredicate) {
this.first = first;
this.originalPredicate = originalPredicate;
}
/**
* @param second second of the constraints. Can be either the high constraint or the low constraint.
* @return StringPredicate to continue adding rules.
*/
public Predicate and(Integer second) {
if (second > first) {
originalPredicate.addPredicate(x -> x.length() <= second && x.length() >= first);
} else {
originalPredicate.addPredicate(x -> x.length() <= first && x.length() >= second);
}
return originalPredicate;
}
}