
com.sun.tools.xjc.addon.krasa.validations.NumericRange Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of krasa-jaxb-tools Show documentation
Show all versions of krasa-jaxb-tools Show documentation
JAXB plugin to generate Bean Validation Annotations 2.0 (JSR-380) from XSDs
The newest version!
package com.sun.tools.xjc.addon.krasa.validations;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
*
* @author Francesco Illuminati
*/
class NumericRange {
private static final Map MAP = new HashMap<>();
static {
MAP.put(Byte.class.getCanonicalName(), new NumericRange(Byte.MIN_VALUE, Byte.MAX_VALUE));
MAP.put(Short.class.getCanonicalName(), new NumericRange(Short.MIN_VALUE, Short.MAX_VALUE));
MAP.put(Integer.class.getCanonicalName(), new NumericRange(Integer.MIN_VALUE, Integer.MAX_VALUE));
MAP.put(Long.class.getCanonicalName(), new NumericRange(Long.MIN_VALUE, Long.MAX_VALUE));
}
final BigDecimal min;
final BigDecimal max;
public NumericRange(Number min, Number max) {
this.min = parse(min);
this.max = parse(max);
}
private static BigDecimal parse(Number num) {
if (num != null) {
return new BigDecimal(Objects.toString(num));
}
return null;
}
public static BigDecimal valid(String typeName, BigDecimal value) {
NumericRange range = MAP.get(typeName);
if (range != null && (value.equals(range.min) || value.equals(range.max)) ) {
return null;
}
return value;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy