Fixed accidental use of JDK 1.7+ Integer/Long.compare methods

Issue: SPR-11319
This commit is contained in:
Juergen Hoeller 2014-01-16 16:54:40 +01:00
parent 11bc9d0aeb
commit 838855b1aa
1 changed files with 36 additions and 29 deletions

View File

@ -24,8 +24,8 @@ import org.springframework.expression.spel.SpelMessage;
import org.springframework.util.NumberUtils; import org.springframework.util.NumberUtils;
/** /**
* A simple basic TypeComparator implementation. It supports comparison of numbers and * A simple basic {@link TypeComparator} implementation.
* types implementing Comparable. * It supports comparison of Numbers and types implementing Comparable.
* *
* @author Andy Clement * @author Andy Clement
* @author Juergen Hoeller * @author Juergen Hoeller
@ -34,15 +34,29 @@ import org.springframework.util.NumberUtils;
*/ */
public class StandardTypeComparator implements TypeComparator { public class StandardTypeComparator implements TypeComparator {
@Override
public boolean canCompare(Object left, Object right) {
if (left == null || right == null) {
return true;
}
if (left instanceof Number && right instanceof Number) {
return true;
}
if (left instanceof Comparable) {
return true;
}
return false;
}
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public int compare(Object left, Object right) throws SpelEvaluationException { public int compare(Object left, Object right) throws SpelEvaluationException {
// If one is null, check if the other is // If one is null, check if the other is
if (left == null) { if (left == null) {
return right == null ? 0 : -1; return (right == null ? 0 : -1);
} }
else if (right == null) { else if (right == null) {
return 1; // left cannot be null return 1; // left cannot be null at this point
} }
// Basic number comparisons // Basic number comparisons
@ -55,48 +69,41 @@ public class StandardTypeComparator implements TypeComparator {
BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
return leftBigDecimal.compareTo(rightBigDecimal); return leftBigDecimal.compareTo(rightBigDecimal);
} }
else if (leftNumber instanceof Double || rightNumber instanceof Double) {
if (leftNumber instanceof Double || rightNumber instanceof Double) {
return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue()); return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
} }
else if (leftNumber instanceof Float || rightNumber instanceof Float) {
if (leftNumber instanceof Float || rightNumber instanceof Float) {
return Float.compare(leftNumber.floatValue(), rightNumber.floatValue()); return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
} }
else if (leftNumber instanceof Long || rightNumber instanceof Long) {
if (leftNumber instanceof Long || rightNumber instanceof Long) { // Don't call Long.compare here - only available on JDK 1.7+
return Long.compare(leftNumber.longValue(), rightNumber.longValue()); return compare(leftNumber.longValue(), rightNumber.longValue());
}
else {
// Don't call Integer.compare here - only available on JDK 1.7+
return compare(leftNumber.intValue(), rightNumber.intValue());
} }
return Integer.compare(leftNumber.intValue(), rightNumber.intValue());
} }
try { try {
if (left instanceof Comparable) { if (left instanceof Comparable) {
return ((Comparable<Object>) left).compareTo(right); return ((Comparable) left).compareTo(right);
} }
} catch (ClassCastException cce) { }
throw new SpelEvaluationException(cce, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass()); catch (ClassCastException ex) {
throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
} }
throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass()); throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
} }
@Override
public boolean canCompare(Object left, Object right) {
if (left == null || right == null) {
return true;
}
if (left instanceof Number && right instanceof Number) { private static int compare(int x, int y) {
return true; return (x < y ? -1 : (x > y ? 1 : 0));
} }
if (left instanceof Comparable) { private static int compare(long x, long y) {
return true; return (x < y ? -1 : (x > y ? 1 : 0));
}
return false;
} }
} }