use of org.apache.commons.jexl2.JexlArithmetic in project commons-jexl by apache.
the class Operators method startsWith.
/**
* The 'startsWith' operator implementation.
* @param node the node
* @param operator the calling operator, $= or $!
* @param left the left operand
* @param right the right operand
* @return true if left starts with right, false otherwise
*/
protected boolean startsWith(final JexlNode node, final String operator, final Object left, final Object right) {
final JexlArithmetic arithmetic = interpreter.arithmetic;
final JexlUberspect uberspect = interpreter.uberspect;
try {
// try operator overload
final Object result = tryOverload(node, JexlOperator.STARTSWITH, left, right);
if (result instanceof Boolean) {
return (Boolean) result;
}
// use arithmetic / pattern matching ?
final Boolean matched = arithmetic.startsWith(left, right);
if (matched != null) {
return matched;
}
// try a startsWith method (duck type)
try {
final Object[] argv = { right };
JexlMethod vm = uberspect.getMethod(left, "startsWith", argv);
if (returnsBoolean(vm)) {
return (Boolean) vm.invoke(left, argv);
}
if (arithmetic.narrowArguments(argv)) {
vm = uberspect.getMethod(left, "startsWith", argv);
if (returnsBoolean(vm)) {
return (Boolean) vm.invoke(left, argv);
}
}
} catch (final Exception e) {
throw new JexlException(node, operator + " error", e);
}
// defaults to equal
return arithmetic.equals(left, right);
} catch (final ArithmeticException xrt) {
throw new JexlException(node, operator + " error", xrt);
}
}
use of org.apache.commons.jexl2.JexlArithmetic in project commons-jexl by apache.
the class Asserter method assertExpression.
/**
* Performs an assertion that the value of the given JEXL expression
* evaluates to the given expected value.
*
* @param expression is the JEXL expression to evaluate
* @param expected is the expected value of the expression
* @throws Exception if the expression could not be evaluationed or an assertion
* fails
*/
public void assertExpression(final String expression, final Object expected) throws Exception {
final JexlScript exp = engine.createScript(expression);
final Object value = exp.execute(context);
if (expected instanceof BigDecimal) {
final JexlArithmetic jexla = engine.getArithmetic();
Assert.assertEquals("expression: " + expression, 0, ((BigDecimal) expected).compareTo(jexla.toBigDecimal(value)));
}
if (expected != null && value != null) {
if (expected.getClass().isArray() && value.getClass().isArray()) {
final int esz = Array.getLength(expected);
final int vsz = Array.getLength(value);
final String report = "expression: " + expression;
Assert.assertEquals(report + ", array size", esz, vsz);
for (int i = 0; i < vsz; ++i) {
Assert.assertEquals(report + ", value@[]" + i, Array.get(expected, i), Array.get(value, i));
}
} else {
Assert.assertEquals("expression: " + expression + ", " + expected.getClass().getSimpleName() + " ?= " + value.getClass().getSimpleName(), expected, value);
}
} else {
Assert.assertEquals("expression: " + expression, expected, value);
}
}
use of org.apache.commons.jexl2.JexlArithmetic in project datawave by NationalSecurityAgency.
the class EventErrorSummary method matches.
/**
* Determine whether this event error summary matches a set of criteria
*
* @param jobName
* @param dataType
* @param uid
* @param specifiedUUIDs
* @param errorType
* @param dateRange
* @param jexlQuery
* @return true if matches, false otherwise
*/
public boolean matches(String jobName, String dataType, String uid, Set<String> specifiedUUIDs, String errorType, Date[] dateRange, String jexlQuery, int maxProcessCount) {
boolean matches = true;
if (maxProcessCount > 0 && processedCount > maxProcessCount) {
matches = false;
} else if (jobName != null && !jobName.equals(this.jobName)) {
matches = false;
} else if (dataType != null && !dataType.equals(this.datatype)) {
matches = false;
} else if (uid != null && !uid.equals(this.uid)) {
matches = false;
} else if ((!specifiedUUIDs.isEmpty()) && !uuidMatchFound(specifiedUUIDs, uuids)) {
matches = false;
} else if (errorType != null && !matchesError(errorType)) {
matches = false;
} else if (dateRange != null) {
if (dateRange[0] != null && errorDate.before(dateRange[0])) {
matches = false;
} else if (dateRange[1] != null && errorDate.after(dateRange[1])) {
matches = false;
}
}
if (matches && jexlQuery != null) {
// Get a JexlEngine initialized with the correct JexlArithmetic for this Document
JexlEngine engine = new JexlEngine();
// Evaluate the JexlContext against the Script
Script script = engine.createScript(jexlQuery);
Object o = script.execute(this);
// Jexl might return us a null depending on the AST
if (o != null && Boolean.class.isAssignableFrom(o.getClass())) {
matches = ((Boolean) o);
} else if (o != null && Collection.class.isAssignableFrom(o.getClass())) {
// if the function returns a collection of matches, return true/false
// based on the number of matches
matches = (!((Collection<?>) o).isEmpty());
} else {
matches = false;
}
}
return matches;
}
Aggregations