org.apache.commons.jexl2.JadeJexlArithmetic Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jade4j Show documentation
Show all versions of jade4j Show documentation
Java implementation of the jade templating language
package org.apache.commons.jexl2;
import org.apache.commons.collections.CollectionUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
public class JadeJexlArithmetic extends JexlArithmetic {
public JadeJexlArithmetic(boolean lenient) {
super(lenient);
}
protected int compare(Object left, Object right, String operator) {
if (left != null && right != null) {
if (left instanceof String || right instanceof String) {
return toString(left).compareTo(toString(right));
} else if (left instanceof BigDecimal || right instanceof BigDecimal) {
BigDecimal l = toBigDecimal(left);
BigDecimal r = toBigDecimal(right);
return l.compareTo(r);
} else if (left instanceof BigInteger || right instanceof BigInteger) {
BigInteger l = toBigInteger(left);
BigInteger r = toBigInteger(right);
return l.compareTo(r);
} else if (isFloatingPoint(left) || isFloatingPoint(right)) {
double lhs = toDouble(left);
double rhs = toDouble(right);
if (Double.isNaN(lhs)) {
if (Double.isNaN(rhs)) {
return 0;
} else {
return -1;
}
} else if (Double.isNaN(rhs)) {
// lhs is not NaN
return +1;
} else if (lhs < rhs) {
return -1;
} else if (lhs > rhs) {
return +1;
} else {
return 0;
}
} else if (isNumberable(left) || isNumberable(right)) {
long lhs = toLong(left);
long rhs = toLong(right);
if (lhs < rhs) {
return -1;
} else if (lhs > rhs) {
return +1;
} else {
return 0;
}
} else if ("==".equals(operator)) {
return left.equals(right) ? 0 : -1;
} else if (left instanceof Comparable>) {
@SuppressWarnings("unchecked") // OK because of instanceof check above
final Comparable