commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r1066867 - in /commons/proper/jexl/trunk: pom.xml src/main/java/org/apache/commons/jexl2/JexlArithmetic.java src/site/xdoc/changes.xml src/test/java/org/apache/commons/jexl2/IssuesTest.java
Date Thu, 03 Feb 2011 16:57:58 GMT
Author: henrib
Date: Thu Feb  3 16:57:58 2011
New Revision: 1066867

URL: http://svn.apache.org/viewvc?rev=1066867&view=rev
Log:
JEXL-106: added a MathContext to JexlArithmetic to control big decimal operations. Default
uses IEEE 754R Decimal128 format.

Modified:
    commons/proper/jexl/trunk/pom.xml
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlArithmetic.java
    commons/proper/jexl/trunk/src/site/xdoc/changes.xml
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java

Modified: commons/proper/jexl/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/pom.xml?rev=1066867&r1=1066866&r2=1066867&view=diff
==============================================================================
--- commons/proper/jexl/trunk/pom.xml (original)
+++ commons/proper/jexl/trunk/pom.xml Thu Feb  3 16:57:58 2011
@@ -19,12 +19,12 @@
     <parent>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-parent</artifactId>
-        <version>15</version>
+        <version>17</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-jexl</artifactId>
-    <version>2.1-SNAPSHOT</version>
+    <version>2.0.2-SNAPSHOT</version>
     <name>Commons JEXL</name>
     <inceptionYear>2001</inceptionYear>
     <description>Jexl is an implementation of the JSTL Expression Language with extensions.</description>
@@ -123,9 +123,9 @@
         <maven.compile.source>1.5</maven.compile.source>
         <maven.compile.target>1.5</maven.compile.target>
         <commons.componentid>jexl</commons.componentid>
-        <commons.release.version>2.0.1</commons.release.version>
+        <commons.release.version>2.0.2</commons.release.version>
         <!-- The RC version used in the staging repository URL. -->
-        <commons.rc.version>RC2</commons.rc.version>
+        <commons.rc.version>RC1</commons.rc.version>
         <commons.release.2.version>1.1</commons.release.2.version>
         <commons.release.2.binary.suffix />
         <commons.jira.id>JEXL</commons.jira.id>
@@ -151,6 +151,10 @@
             </resource>
         </resources>
         <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+             </plugin>
             <!-- workaround MRELEASE-424 when publishing -->
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
@@ -250,7 +254,7 @@
             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>cobertura-maven-plugin</artifactId>
-                <version>2.3</version>
+                <version>2.4</version>
                 <configuration>
                     <instrumentation>
                         <excludes>
@@ -274,7 +278,7 @@
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-pmd-plugin</artifactId>
-                <version>2.4</version>
+                <version>2.5</version>
                 <configuration>
                     <targetJdk>1.5</targetJdk>
                     <excludes>

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlArithmetic.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlArithmetic.java?rev=1066867&r1=1066866&r2=1066867&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlArithmetic.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlArithmetic.java Thu
Feb  3 16:57:58 2011
@@ -20,6 +20,7 @@ import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.math.MathContext;
 
 /**
  * Perform arithmetic.
@@ -52,13 +53,25 @@ public class JexlArithmetic {
     protected static final BigInteger BIGI_LONG_MIN_VALUE = BigInteger.valueOf(Long.MIN_VALUE);
     /** Whether this JexlArithmetic instance behaves in strict or lenient mode. */
     private boolean strict;
+    /** The big decimal math context. */
+    protected final MathContext mathContext;
 
     /**
      * Creates a JexlArithmetic.
      * @param lenient whether this arithmetic is lenient or strict
      */
     public JexlArithmetic(boolean lenient) {
+        this(lenient, MathContext.DECIMAL128);
+    }
+
+    /**
+     * Creates a JexlArithmetic.
+     * @param lenient whether this arithmetic is lenient or strict
+     * @param bigdContext the math context instance to use for +,-,/,*,% operations on big
decimals.
+     */
+    public JexlArithmetic(boolean lenient, MathContext bigdContext) {
         this.strict = !lenient;
+        this.mathContext = bigdContext;
     }
 
     /**
@@ -84,6 +97,14 @@ public class JexlArithmetic {
     }
 
     /**
+     * The MathContext instance used for +,-,/,*,% operations on big decimals.
+     * @return the math context
+     */
+    public MathContext getMathContext() {
+        return mathContext;
+    }
+
+    /**
      * The result of +,/,-,*,% when both operands are null.
      * @return Integer(0) if lenient
      * @throws NullPointerException if strict
@@ -308,7 +329,7 @@ public class JexlArithmetic {
             if (left instanceof BigDecimal || right instanceof BigDecimal) {
                 BigDecimal l = toBigDecimal(left);
                 BigDecimal r = toBigDecimal(right);
-                return l.add(r);
+                return l.add(r, mathContext);
             }
             
             // otherwise treat as integers
@@ -355,7 +376,7 @@ public class JexlArithmetic {
         if (left instanceof BigDecimal || right instanceof BigDecimal) {
             BigDecimal l = toBigDecimal(left);
             BigDecimal r = toBigDecimal(right);
-            BigDecimal d = l.divide(r);
+            BigDecimal d = l.divide(r, mathContext);
             return d;
         }
 
@@ -399,7 +420,7 @@ public class JexlArithmetic {
         if (left instanceof BigDecimal || right instanceof BigDecimal) {
             BigDecimal l = toBigDecimal(left);
             BigDecimal r = toBigDecimal(right);
-            BigDecimal remainder = l.remainder(r);
+            BigDecimal remainder = l.remainder(r, mathContext);
             return remainder;
         }
 
@@ -439,7 +460,7 @@ public class JexlArithmetic {
         if (left instanceof BigDecimal || right instanceof BigDecimal) {
             BigDecimal l = toBigDecimal(left);
             BigDecimal r = toBigDecimal(right);
-            return l.multiply(r);
+            return l.multiply(r, mathContext);
         }
 
         // otherwise treat as integers
@@ -478,7 +499,7 @@ public class JexlArithmetic {
         if (left instanceof BigDecimal || right instanceof BigDecimal) {
             BigDecimal l = toBigDecimal(left);
             BigDecimal r = toBigDecimal(right);
-            return l.subtract(r);
+            return l.subtract(r, mathContext);
         }
 
         // otherwise treat as integers

Modified: commons/proper/jexl/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/site/xdoc/changes.xml?rev=1066867&r1=1066866&r2=1066867&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/jexl/trunk/src/site/xdoc/changes.xml Thu Feb  3 16:57:58 2011
@@ -26,6 +26,9 @@
   </properties>
   <body>
     <release version="2.0.2" date="unreleased">
+        <action dev="henrib" type="add" issue="JEXL-106" due-to="Michal Sabol">
+            When divide two BigDecimal values in an expression it results in java.lang.ArithmeticException:
Non-terminating decimal expansion; no exact representable decimal result.
+        </action>
         <action dev="henrib" type="add" issue="JEXL-105" due-to="Cary Thompson">Array
literals are considered constant even when they are not.</action>
         <action dev="henrib" type="add" issue="JEXL-104" due-to="Andreas Haufler">NPE
in JexlArithmetic when an Array-Expression containing a null is used.</action>
         <action dev="henrib" type="add" issue="JEXL-102" due-to="sebb">Add "jexl2"
as a supported name</action>

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java?rev=1066867&r1=1066866&r2=1066867&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java Thu Feb
 3 16:57:58 2011
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.jexl2;
 
+import java.math.BigDecimal;
+import java.math.MathContext;
 import org.apache.commons.jexl2.introspection.Uberspect;
 import org.apache.commons.jexl2.internal.Introspector;
 import java.util.HashMap;
@@ -418,7 +420,6 @@ public class IssuesTest extends JexlTest
     }
 
     public void test105() throws Exception {
-
         JexlContext context = new MapContext();
         Expression selectExp = new JexlEngine().createExpression("[a.propA]");
         context.set("a", new A105("a1", "p1"));
@@ -430,4 +431,25 @@ public class IssuesTest extends JexlTest
         r = (Object[]) selectExp.evaluate(context);
         assertEquals("p2", r[0]);
     }
+
+    public void test106() throws Exception {
+        JexlContext context = new MapContext();
+        context.set("a", new BigDecimal(1));
+        context.set("b", new BigDecimal(3));
+        JexlEngine jexl = new JexlEngine();
+        try {
+            Object value = jexl.createExpression("a / b").evaluate(context);
+            assertNotNull(value);
+        } catch(JexlException xjexl) {
+            fail("should not occur");
+        }
+        JexlArithmetic arithmetic = new JexlArithmetic(false, MathContext.UNLIMITED);
+        JexlEngine jexlX = new JexlEngine(null, arithmetic, null, null);
+        try {
+            Object value = jexlX.createExpression("a / b").evaluate(context);
+            fail("should fail");
+        } catch(JexlException xjexl) {
+            //ok  to fail
+        }
+    }
 }



Mime
View raw message