Author: epbernard
Date: 2006-08-31 00:10:44 -0400 (Thu, 31 Aug 2006)
New Revision: 10384
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/MaxValidator.java
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/MinValidator.java
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/RangeValidator.java
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/Tv.java
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/ValidatorTest.java
Log:
ANN-423 use BigDecimal to parse strings, compare BigDecimal/BigIntegers if needed
Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/validator/MaxValidator.java
===================================================================
---
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/MaxValidator.java 2006-08-31
01:51:18 UTC (rev 10383)
+++
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/MaxValidator.java 2006-08-31
04:10:44 UTC (rev 10384)
@@ -2,6 +2,8 @@
package org.hibernate.validator;
import java.io.Serializable;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;
@@ -24,8 +26,7 @@
if ( value == null ) return true;
if ( value instanceof String ) {
try {
- double dv = Double.parseDouble( (String) value );
- return dv <= max;
+ return new BigDecimal( (String) value ).compareTo( BigDecimal.valueOf( max ) ) <=
0;
}
catch (NumberFormatException nfe) {
return false;
@@ -35,6 +36,12 @@
double dv = ( (Number) value ).doubleValue();
return dv <= max;
}
+ else if ( value instanceof BigInteger ) {
+ return ( (BigInteger) value ).compareTo( BigInteger.valueOf( max ) ) <= 0;
+ }
+ else if ( value instanceof BigDecimal ) {
+ return ( (BigDecimal) value ).compareTo( BigDecimal.valueOf( max ) ) <= 0;
+ }
else if ( value instanceof Number ) {
long lv = ( (Number) value ).longValue();
return lv <= max;
Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/validator/MinValidator.java
===================================================================
---
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/MinValidator.java 2006-08-31
01:51:18 UTC (rev 10383)
+++
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/MinValidator.java 2006-08-31
04:10:44 UTC (rev 10384)
@@ -2,6 +2,8 @@
package org.hibernate.validator;
import java.io.Serializable;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;
@@ -24,8 +26,7 @@
if ( value == null ) return true;
if ( value instanceof String ) {
try {
- double dv = Double.parseDouble( (String) value );
- return dv >= min;
+ return new BigDecimal( (String) value ).compareTo( BigDecimal.valueOf(min) ) >=
0;
}
catch (NumberFormatException nfe) {
return false;
@@ -35,6 +36,12 @@
double dv = ( (Number) value ).doubleValue();
return dv >= min;
}
+ else if ( value instanceof BigInteger ) {
+ return ( (BigInteger) value ).compareTo( BigInteger.valueOf( min ) ) >= 0;
+ }
+ else if ( value instanceof BigDecimal ) {
+ return ( (BigDecimal) value ).compareTo( BigDecimal.valueOf( min ) ) >= 0;
+ }
else if ( value instanceof Number ) {
long lv = ( (Number) value ).longValue();
return lv >= min;
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/RangeValidator.java
===================================================================
---
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/RangeValidator.java 2006-08-31
01:51:18 UTC (rev 10383)
+++
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/RangeValidator.java 2006-08-31
04:10:44 UTC (rev 10384)
@@ -2,6 +2,7 @@
package org.hibernate.validator;
import java.io.Serializable;
+import java.math.BigDecimal;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;
@@ -24,8 +25,8 @@
if ( value == null ) return true;
if ( value instanceof String ) {
try {
- double dv = Double.parseDouble( (String) value );
- return dv >= min && dv <= max;
+ BigDecimal dv = new BigDecimal( (String) value );
+ return dv.compareTo( BigDecimal.valueOf( min ) ) >= 0 && dv.compareTo(
BigDecimal.valueOf( max ) ) <= 0;
}
catch (NumberFormatException nfe) {
return false;
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/Tv.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/Tv.java 2006-08-31
01:51:18 UTC (rev 10383)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/Tv.java 2006-08-31
04:10:44 UTC (rev 10384)
@@ -2,11 +2,13 @@
package org.hibernate.validator.test;
import java.util.Date;
+import java.math.BigInteger;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.validator.Future;
import org.hibernate.validator.Length;
+import org.hibernate.validator.Min;
/**
* @author Emmanuel Bernard
@@ -23,4 +25,6 @@
public Date expDate;
@Length(min = 0)
public String description;
+ @Min(1000)
+ public BigInteger lifetime;
}
Modified:
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/ValidatorTest.java
===================================================================
---
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/ValidatorTest.java 2006-08-31
01:51:18 UTC (rev 10383)
+++
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/ValidatorTest.java 2006-08-31
04:10:44 UTC (rev 10384)
@@ -4,6 +4,7 @@
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
+import java.math.BigInteger;
import junit.framework.TestCase;
import org.hibernate.validator.ClassValidator;
@@ -122,6 +123,14 @@
}
+ public void testBigInteger() throws Exception {
+ Tv tv = new Tv();
+ tv.lifetime = new BigInteger("9223372036854775808");
+ ClassValidator<Tv> classValidator = new ClassValidator<Tv>( Tv.class );
+ InvalidValue[] invalidValues = classValidator.getInvalidValues( tv );
+ assertEquals( 0, invalidValues.length );
+ }
+
public void testBeanValidator() throws Exception {
Vase v = new Vase();
ClassValidator<Vase> classValidator = new ClassValidator<Vase>( Vase.class
);