Author: dion
Date: Mon Aug 25 22:21:57 2008
New Revision: 688962
URL:
http://svn.apache.org/viewvc?rev=688962&view=revLog:
add support for BigInteger and BigDecimal in JEXL arithmetic.
If either the left or right operands are BigD or BigI, the result is returned as that type
Modified:
commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java
commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java
commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java
commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java
Modified: commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java?rev=688962&r1=688961&r2=688962&view=diff==============================================================================
--- commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java (original)
+++ commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java Mon Aug 25 22:21:57 2008
@@ -17,9 +17,13 @@
package org.apache.commons.jexl.parser;
+import java.math.BigInteger;
+import java.math.BigDecimal;
+
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.util.Coercion;
+
/**
* Addition : either integer addition or string concatenation.
*
@@ -29,7 +33,7 @@
public class ASTAddNode extends SimpleNode {
/**
* Create the node given an id.
- *
+ *
* @param id node id.
*/
public ASTAddNode(int id) {
@@ -38,7 +42,7 @@
/**
* Create a node with the given parser and id.
- *
+ *
* @param p a parser.
* @param id node id.
*/
@@ -47,7 +51,7 @@
}
- /**
+ /**
* {@inheritDoc}
*/
public Object jjtAccept(ParserVisitor visitor, Object data) {
@@ -55,7 +59,7 @@
}
/**
- * {@inheritDoc}
+ * {@inheritDoc}
*/
public Object value(JexlContext context) throws Exception {
Object left = ((SimpleNode) jjtGetChild(0)).value(context);
@@ -68,6 +72,19 @@
return new Long(0);
}
+
+ if (left instanceof BigInteger || right instanceof BigInteger) {
+ BigInteger l = Coercion.coerceBigInteger(left);
+ BigInteger r = Coercion.coerceBigInteger(right);
+ return l.add(r);
+ }
+
+ if (left instanceof BigDecimal || right instanceof BigDecimal) {
+ BigDecimal l = Coercion.coerceBigDecimal(left);
+ BigDecimal r = Coercion.coerceBigDecimal(right);
+ return l.add(r);
+ }
+
/*
* if anything is float, double or string with ( "." | "E" | "e")
* coerce all to doubles and do it
@@ -75,7 +92,7 @@
if (left instanceof Float || left instanceof Double
|| right instanceof Float || right instanceof Double
|| (left instanceof String
- && (((String) left).indexOf(".") != -1
+ && (((String) left).indexOf(".") != -1
|| ((String) left).indexOf("e") != -1
|| ((String) left).indexOf("E") != -1)
)
Modified: commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java?rev=688962&r1=688961&r2=688962&view=diff==============================================================================
--- commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java (original)
+++ commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java Mon Aug 25 22:21:57 2008
@@ -16,19 +16,22 @@
*/
package org.apache.commons.jexl.parser;
+import java.math.BigInteger;
+import java.math.BigDecimal;
+
import org.apache.commons.jexl.util.Coercion;
import org.apache.commons.jexl.JexlContext;
/**
* a / b, mathematical divide.
- *
+ *
* @author <a href="mailto:
geirm@...">Geir Magnusson Jr.</a>
* @version $Id$
*/
public class ASTDivNode extends SimpleNode {
/**
* Create the node given an id.
- *
+ *
* @param id node id.
*/
public ASTDivNode(int id) {
@@ -37,7 +40,7 @@
/**
* Create a node with the given parser and id.
- *
+ *
* @param p a parser.
* @param id node id.
*/
@@ -62,6 +65,18 @@
return new Byte((byte) 0);
}
+ if (left instanceof BigInteger || right instanceof BigInteger) {
+ BigInteger l = Coercion.coerceBigInteger(left);
+ BigInteger r = Coercion.coerceBigInteger(right);
+ return l.divide(r);
+ }
+
+ if (left instanceof BigDecimal || right instanceof BigDecimal) {
+ BigDecimal l = Coercion.coerceBigDecimal(left);
+ BigDecimal r = Coercion.coerceBigDecimal(right);
+ return l.divide(r);
+ }
+
Double l = Coercion.coerceDouble(left);
Double r = Coercion.coerceDouble(right);
Modified: commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java?rev=688962&r1=688961&r2=688962&view=diff==============================================================================
--- commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java (original)
+++ commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java Mon Aug 25 22:21:57 2008
@@ -17,19 +17,22 @@
package org.apache.commons.jexl.parser;
+import java.math.BigInteger;
+import java.math.BigDecimal;
+
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.util.Coercion;
/**
* Multiplication.
- *
+ *
* @author <a href="mailto:
geirm@...">Geir Magnusson Jr.</a>
* @version $Id$
*/
public class ASTMulNode extends SimpleNode {
/**
* Create the node given an id.
- *
+ *
* @param id node id.
*/
public ASTMulNode(int id) {
@@ -38,7 +41,7 @@
/**
* Create a node with the given parser and id.
- *
+ *
* @param p a parser.
* @param id node id.
*/
@@ -63,6 +66,18 @@
return new Byte((byte) 0);
}
+ if (left instanceof BigInteger || right instanceof BigInteger) {
+ BigInteger l = Coercion.coerceBigInteger(left);
+ BigInteger r = Coercion.coerceBigInteger(right);
+ return l.multiply(r);
+ }
+
+ if (left instanceof BigDecimal || right instanceof BigDecimal) {
+ BigDecimal l = Coercion.coerceBigDecimal(left);
+ BigDecimal r = Coercion.coerceBigDecimal(right);
+ return l.multiply(r);
+ }
+
/*
* if anything is float, double or string with ( "." | "E" | "e") coerce
* all to doubles and do it
@@ -71,13 +86,13 @@
|| left instanceof Double
|| right instanceof Float
|| right instanceof Double
- || (left instanceof String
- && (((String) left).indexOf(".") != -1
- || ((String) left).indexOf("e") != -1
+ || (left instanceof String
+ && (((String) left).indexOf(".") != -1
+ || ((String) left).indexOf("e") != -1
|| ((String) left).indexOf("E") != -1))
- || (right instanceof String
- && (((String) right).indexOf(".") != -1
- || ((String) right).indexOf("e") != -1
+ || (right instanceof String
+ && (((String) right).indexOf(".") != -1
+ || ((String) right).indexOf("e") != -1
|| ((String) right).indexOf("E") != -1))) {
Double l = Coercion.coerceDouble(left);
Double r = Coercion.coerceDouble(right);
Modified: commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java?rev=688962&r1=688961&r2=688962&view=diff==============================================================================
--- commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java (original)
+++ commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java Mon Aug 25 22:21:57 2008
@@ -17,12 +17,15 @@
package org.apache.commons.jexl.parser;
+import java.math.BigInteger;
+import java.math.BigDecimal;
+
import org.apache.commons.jexl.util.Coercion;
import org.apache.commons.jexl.JexlContext;
/**
* Subtraction.
- *
+ *
* @author <a href="mailto:
geirm@...">Geir Magnusson Jr.</a>
* @author <a href="mailto:
mhw@...">Mark H. Wilkinson</a>
* @version $Id$
@@ -30,7 +33,7 @@
public class ASTSubtractNode extends SimpleNode {
/**
* Create the node given an id.
- *
+ *
* @param id node id.
*/
public ASTSubtractNode(int id) {
@@ -39,7 +42,7 @@
/**
* Create a node with the given parser and id.
- *
+ *
* @param p a parser.
* @param id node id.
*/
@@ -59,6 +62,18 @@
return new Byte((byte) 0);
}
+ if (left instanceof BigInteger || right instanceof BigInteger) {
+ BigInteger l = Coercion.coerceBigInteger(left);
+ BigInteger r = Coercion.coerceBigInteger(right);
+ return l.subtract(r);
+ }
+
+ if (left instanceof BigDecimal || right instanceof BigDecimal) {
+ BigDecimal l = Coercion.coerceBigDecimal(left);
+ BigDecimal r = Coercion.coerceBigDecimal(right);
+ return l.subtract(r);
+ }
+
/*
* if anything is float, double or string with ( "." | "E" | "e") coerce
* all to doubles and do it
@@ -67,13 +82,13 @@
|| left instanceof Double
|| right instanceof Float
|| right instanceof Double
- || (left instanceof String
- && (((String) left).indexOf(".") != -1
- || ((String) left).indexOf("e") != -1
+ || (left instanceof String
+ && (((String) left).indexOf(".") != -1
+ || ((String) left).indexOf("e") != -1
|| ((String) left).indexOf("E") != -1))
- || (right instanceof String
- && (((String) right).indexOf(".") != -1
- || ((String) right).indexOf("e") != -1
+ || (right instanceof String
+ && (((String) right).indexOf(".") != -1
+ || ((String) right).indexOf("e") != -1
|| ((String) right).indexOf("E") != -1))) {
Double l = Coercion.coerceDouble(left);
Double r = Coercion.coerceDouble(right);