use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class MathFunction method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
Object param = expr.getArguments().get(0);
Object paramValue = null;
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
paramValue = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
paramValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof InvokeExpression) {
InvokeExpression invokeExpr = (InvokeExpression) param;
paramValue = eval.getValueForInvokeExpression(invokeExpr);
} else if (param instanceof Literal) {
paramValue = ((Literal) param).getLiteral();
} else if (param instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) param;
paramValue = dyExpr.evaluate(eval);
} else {
throw new NucleusException(method + "(num) where num is instanceof " + param.getClass().getName() + " not supported");
}
Object result = null;
if (paramValue instanceof Double) {
result = new Double(evaluateMathFunction(((Double) paramValue).doubleValue()));
} else if (paramValue instanceof Float) {
result = new Float(evaluateMathFunction(((Float) paramValue).floatValue()));
} else if (paramValue instanceof BigDecimal) {
result = new BigDecimal(evaluateMathFunction(((BigDecimal) paramValue).doubleValue()));
} else if (paramValue instanceof Integer) {
result = new Double(evaluateMathFunction(((Integer) paramValue).doubleValue()));
} else if (paramValue instanceof Long) {
result = new Double(evaluateMathFunction(((Long) paramValue).doubleValue()));
} else {
throw new NucleusException("Not possible to use " + getFunctionName() + " on value of type " + paramValue.getClass().getName());
}
return result;
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class StringCharAtMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return Integer.valueOf(-1);
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
// Evaluate the argument
Object arg1Obj = null;
Object param = expr.getArguments().get(0);
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
arg1Obj = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
arg1Obj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
arg1Obj = ((Literal) param).getLiteral();
} else if (param instanceof InvokeExpression) {
arg1Obj = eval.getValueForInvokeExpression((InvokeExpression) param);
} else if (param instanceof DyadicExpression) {
arg1Obj = ((DyadicExpression) param).evaluate(eval);
} else {
throw new NucleusException(method + "(param1) where param is instanceof " + param.getClass().getName() + " not supported");
}
if (!(arg1Obj instanceof Number)) {
throw new NucleusException(method + "(param1]) : param1 must be numeric");
}
int arg1 = ((Number) arg1Obj).intValue();
return Integer.valueOf(((String) invokedValue).indexOf(arg1));
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class StringConcatMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return null;
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
Expression arg0Expr = expr.getArguments().get(0);
Object arg0Val = null;
if (arg0Expr instanceof PrimaryExpression) {
arg0Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg0Expr);
} else if (arg0Expr instanceof ParameterExpression) {
arg0Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg0Expr);
} else if (arg0Expr instanceof Literal) {
arg0Val = ((Literal) arg0Expr).getLiteral();
} else if (arg0Expr instanceof DyadicExpression) {
arg0Val = ((DyadicExpression) arg0Expr).evaluate(eval);
}
if (!(arg0Val instanceof String)) {
throw new NucleusException(method + "(param1) : param1 must be String");
}
return ((String) invokedValue).concat((String) arg0Val);
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class StringEqualsIgnoreCaseMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return Boolean.FALSE;
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
String arg1 = null;
Object argObj = null;
Object param = expr.getArguments().get(0);
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
argObj = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
argObj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
argObj = ((Literal) param).getLiteral();
} else if (param instanceof InvokeExpression) {
argObj = eval.getValueForInvokeExpression((InvokeExpression) param);
} else {
throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
}
arg1 = QueryUtils.getStringValue(argObj);
return ((String) invokedValue).equalsIgnoreCase(arg1) ? Boolean.TRUE : Boolean.FALSE;
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class StringStartsWithMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return Boolean.FALSE;
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
String arg1 = null;
Object argObj = null;
Object param = expr.getArguments().get(0);
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
argObj = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
argObj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
argObj = ((Literal) param).getLiteral();
} else if (param instanceof InvokeExpression) {
argObj = eval.getValueForInvokeExpression((InvokeExpression) param);
} else {
throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
}
arg1 = QueryUtils.getStringValue(argObj);
Boolean result = null;
if (expr.getArguments().size() == 2) {
Literal param2 = (Literal) expr.getArguments().get(1);
int arg2 = -1;
if (param2.getLiteral() instanceof Number) {
arg2 = ((Number) param2.getLiteral()).intValue();
}
result = ((String) invokedValue).startsWith(arg1, arg2) ? Boolean.TRUE : Boolean.FALSE;
} else {
result = ((String) invokedValue).startsWith(arg1) ? Boolean.TRUE : Boolean.FALSE;
}
return result;
}
Aggregations