Skip to content

Commit 92ad02a

Browse files
committed
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
1 parent fb08644 commit 92ad02a

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

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

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.expression.spel.support;
1818

1919
import java.math.BigDecimal;
20+
import java.math.BigInteger;
2021

2122
import org.springframework.expression.TypeComparator;
2223
import org.springframework.expression.spel.SpelEvaluationException;
@@ -75,19 +76,36 @@ else if (leftNumber instanceof Double || rightNumber instanceof Double) {
7576
else if (leftNumber instanceof Float || rightNumber instanceof Float) {
7677
return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
7778
}
79+
else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
80+
BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
81+
BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
82+
return leftBigInteger.compareTo(rightBigInteger);
83+
}
7884
else if (leftNumber instanceof Long || rightNumber instanceof Long) {
7985
// Don't call Long.compare here - only available on JDK 1.7+
8086
return compare(leftNumber.longValue(), rightNumber.longValue());
8187
}
82-
else {
88+
else if (leftNumber instanceof Integer || rightNumber instanceof Integer) {
8389
// Don't call Integer.compare here - only available on JDK 1.7+
8490
return compare(leftNumber.intValue(), rightNumber.intValue());
8591
}
92+
else if (leftNumber instanceof Short || rightNumber instanceof Short) {
93+
// Don't call Short.compare here - only available on JDK 1.7+
94+
return compare(leftNumber.shortValue(), rightNumber.shortValue());
95+
}
96+
else if (leftNumber instanceof Byte || rightNumber instanceof Byte) {
97+
// Don't call Short.compare here - only available on JDK 1.7+
98+
return compare(leftNumber.byteValue(), rightNumber.byteValue());
99+
}
100+
else {
101+
// Unknown Number subtypes -> best guess is double multiplication
102+
return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
103+
}
86104
}
87105

88106
try {
89107
if (left instanceof Comparable) {
90-
return ((Comparable) left).compareTo(right);
108+
return ((Comparable<Object>) left).compareTo(right);
91109
}
92110
}
93111
catch (ClassCastException ex) {
@@ -98,12 +116,20 @@ else if (leftNumber instanceof Long || rightNumber instanceof Long) {
98116
}
99117

100118

101-
private static int compare(int x, int y) {
119+
private static int compare(long x, long y) {
102120
return (x < y ? -1 : (x > y ? 1 : 0));
103121
}
104122

105-
private static int compare(long x, long y) {
123+
private static int compare(int x, int y) {
106124
return (x < y ? -1 : (x > y ? 1 : 0));
107125
}
108126

127+
private static int compare(short x, short y) {
128+
return x - y;
129+
}
130+
131+
private static int compare(byte x, byte y) {
132+
return x - y;
133+
}
134+
109135
}

0 commit comments

Comments
 (0)