Skip to content

Commit c92d43c

Browse files
committed
HV-1701 Add integer type specific DecimalMin/DecimalMax validators
- Make checks in Number validator for DecimalMin/DecimalMax to check for double/float to capture infinity situation correctly. Also direct a check of Big* types to corresponding methods - Add specific validators for DecimalMin/DecimalMax constraints and byte/short/integer types as the existing common number one makes additional actions that can be omitted in this case.
1 parent 8dc67c4 commit c92d43c

File tree

12 files changed

+222
-6
lines changed

12 files changed

+222
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal;
8+
9+
/**
10+
* Check that the number being validated is less than or equal to the maximum
11+
* value specified.
12+
*
13+
* @author Marko Bekhta
14+
*/
15+
public class DecimalMaxValidatorForByte extends AbstractDecimalMaxValidator<Byte> {
16+
17+
@Override
18+
protected int compare(Byte number) {
19+
return DecimalNumberComparatorHelper.compare( number.longValue(), maxValue );
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal;
8+
9+
/**
10+
* Check that the number being validated is less than or equal to the maximum
11+
* value specified.
12+
*
13+
* @author Marko Bekhta
14+
*/
15+
public class DecimalMaxValidatorForInteger extends AbstractDecimalMaxValidator<Integer> {
16+
17+
@Override
18+
protected int compare(Integer number) {
19+
return DecimalNumberComparatorHelper.compare( number.longValue(), maxValue );
20+
}
21+
}

engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/bv/number/bound/decimal/DecimalMaxValidatorForNumber.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal;
88

9+
import org.hibernate.validator.internal.constraintvalidators.bv.number.InfinityNumberComparatorHelper;
10+
911
/**
1012
* Check that the number being validated is less than or equal to the maximum
1113
* value specified.
@@ -14,7 +16,8 @@
1416
*/
1517
public class DecimalMaxValidatorForNumber extends AbstractDecimalMaxValidator<Number> {
1618

17-
@Override protected int compare(Number number) {
18-
return DecimalNumberComparatorHelper.compare( number, maxValue );
19+
@Override
20+
protected int compare(Number number) {
21+
return DecimalNumberComparatorHelper.compare( number, maxValue, InfinityNumberComparatorHelper.GREATER_THAN );
1922
}
2023
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal;
8+
9+
/**
10+
* Check that the number being validated is less than or equal to the maximum
11+
* value specified.
12+
*
13+
* @author Marko Bekhta
14+
*/
15+
public class DecimalMaxValidatorForShort extends AbstractDecimalMaxValidator<Short> {
16+
17+
@Override
18+
protected int compare(Short number) {
19+
return DecimalNumberComparatorHelper.compare( number.longValue(), maxValue );
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal;
8+
9+
/**
10+
* Check that the number being validated is greater than or equal to the minimum
11+
* value specified.
12+
*
13+
* @author Marko Bekhta
14+
*/
15+
public class DecimalMinValidatorForByte extends AbstractDecimalMinValidator<Byte> {
16+
17+
@Override
18+
protected int compare(Byte number) {
19+
return DecimalNumberComparatorHelper.compare( number.longValue(), minValue );
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal;
8+
9+
/**
10+
* Check that the number being validated is greater than or equal to the minimum
11+
* value specified.
12+
*
13+
* @author Marko Bekhta
14+
*/
15+
public class DecimalMinValidatorForInteger extends AbstractDecimalMinValidator<Integer> {
16+
17+
@Override
18+
protected int compare(Integer number) {
19+
return DecimalNumberComparatorHelper.compare( number.longValue(), minValue );
20+
}
21+
}

engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/bv/number/bound/decimal/DecimalMinValidatorForNumber.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal;
88

9+
import org.hibernate.validator.internal.constraintvalidators.bv.number.InfinityNumberComparatorHelper;
10+
911
/**
1012
* Check that the number being validated is greater than or equal to the minimum
1113
* value specified.
@@ -14,7 +16,8 @@
1416
*/
1517
public class DecimalMinValidatorForNumber extends AbstractDecimalMinValidator<Number> {
1618

17-
@Override protected int compare(Number number) {
18-
return DecimalNumberComparatorHelper.compare( number, minValue );
19+
@Override
20+
protected int compare(Number number) {
21+
return DecimalNumberComparatorHelper.compare( number, minValue, InfinityNumberComparatorHelper.LESS_THAN );
1922
}
2023
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal;
8+
9+
/**
10+
* Check that the number being validated is greater than or equal to the minimum
11+
* value specified.
12+
*
13+
* @author Marko Bekhta
14+
*/
15+
public class DecimalMinValidatorForShort extends AbstractDecimalMinValidator<Short> {
16+
17+
@Override
18+
protected int compare(Short number) {
19+
return DecimalNumberComparatorHelper.compare( number.longValue(), minValue );
20+
}
21+
}

engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/bv/number/bound/decimal/DecimalNumberComparatorHelper.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,33 @@ public static int compare(Long number, BigDecimal value) {
3232
return BigDecimal.valueOf( number ).compareTo( value );
3333
}
3434

35-
public static int compare(Number number, BigDecimal value) {
36-
return BigDecimal.valueOf( number.doubleValue() ).compareTo( value );
35+
public static int compare(Number number, BigDecimal value, OptionalInt treatNanAs) {
36+
// In case of comparing numbers we need to check for special cases:
37+
// 1. Floating point numbers should consider nan/infinity as values hence they should
38+
// be directed to corresponding overloaded methods:
39+
if ( number instanceof Double ) {
40+
return compare( (Double) number, value, treatNanAs );
41+
}
42+
if ( number instanceof Float ) {
43+
return compare( (Float) number, value, treatNanAs );
44+
}
45+
46+
// 2. For big numbers we don't want to lose any data so we just cast them and call corresponding methods:
47+
if ( number instanceof BigDecimal ) {
48+
return compare( (BigDecimal) number, value );
49+
}
50+
if ( number instanceof BigInteger ) {
51+
return compare( (BigInteger) number, value );
52+
}
53+
54+
// 3. For any integer types we convert them to long as we would do that anyway
55+
// to create a BigDecimal instance. And use corresponding method for longs:
56+
if ( number instanceof Byte || number instanceof Integer || number instanceof Long || number instanceof Short ) {
57+
return compare( number.longValue(), value );
58+
}
59+
60+
// 4. As a fallback we convert the number to double:
61+
return compare( number.doubleValue(), value, treatNanAs );
3762
}
3863

3964
public static int compare(Double number, BigDecimal value, OptionalInt treatNanAs) {

engine/src/main/java/org/hibernate/validator/internal/metadata/core/ConstraintHelper.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,22 @@
133133
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.MinValidatorForShort;
134134
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForBigDecimal;
135135
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForBigInteger;
136+
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForByte;
136137
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForDouble;
137138
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForFloat;
139+
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForInteger;
138140
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForLong;
139141
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForNumber;
142+
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForShort;
140143
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForBigDecimal;
141144
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForBigInteger;
145+
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForByte;
142146
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForDouble;
143147
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForFloat;
148+
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForInteger;
144149
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForLong;
145150
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForNumber;
151+
import org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForShort;
146152
import org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForBigDecimal;
147153
import org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForBigInteger;
148154
import org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForByte;
@@ -337,20 +343,26 @@ public ConstraintHelper() {
337343
putConstraints( tmpConstraints, DecimalMax.class, Arrays.asList(
338344
DecimalMaxValidatorForBigDecimal.class,
339345
DecimalMaxValidatorForBigInteger.class,
346+
DecimalMaxValidatorForByte.class,
340347
DecimalMaxValidatorForDouble.class,
341348
DecimalMaxValidatorForFloat.class,
342349
DecimalMaxValidatorForLong.class,
350+
DecimalMaxValidatorForInteger.class,
343351
DecimalMaxValidatorForNumber.class,
352+
DecimalMaxValidatorForShort.class,
344353
DecimalMaxValidatorForCharSequence.class,
345354
DecimalMaxValidatorForMonetaryAmount.class
346355
) );
347356
putConstraints( tmpConstraints, DecimalMin.class, Arrays.asList(
348357
DecimalMinValidatorForBigDecimal.class,
349358
DecimalMinValidatorForBigInteger.class,
359+
DecimalMinValidatorForByte.class,
350360
DecimalMinValidatorForDouble.class,
351361
DecimalMinValidatorForFloat.class,
352362
DecimalMinValidatorForLong.class,
363+
DecimalMinValidatorForInteger.class,
353364
DecimalMinValidatorForNumber.class,
365+
DecimalMinValidatorForShort.class,
354366
DecimalMinValidatorForCharSequence.class,
355367
DecimalMinValidatorForMonetaryAmount.class
356368
) );
@@ -359,19 +371,25 @@ public ConstraintHelper() {
359371
putConstraints( tmpConstraints, DecimalMax.class, Arrays.asList(
360372
DecimalMaxValidatorForBigDecimal.class,
361373
DecimalMaxValidatorForBigInteger.class,
374+
DecimalMaxValidatorForByte.class,
362375
DecimalMaxValidatorForDouble.class,
363376
DecimalMaxValidatorForFloat.class,
364377
DecimalMaxValidatorForLong.class,
378+
DecimalMaxValidatorForInteger.class,
365379
DecimalMaxValidatorForNumber.class,
380+
DecimalMaxValidatorForShort.class,
366381
DecimalMaxValidatorForCharSequence.class
367382
) );
368383
putConstraints( tmpConstraints, DecimalMin.class, Arrays.asList(
369384
DecimalMinValidatorForBigDecimal.class,
370385
DecimalMinValidatorForBigInteger.class,
386+
DecimalMinValidatorForByte.class,
371387
DecimalMinValidatorForDouble.class,
372388
DecimalMinValidatorForFloat.class,
373389
DecimalMinValidatorForLong.class,
390+
DecimalMinValidatorForInteger.class,
374391
DecimalMinValidatorForNumber.class,
392+
DecimalMinValidatorForShort.class,
375393
DecimalMinValidatorForCharSequence.class
376394
) );
377395
}

0 commit comments

Comments
 (0)