Browse Source

Consistent number handling in StandardTypeComparator (BigInteger support, doubleValue fallback)

Also includes a fix for the Comparable raw type invocation.

Issue: SPR-9913
Issue: SPR-12353
pull/666/merge
Juergen Hoeller 10 years ago
parent
commit
92ad02ae8f
  1. 36
      spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java

36
spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.expression.spel.support;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.springframework.expression.TypeComparator;
import org.springframework.expression.spel.SpelEvaluationException;
@ -75,19 +76,36 @@ public class StandardTypeComparator implements TypeComparator { @@ -75,19 +76,36 @@ public class StandardTypeComparator implements TypeComparator {
else if (leftNumber instanceof Float || rightNumber instanceof Float) {
return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
}
else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
return leftBigInteger.compareTo(rightBigInteger);
}
else if (leftNumber instanceof Long || rightNumber instanceof Long) {
// Don't call Long.compare here - only available on JDK 1.7+
return compare(leftNumber.longValue(), rightNumber.longValue());
}
else {
else if (leftNumber instanceof Integer || rightNumber instanceof Integer) {
// Don't call Integer.compare here - only available on JDK 1.7+
return compare(leftNumber.intValue(), rightNumber.intValue());
}
else if (leftNumber instanceof Short || rightNumber instanceof Short) {
// Don't call Short.compare here - only available on JDK 1.7+
return compare(leftNumber.shortValue(), rightNumber.shortValue());
}
else if (leftNumber instanceof Byte || rightNumber instanceof Byte) {
// Don't call Short.compare here - only available on JDK 1.7+
return compare(leftNumber.byteValue(), rightNumber.byteValue());
}
else {
// Unknown Number subtypes -> best guess is double multiplication
return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
}
}
try {
if (left instanceof Comparable) {
return ((Comparable) left).compareTo(right);
return ((Comparable<Object>) left).compareTo(right);
}
}
catch (ClassCastException ex) {
@ -98,12 +116,20 @@ public class StandardTypeComparator implements TypeComparator { @@ -98,12 +116,20 @@ public class StandardTypeComparator implements TypeComparator {
}
private static int compare(int x, int y) {
private static int compare(long x, long y) {
return (x < y ? -1 : (x > y ? 1 : 0));
}
private static int compare(long x, long y) {
private static int compare(int x, int y) {
return (x < y ? -1 : (x > y ? 1 : 0));
}
private static int compare(short x, short y) {
return x - y;
}
private static int compare(byte x, byte y) {
return x - y;
}
}

Loading…
Cancel
Save