com.github.fashionbrot.internal.SizeConstraint Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of validation Show documentation
Show all versions of validation Show documentation
validation 参数验证 https://github.com/fashionbrot/validation
The newest version!
package com.github.fashionbrot.internal;
import com.github.fashionbrot.annotation.Size;
import com.github.fashionbrot.common.util.JavaUtil;
import com.github.fashionbrot.constraint.ConstraintValidator;
import com.github.fashionbrot.util.ClassUtil;
import java.util.Collection;
import java.util.Map;
public class SizeConstraint implements ConstraintValidator {
@Override
public boolean isValid(Size annotation, Object value, Class> valueType) {
if (value == null) {
return annotation.skipEmpty();
}
long min = annotation.min();
long max = annotation.max();
if (ClassUtil.isMap(valueType)) {
Map map = (Map, ?>) value;
int size = map.size();
return checkSize(size, annotation, min, max);
} else if (ClassUtil.isCollection(valueType)) {
Collection collection = (Collection) value;
int size = collection.size();
return checkSize(size, annotation, min, max);
} else if (ClassUtil.isArray(valueType)) {
Object[] array = (Object[]) value;
int size = array.length;
return checkSize(size, annotation, min, max);
}
return true;
}
private boolean checkSize(int size, Size annotation, long min, long max) {
if (size == 0) {//#issues4
return annotation.skipEmpty();
}
return !(min > size || size > max);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy