From 838855b1aae45669e40d8d9a9e332aaac98fb543 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 16 Jan 2014 16:54:40 +0100 Subject: [PATCH] Fixed accidental use of JDK 1.7+ Integer/Long.compare methods Issue: SPR-11319 --- .../spel/support/StandardTypeComparator.java | 65 ++++++++++--------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java index 9974ffc535..6f05bb46e7 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java @@ -24,8 +24,8 @@ import org.springframework.expression.spel.SpelMessage; import org.springframework.util.NumberUtils; /** - * A simple basic TypeComparator implementation. It supports comparison of numbers and - * types implementing Comparable. + * A simple basic {@link TypeComparator} implementation. + * It supports comparison of Numbers and types implementing Comparable. * * @author Andy Clement * @author Juergen Hoeller @@ -34,15 +34,29 @@ import org.springframework.util.NumberUtils; */ 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 @SuppressWarnings("unchecked") public int compare(Object left, Object right) throws SpelEvaluationException { // If one is null, check if the other is if (left == null) { - return right == null ? 0 : -1; + return (right == null ? 0 : -1); } else if (right == null) { - return 1; // left cannot be null + return 1; // left cannot be null at this point } // Basic number comparisons @@ -55,48 +69,41 @@ public class StandardTypeComparator implements TypeComparator { BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class); return leftBigDecimal.compareTo(rightBigDecimal); } - - if (leftNumber instanceof Double || rightNumber instanceof Double) { + else if (leftNumber instanceof Double || rightNumber instanceof Double) { return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue()); } - - if (leftNumber instanceof Float || rightNumber instanceof Float) { + else if (leftNumber instanceof Float || rightNumber instanceof Float) { return Float.compare(leftNumber.floatValue(), rightNumber.floatValue()); } - - if (leftNumber instanceof Long || rightNumber instanceof Long) { - return Long.compare(leftNumber.longValue(), rightNumber.longValue()); + 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 { + // 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 { if (left instanceof Comparable) { - return ((Comparable) 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()); } - @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; - } + private static int compare(int x, int y) { + return (x < y ? -1 : (x > y ? 1 : 0)); + } - return false; + private static int compare(long x, long y) { + return (x < y ? -1 : (x > y ? 1 : 0)); } }