sahvx655-wq commented on code in PR #394:
URL: https://github.com/apache/commons-validator/pull/394#discussion_r3385516184


##########
src/main/java/org/apache/commons/validator/routines/BigIntegerValidator.java:
##########
@@ -157,7 +158,12 @@ public boolean minValue(final BigInteger value, final long 
min) {
      */
     @Override
     protected Object processParsedValue(final Object value, final Format 
formatter) {
-        return BigInteger.valueOf(((Number) value).longValue());
+        // A value that fits in a long is parsed as a Long; anything larger 
comes back as a
+        // Double, whose longValue() saturates at Long.MAX_VALUE and loses the 
magnitude.

Review Comment:
   Fair point, that comment was describing the old failure path rather than the 
code in front of you, so it just read as noise. I've removed it in 2e71709. The 
two branches now mirror BigDecimalValidator.processParsedValue, which has the 
same Long-versus-string split and carries no comment.



##########
src/main/java/org/apache/commons/validator/routines/BigIntegerValidator.java:
##########
@@ -157,7 +158,12 @@ public boolean minValue(final BigInteger value, final long 
min) {
      */
     @Override
     protected Object processParsedValue(final Object value, final Format 
formatter) {
-        return BigInteger.valueOf(((Number) value).longValue());
+        // A value that fits in a long is parsed as a Long; anything larger 
comes back as a
+        // Double, whose longValue() saturates at Long.MAX_VALUE and loses the 
magnitude.
+        if (value instanceof Long) {
+            return BigInteger.valueOf(((Long) value).longValue());
+        }
+        return new BigDecimal(value.toString()).toBigInteger();

Review Comment:
   Right, both branches return a BigInteger: the Long case through 
BigInteger.valueOf, anything larger through new 
BigDecimal(value.toString()).toBigInteger(). The split is only about how the 
value arrives, not the type that comes back. NumberFormat returns a Long while 
the input still fits in a long and switches to a Double once it overflows, and 
Double.longValue() saturates at Long.MAX_VALUE, which is what was quietly 
dropping the magnitude. Routing the non-Long case through its string form keeps 
the digits and lines up with what BigDecimalValidator already does.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to