You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2016/05/13 09:00:54 UTC

svn commit: r1743627 - in /commons/proper/jexl/trunk: RELEASE-NOTES.txt src/main/java/org/apache/commons/jexl3/JexlArithmetic.java src/site/xdoc/changes.xml src/test/java/org/apache/commons/jexl3/ArithmeticTest.java

Author: henrib
Date: Fri May 13 09:00:54 2016
New Revision: 1743627

URL: http://svn.apache.org/viewvc?rev=1743627&view=rev
Log:
JEXL-195: added AtomicBoolean handling where relevant in JexlArithmetic.

Modified:
    commons/proper/jexl/trunk/RELEASE-NOTES.txt
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
    commons/proper/jexl/trunk/src/site/xdoc/changes.xml
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java

Modified: commons/proper/jexl/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/RELEASE-NOTES.txt?rev=1743627&r1=1743626&r2=1743627&view=diff
==============================================================================
--- commons/proper/jexl/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/jexl/trunk/RELEASE-NOTES.txt Fri May 13 09:00:54 2016
@@ -28,6 +28,7 @@ Version 3.0.1 is a micro release to fix
 Bugs Fixed in 3.0.1:
 ====================
 
+* JEXL-195:     Support for AtomicBoolean in logical expressions
 * JEXL-193:     InterruptedException is swallowed in function call in silent and non-strict mode
 * JEXL-192:     Invalid return type when expected result is null
 * JEXL-191:     Jexl3 unsolvable property exception when using enum

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java?rev=1743627&r1=1743626&r2=1743627&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java Fri May 13 09:00:54 2016
@@ -25,6 +25,7 @@ import java.math.BigInteger;
 import java.math.MathContext;
 import java.util.Collection;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -787,6 +788,8 @@ public class JexlArithmetic {
             return (byte) -((Byte) val);
         } else if (val instanceof Boolean) {
             return ((Boolean) val) ? Boolean.FALSE : Boolean.TRUE;
+        } else if (val instanceof AtomicBoolean) {
+            return ((AtomicBoolean) val).get() ? Boolean.FALSE : Boolean.TRUE;
         }
         throw new ArithmeticException("Object negation:(" + val + ")");
     }
@@ -1152,6 +1155,8 @@ public class JexlArithmetic {
         } else if (val instanceof Number) {
             double number = toDouble(val);
             return !Double.isNaN(number) && number != 0.d;
+        } else if (val instanceof AtomicBoolean) {
+            return ((AtomicBoolean) val).get();
         } else if (val instanceof String) {
             String strval = val.toString();
             return strval.length() > 0 && !"false".equals(strval);
@@ -1190,6 +1195,8 @@ public class JexlArithmetic {
             return Integer.parseInt((String) val);
         } else if (val instanceof Boolean) {
             return ((Boolean) val) ? 1 : 0;
+        } else if (val instanceof AtomicBoolean) {
+            return ((AtomicBoolean) val).get() ? 1 : 0;
         } else if (val instanceof Character) {
             return ((Character) val);
         }
@@ -1228,6 +1235,8 @@ public class JexlArithmetic {
             }
         } else if (val instanceof Boolean) {
             return ((Boolean) val) ? 1L : 0L;
+        } else if (val instanceof AtomicBoolean) {
+            return ((AtomicBoolean) val).get() ? 1L : 0L;
         } else if (val instanceof Character) {
             return ((Character) val);
         }
@@ -1264,6 +1273,8 @@ public class JexlArithmetic {
             return BigInteger.valueOf(((Number) val).longValue());
         } else if (val instanceof Boolean) {
             return BigInteger.valueOf(((Boolean) val) ? 1L : 0L);
+        } else if (val instanceof AtomicBoolean) {
+            return BigInteger.valueOf(((AtomicBoolean) val).get() ? 1L : 0L);
         } else if (val instanceof String) {
             String string = (String) val;
             if ("".equals(string)) {
@@ -1305,6 +1316,8 @@ public class JexlArithmetic {
             return roundBigDecimal(new BigDecimal(val.toString(), getMathContext()));
         } else if (val instanceof Boolean) {
             return BigDecimal.valueOf(((Boolean) val) ? 1. : 0.);
+        } else if (val instanceof AtomicBoolean) {
+            return BigDecimal.valueOf(((AtomicBoolean) val).get() ? 1L : 0L);
         } else if (val instanceof String) {
             String string = (String) val;
             if ("".equals(string)) {
@@ -1340,6 +1353,8 @@ public class JexlArithmetic {
             return Double.parseDouble(String.valueOf(val));
         } else if (val instanceof Boolean) {
             return ((Boolean) val) ? 1. : 0.;
+        } else if (val instanceof AtomicBoolean) {
+            return ((AtomicBoolean) val).get() ? 1. : 0.;
         } else if (val instanceof String) {
             String string = (String) val;
             if ("".equals(string)) {

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=1743627&r1=1743626&r2=1743627&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/jexl/trunk/src/site/xdoc/changes.xml Fri May 13 09:00:54 2016
@@ -26,6 +26,9 @@
     </properties>
     <body>
         <release version="3.0.1" date="unreleased">
+            <action dev="henrib" type="fix" issue="JEXL-195" due-to="Dmitri Blinov">
+                Support for AtomicBoolean in logical expressions
+            </action>
             <action dev="henrib" type="fix" issue="JEXL-193" due-to="Dmitri Blinov">
                 InterruptedException is swallowed in function call in silent and non-strict mode
             </action>

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java?rev=1743627&r1=1743626&r2=1743627&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java Fri May 13 09:00:54 2016
@@ -25,6 +25,7 @@ import java.util.Map;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.util.concurrent.atomic.AtomicBoolean;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import org.junit.Assert;
@@ -1310,5 +1311,74 @@ public class ArithmeticTest extends Jexl
         Assert.assertEquals(BigDecimal.valueOf(0.), ja.toBigDecimal(false));
     }
 
-
+    @Test
+    public void testAtomicBoolean() throws Exception {
+        // in a condition
+        JexlScript e = JEXL.createScript("if (x) 1 else 2;", "x");
+        JexlContext jc = new MapContext();
+        AtomicBoolean ab = new AtomicBoolean(false);
+        Object o;
+        o = e.execute(jc, ab);
+        Assert.assertEquals("Result is not 2", new Integer(2), o);
+        ab.set(true);
+        o = e.execute(jc, ab);
+        Assert.assertEquals("Result is not 1", new Integer(1), o);
+        // in a binary logical op
+        e = JEXL.createScript("x && y", "x", "y");
+        ab.set(true);
+        o = e.execute(jc, ab, Boolean.FALSE);
+        Assert.assertFalse((Boolean) o);
+        ab.set(true);
+        o = e.execute(jc, ab, Boolean.TRUE);
+        Assert.assertTrue((Boolean) o);
+        ab.set(false);
+        o = e.execute(jc, ab, Boolean.FALSE);
+        Assert.assertFalse((Boolean) o);
+        ab.set(false);
+        o = e.execute(jc, ab, Boolean.FALSE);
+        Assert.assertFalse((Boolean) o);
+        // in arithmetic op
+        e = JEXL.createScript("x + y", "x", "y");
+        ab.set(true);
+        o = e.execute(jc, ab, 10);
+        Assert.assertEquals(11, o);
+        o = e.execute(jc, 10, ab);
+        Assert.assertEquals(11, o);
+        o = e.execute(jc, ab, 10.d);
+        Assert.assertEquals(11.d, (Double) o, EPSILON);
+        o = e.execute(jc, 10.d, ab);
+        Assert.assertEquals(11.d, (Double) o, EPSILON);
+
+        BigInteger bi10 = BigInteger.TEN;
+        ab.set(false);
+        o = e.execute(jc, ab, bi10);
+        Assert.assertEquals(bi10, o);
+        o = e.execute(jc, bi10, ab);
+        Assert.assertEquals(bi10, o);
+
+        BigDecimal bd10 = BigDecimal.TEN;
+        ab.set(false);
+        o = e.execute(jc, ab, bd10);
+        Assert.assertEquals(bd10, o);
+        o = e.execute(jc, bd10, ab);
+        Assert.assertEquals(bd10, o);
+
+        // in a (the) monadic op
+        e = JEXL.createScript("!x", "x");
+        ab.set(true);
+        o = e.execute(jc, ab);
+        Assert.assertFalse((Boolean) o);
+        ab.set(false);
+        o = e.execute(jc, ab);
+        Assert.assertTrue((Boolean) o);
+
+        // in a (the) monadic op
+        e = JEXL.createScript("-x", "x");
+        ab.set(true);
+        o = e.execute(jc, ab);
+        Assert.assertFalse((Boolean) o);
+        ab.set(false);
+        o = e.execute(jc, ab);
+        Assert.assertTrue((Boolean) o);
+    }
 }