Java Reference
In-Depth Information
public ScriptContext getScriptContext() {
return context;
}
public Double eval() {
// Parse the expression
if (!parsed) {
this.parse();
this.parsed = true;
}
// Extract the values for the operand
double op1Value = getOperandValue(op1Sign, op1);
double op2Value = getOperandValue(op2Sign, op2);
// Evaluate the expression
Double result = null;
switch (operation) {
case '+':
result = op1Value + op2Value;
break;
case '-':
result = op1Value - op2Value;
break;
case '*':
result = op1Value * op2Value;
break;
case '/':
result = op1Value / op2Value;
break;
default:
throw new RuntimeException(
"Invalid operation:" + operation);
}
return result;
}
private double getOperandValue(char sign, String operand) {
// Check if operand is a double
double value;
try {
value = Double.parseDouble(operand);
return sign == '-' ? -value : value;
}
catch (NumberFormatException e) {
// Ignore it. Operand is not in a format that can be
// converted to a double value.
}
Search WWH ::




Custom Search