Compare two objects, after coercing to the same type if appropriate. If the objects are identical, or they are equal according to #equals(ELContext, Object, Object) then return 0. If either object is a BigDecimal, then coerce both to BigDecimal first. Similarly for Double(Float), Big
(final ELContext ctx, final Object obj0, final Object obj1)
| 83 | * @throws ClassCastException if the objects are not mutually comparable |
| 84 | */ |
| 85 | public static int compare(final ELContext ctx, final Object obj0, final Object obj1) throws ELException { |
| 86 | if (obj0 == obj1 || equals(ctx, obj0, obj1)) { |
| 87 | return 0; |
| 88 | } |
| 89 | if (isBigDecimalOp(obj0, obj1)) { |
| 90 | BigDecimal bd0 = (BigDecimal) coerceToNumber(ctx, obj0, BigDecimal.class); |
| 91 | BigDecimal bd1 = (BigDecimal) coerceToNumber(ctx, obj1, BigDecimal.class); |
| 92 | return bd0.compareTo(bd1); |
| 93 | } |
| 94 | if (isDoubleOp(obj0, obj1)) { |
| 95 | Double d0 = (Double) coerceToNumber(ctx, obj0, Double.class); |
| 96 | Double d1 = (Double) coerceToNumber(ctx, obj1, Double.class); |
| 97 | return d0.compareTo(d1); |
| 98 | } |
| 99 | if (isBigIntegerOp(obj0, obj1)) { |
| 100 | BigInteger bi0 = (BigInteger) coerceToNumber(ctx, obj0, BigInteger.class); |
| 101 | BigInteger bi1 = (BigInteger) coerceToNumber(ctx, obj1, BigInteger.class); |
| 102 | return bi0.compareTo(bi1); |
| 103 | } |
| 104 | if (isLongOp(obj0, obj1)) { |
| 105 | Long l0 = (Long) coerceToNumber(ctx, obj0, Long.class); |
| 106 | Long l1 = (Long) coerceToNumber(ctx, obj1, Long.class); |
| 107 | return l0.compareTo(l1); |
| 108 | } |
| 109 | if (obj0 instanceof String || obj1 instanceof String) { |
| 110 | return coerceToString(ctx, obj0).compareTo(coerceToString(ctx, obj1)); |
| 111 | } |
| 112 | if (obj0 instanceof Comparable<?>) { |
| 113 | @SuppressWarnings("unchecked") // checked above |
| 114 | final Comparable<Object> comparable = (Comparable<Object>) obj0; |
| 115 | return (obj1 != null) ? comparable.compareTo(obj1) : 1; |
| 116 | } |
| 117 | if (obj1 instanceof Comparable<?>) { |
| 118 | @SuppressWarnings("unchecked") // checked above |
| 119 | final Comparable<Object> comparable = (Comparable<Object>) obj1; |
| 120 | return (obj0 != null) ? -comparable.compareTo(obj0) : -1; |
| 121 | } |
| 122 | throw new ELException(MessageFactory.get("error.compare", obj0, obj1)); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Compare two objects for equality, after coercing to the same type if appropriate. |