DoubleRangeFieldValidator的实现和疑问

[size=medium]
我们使用IntRangeFieldValidator的比较多,今天看了一下 DoubleRangeFieldValidator这个注解,看到有四个参数 minInclusive,maxInclusive,minExclusive,maxExclusive
有点懵,再看一下它的实现:
[/size]
[color=red]
[size=medium]
现在的疑问是我们发现minInclusiveValue和minExclusiveValue是相关的,为什么我们不能把参数变成这样四个参数呢:
String min = null;
String max = null;
boolean includeMin = true;
boolean includeMax = true;
[/size]
[/color]

public class DoubleRangeFieldValidator extends FieldValidatorSupport {

String maxInclusive = null;
String minInclusive = null;
String minExclusive = null;
String maxExclusive = null;

Double maxInclusiveValue = null;
Double minInclusiveValue = null;
Double minExclusiveValue = null;
Double maxExclusiveValue = null;

// 这个方法没有什么,主要是把要验证的数转Double,然后在
// 设置那四个参数,最后进行比较。
public void validate(Object object) throws ValidationException {
String fieldName = getFieldName();
Double value;
try {
Object obj = this.getFieldValue(fieldName, object);
if (obj == null) {
return;
}
value = Double.valueOf(obj.toString());
} catch (NumberFormatException e) {
return;
}

parseParameterValues();

// 重点来看一下这个实现,如果设置了maxInclusiveValue,那么该值比它大的时候,
// 就直接报错,minInclusiveValue道理也是一样的。 maxExclusiveValue实际上是
// 判断一下包含。
if ((maxInclusiveValue != null && value.compareTo(maxInclusiveValue) > 0) ||
(minInclusiveValue != null && value.compareTo(minInclusiveValue) < 0) ||
(maxExclusiveValue != null && value.compareTo(maxExclusiveValue) >= 0) ||
(minExclusiveValue != null && value.compareTo(minExclusiveValue) <= 0)) {
addFieldError(fieldName, object);
}
}

private void parseParameterValues() {
this.minInclusiveValue = parseDouble(minInclusive);
this.maxInclusiveValue = parseDouble(maxInclusive);
this.minExclusiveValue = parseDouble(minExclusive);
this.maxExclusiveValue = parseDouble(maxExclusive);
}

private Double parseDouble (String value) {
if (value != null) {
try {
return Double.valueOf(value);
} catch (NumberFormatException e) {
if (log.isWarnEnabled()) {
log.warn("DoubleRangeFieldValidator - [parseDouble]: Unable to parse given double parameter " + value);
}
}
}
return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值