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 2021/05/31 09:58:05 UTC
[commons-jexl] branch master updated: Simplify boolean expressions
This is an automated email from the ASF dual-hosted git repository.
henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git
The following commit(s) were added to refs/heads/master by this push:
new f5caa99 Simplify boolean expressions
new bb5fb90 Merge pull request #52 from csamak/booleans
f5caa99 is described below
commit f5caa99dd28b70ba35bf2838102e44b466675c1c
Author: Cameron Samak <cs...@apache.org>
AuthorDate: Sun May 30 21:17:40 2021 +0000
Simplify boolean expressions
Many uses of Boolean.TRUE and Boolean.FALSE were unnecessary.
---
.../org/apache/commons/jexl3/JexlArithmetic.java | 16 ++++++-------
.../apache/commons/jexl3/internal/Interpreter.java | 28 ++++++++++------------
.../apache/commons/jexl3/internal/Operators.java | 6 ++---
3 files changed, 24 insertions(+), 26 deletions(-)
diff --git a/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java b/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
index 56f2524..334207c 100644
--- a/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
+++ b/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
@@ -996,10 +996,10 @@ public class JexlArithmetic {
return (byte) -((Byte) val);
}
if (val instanceof Boolean) {
- return ((Boolean) val) ? Boolean.FALSE : Boolean.TRUE;
+ return !(Boolean) val;
}
if (val instanceof AtomicBoolean) {
- return ((AtomicBoolean) val).get() ? Boolean.FALSE : Boolean.TRUE;
+ return !((AtomicBoolean) val).get();
}
throw new ArithmeticException("Object negate:(" + val + ")");
}
@@ -1179,20 +1179,20 @@ public class JexlArithmetic {
public Boolean isEmpty(final Object object, final Boolean def) {
if (object instanceof Number) {
final double d = ((Number) object).doubleValue();
- return Double.isNaN(d) || d == 0.d ? Boolean.TRUE : Boolean.FALSE;
+ return Double.isNaN(d) || d == 0.d;
}
if (object instanceof CharSequence) {
- return ((CharSequence) object).length() == 0 ? Boolean.TRUE : Boolean.FALSE;
+ return ((CharSequence) object).length() == 0;
}
if (object.getClass().isArray()) {
- return Array.getLength(object) == 0 ? Boolean.TRUE : Boolean.FALSE;
+ return Array.getLength(object) == 0;
}
if (object instanceof Collection<?>) {
- return ((Collection<?>) object).isEmpty() ? Boolean.TRUE : Boolean.FALSE;
+ return ((Collection<?>) object).isEmpty();
}
// Map isn't a collection
if (object instanceof Map<?, ?>) {
- return ((Map<?, ?>) object).isEmpty() ? Boolean.TRUE : Boolean.FALSE;
+ return ((Map<?, ?>) object).isEmpty();
}
return def;
}
@@ -1287,7 +1287,7 @@ public class JexlArithmetic {
* @return !val
*/
public Object not(final Object val) {
- return toBoolean(val) ? Boolean.FALSE : Boolean.TRUE;
+ return !toBoolean(val);
}
/**
diff --git a/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java b/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
index b447db6..ab24f53 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
@@ -368,9 +368,7 @@ public class Interpreter extends InterpreterBase {
final Object right = node.jjtGetChild(1).jjtAccept(this, data);
try {
final Object result = operators.tryOverload(node, JexlOperator.EQ, left, right);
- return result != JexlEngine.TRY_FAILED
- ? result
- : arithmetic.equals(left, right) ? Boolean.TRUE : Boolean.FALSE;
+ return result != JexlEngine.TRY_FAILED ? result : arithmetic.equals(left, right);
} catch (final ArithmeticException xrt) {
throw new JexlException(node, "== error", xrt);
}
@@ -383,8 +381,8 @@ public class Interpreter extends InterpreterBase {
try {
final Object result = operators.tryOverload(node, JexlOperator.EQ, left, right);
return result != JexlEngine.TRY_FAILED
- ? arithmetic.toBoolean(result) ? Boolean.FALSE : Boolean.TRUE
- : arithmetic.equals(left, right) ? Boolean.FALSE : Boolean.TRUE;
+ ? !arithmetic.toBoolean(result)
+ : !arithmetic.equals(left, right);
} catch (final ArithmeticException xrt) {
final JexlNode xnode = findNullOperand(xrt, node, left, right);
throw new JexlException(xnode, "!= error", xrt);
@@ -399,7 +397,7 @@ public class Interpreter extends InterpreterBase {
final Object result = operators.tryOverload(node, JexlOperator.GTE, left, right);
return result != JexlEngine.TRY_FAILED
? result
- : arithmetic.greaterThanOrEqual(left, right) ? Boolean.TRUE : Boolean.FALSE;
+ : arithmetic.greaterThanOrEqual(left, right);
} catch (final ArithmeticException xrt) {
throw new JexlException(node, ">= error", xrt);
}
@@ -413,7 +411,7 @@ public class Interpreter extends InterpreterBase {
final Object result = operators.tryOverload(node, JexlOperator.GT, left, right);
return result != JexlEngine.TRY_FAILED
? result
- : arithmetic.greaterThan(left, right) ? Boolean.TRUE : Boolean.FALSE;
+ : arithmetic.greaterThan(left, right);
} catch (final ArithmeticException xrt) {
throw new JexlException(node, "> error", xrt);
}
@@ -427,7 +425,7 @@ public class Interpreter extends InterpreterBase {
final Object result = operators.tryOverload(node, JexlOperator.LTE, left, right);
return result != JexlEngine.TRY_FAILED
? result
- : arithmetic.lessThanOrEqual(left, right) ? Boolean.TRUE : Boolean.FALSE;
+ : arithmetic.lessThanOrEqual(left, right);
} catch (final ArithmeticException xrt) {
throw new JexlException(node, "<= error", xrt);
}
@@ -441,7 +439,7 @@ public class Interpreter extends InterpreterBase {
final Object result = operators.tryOverload(node, JexlOperator.LT, left, right);
return result != JexlEngine.TRY_FAILED
? result
- : arithmetic.lessThan(left, right) ? Boolean.TRUE : Boolean.FALSE;
+ : arithmetic.lessThan(left, right);
} catch (final ArithmeticException xrt) {
throw new JexlException(node, "< error", xrt);
}
@@ -451,42 +449,42 @@ public class Interpreter extends InterpreterBase {
protected Object visit(final ASTSWNode node, final Object data) {
final Object left = node.jjtGetChild(0).jjtAccept(this, data);
final Object right = node.jjtGetChild(1).jjtAccept(this, data);
- return operators.startsWith(node, "^=", left, right) ? Boolean.TRUE : Boolean.FALSE;
+ return operators.startsWith(node, "^=", left, right);
}
@Override
protected Object visit(final ASTNSWNode node, final Object data) {
final Object left = node.jjtGetChild(0).jjtAccept(this, data);
final Object right = node.jjtGetChild(1).jjtAccept(this, data);
- return operators.startsWith(node, "^!", left, right) ? Boolean.FALSE : Boolean.TRUE;
+ return !operators.startsWith(node, "^!", left, right);
}
@Override
protected Object visit(final ASTEWNode node, final Object data) {
final Object left = node.jjtGetChild(0).jjtAccept(this, data);
final Object right = node.jjtGetChild(1).jjtAccept(this, data);
- return operators.endsWith(node, "$=", left, right) ? Boolean.TRUE : Boolean.FALSE;
+ return operators.endsWith(node, "$=", left, right);
}
@Override
protected Object visit(final ASTNEWNode node, final Object data) {
final Object left = node.jjtGetChild(0).jjtAccept(this, data);
final Object right = node.jjtGetChild(1).jjtAccept(this, data);
- return operators.endsWith(node, "$!", left, right) ? Boolean.FALSE : Boolean.TRUE;
+ return !operators.endsWith(node, "$!", left, right);
}
@Override
protected Object visit(final ASTERNode node, final Object data) {
final Object left = node.jjtGetChild(0).jjtAccept(this, data);
final Object right = node.jjtGetChild(1).jjtAccept(this, data);
- return operators.contains(node, "=~", right, left) ? Boolean.TRUE : Boolean.FALSE;
+ return operators.contains(node, "=~", right, left);
}
@Override
protected Object visit(final ASTNRNode node, final Object data) {
final Object left = node.jjtGetChild(0).jjtAccept(this, data);
final Object right = node.jjtGetChild(1).jjtAccept(this, data);
- return operators.contains(node, "!~", right, left) ? Boolean.FALSE : Boolean.TRUE;
+ return !operators.contains(node, "!~", right, left);
}
@Override
diff --git a/src/main/java/org/apache/commons/jexl3/internal/Operators.java b/src/main/java/org/apache/commons/jexl3/internal/Operators.java
index 1484d49..a72dc1d 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Operators.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Operators.java
@@ -236,7 +236,7 @@ public class Operators {
throw new JexlException(node, operator + " error", e);
}
// defaults to equal
- return arithmetic.equals(left, right) ? Boolean.TRUE : Boolean.FALSE;
+ return arithmetic.equals(left, right);
} catch (final ArithmeticException xrt) {
throw new JexlException(node, operator + " error", xrt);
}
@@ -281,7 +281,7 @@ public class Operators {
throw new JexlException(node, operator + " error", e);
}
// defaults to equal
- return arithmetic.equals(left, right) ? Boolean.TRUE : Boolean.FALSE;
+ return arithmetic.equals(left, right);
} catch (final ArithmeticException xrt) {
throw new JexlException(node, operator + " error", xrt);
}
@@ -369,7 +369,7 @@ public class Operators {
}
}
}
- return result instanceof Boolean ? (Boolean) result : true;
+ return !(result instanceof Boolean) || (Boolean) result;
}
/**