Author: dion
Date: Mon Aug 25 22:21:16 2008
New Revision: 688961
URL: http://svn.apache.org/viewvc?rev=688961&view=rev
Log:
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/util/Coercion.java
Modified:
commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/util/Coercion.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/util/Coercion.java?rev=688961&r1=688960&r2=688961&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/util/Coercion.java
(original)
+++
commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/util/Coercion.java
Mon Aug 25 22:21:16 2008
@@ -16,6 +16,9 @@
*/
package org.apache.commons.jexl.util;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
/**
* Coercion utilities for the JSTL EL-like coercion.
*
@@ -135,6 +138,70 @@
}
/**
+ * Coerce to a BigDecimal.
+ *
+ * @param val Object to be coerced.
+ * @return The Double coerced value.
+ * @throws Exception If coercion fails.
+ */
+ public static BigDecimal coerceBigDecimal(Object val)
+ throws Exception {
+ if (val == null) {
+ return BigDecimal.valueOf(0);
+ } else if (val instanceof String) {
+ if ("".equals(val)) {
+ return BigDecimal.valueOf(0);
+ }
+
+ /*
+ * the spec seems to be iffy about this. Going to give it a wack
+ * anyway
+ */
+
+ return new BigDecimal((String) val);
+ } else if (val instanceof BigDecimal) {
+ return (BigDecimal) val;
+ } else if (val instanceof Character) {
+ return BigDecimal.valueOf(((Character) val).charValue());
+ } else if (val instanceof Number) {
+ return new BigDecimal(val.toString());
+ } else if (val instanceof Boolean) {
+ throw new Exception("Boolean->Double coercion exception");
+ }
+
+ throw new Exception("Double coercion exception");
+ }
+
+ /**
+ * Coerce to a BigInteger.
+ *
+ * @param val Object to be coerced.
+ * @return The Integer coerced value.
+ * @throws Exception If coercion fails.
+ */
+ public static BigInteger coerceBigInteger(Object val)
+ throws Exception {
+ if (val == null) {
+ return BigInteger.ZERO;
+ } else if (val instanceof String) {
+ if ("".equals(val)) {
+ return BigInteger.ZERO;
+ }
+ return new BigInteger((String) val);
+ } else if (val instanceof BigInteger) {
+ return (BigInteger) val;
+ } else if (val instanceof Character) {
+ return BigInteger.valueOf(((Character) val).charValue());
+ } else if (val instanceof Number) {
+ return BigInteger.valueOf(((Number) val).longValue());
+ } else if (val instanceof Boolean) {
+ throw new Exception("Boolean->Integer coercion exception");
+ }
+
+ throw new Exception("Integer coercion exception");
+ }
+
+ /**
* Is Object a floating point number.
*
* @param o Object to be analyzed.