1 package com.genimen.djeneric.repository.oql.core.nodes; 2 3 import java.math.BigDecimal ; 4 import java.util.HashMap ; 5 6 import com.genimen.djeneric.repository.oql.core.DjOqlParserEngine; 7 import com.genimen.djeneric.repository.oql.core.MatchException; 8 import com.genimen.djeneric.repository.oql.core.ParseException; 9 import com.genimen.djeneric.repository.oql.core.SimpleNode; 10 11 public class OperatorNode extends SimpleNode implements ValueExpression 12 { 13 private String operator; 14 15 public OperatorNode(int i) 16 { 17 super(i); 18 } 19 20 public OperatorNode(DjOqlParserEngine p, int i) 21 { 22 super(p, i); 23 } 24 25 public String getName() 26 { 27 return toString(); 28 } 29 30 public String toString() 31 { 32 return getOperator() + " (op)"; 33 } 34 35 public void setOperator(String operator) 36 { 37 this.operator = operator; 38 } 39 40 public String getOperator() 41 { 42 return operator; 43 } 44 45 public void translate(StringBuffer result, HashMap path2AliasMapping) throws ParseException 46 { 47 appendOpenBrackets(result); 48 getChild(0).translate(result, path2AliasMapping); 49 result.append(operator); 50 getChild(1).translate(result, path2AliasMapping); 51 appendCloseBrackets(result); 52 } 53 54 public Object getValue(MatchingContext context) throws MatchException 55 { 56 ValueExpression left = (ValueExpression) getChild(0); 57 ValueExpression right = (ValueExpression) getChild(1); 58 59 if (getOperator().equals(".")) 61 { 62 Object leftVal = left.getValue(context); 63 if (!(right instanceof PropertyOrFunctionNode)) 64 { 65 throw new MatchException("Can only apply . to a right side function", beginLine, beginColumn); 66 } 67 PropertyOrFunctionNode pof = (PropertyOrFunctionNode) right; 68 try 69 { 70 return pof.evalFunction(leftVal, context); 71 } 72 catch (Exception x) 73 { 74 throw new MatchException(x.getMessage(), beginLine, beginColumn); 75 } 76 } 77 78 Object leftVal = left.getValue(context); 79 Object rightVal = right.getValue(context); 80 81 if (leftVal == null || rightVal == null) 83 { 84 return null; 85 } 86 87 if ((leftVal instanceof String ) || (rightVal instanceof String )) 89 { 90 if (getOperator().equals("+")) return leftVal.toString() + rightVal.toString(); 91 throw new MatchException("Can not apply " + getOperator() + " to strings", beginLine, beginColumn); 92 } 93 94 if ((leftVal instanceof BigDecimal ) || (rightVal instanceof BigDecimal )) 96 { 97 float l = toBigDecimal(leftVal).floatValue(); 98 float r = toBigDecimal(rightVal).floatValue(); 99 100 if (getOperator().equals("+")) return new Float (l + r); 101 if (getOperator().equals("-")) return new Float (l - r); 102 if (getOperator().equals("*")) return new Float (l * r); 103 if (getOperator().equals("/")) return new Float (l / r); 104 throw new MatchException("Can not apply " + getOperator() + " to floats", beginLine, beginColumn); 105 } 106 if ((leftVal instanceof Float ) || (rightVal instanceof Float )) 108 { 109 float l = toBigDecimal(leftVal).floatValue(); 110 float r = toBigDecimal(rightVal).floatValue(); 111 112 if (getOperator().equals("+")) return new Float (l + r); 113 if (getOperator().equals("-")) return new Float (l - r); 114 if (getOperator().equals("*")) return new Float (l * r); 115 if (getOperator().equals("/")) return new Float (l / r); 116 throw new MatchException("Can not apply " + getOperator() + " to floats", beginLine, beginColumn); 117 } 118 if ((leftVal instanceof Integer ) || (rightVal instanceof Integer )) 120 { 121 int l = toLong(leftVal).intValue(); 122 int r = toLong(rightVal).intValue(); 123 124 if (getOperator().equals("+")) return new Integer (l + r); 125 if (getOperator().equals("-")) return new Integer (l - r); 126 if (getOperator().equals("*")) return new Integer (l * r); 127 if (getOperator().equals("/")) return new Integer (l / r); 128 if (getOperator().equals("%")) return new Integer (l % r); 129 throw new MatchException("Can not apply " + getOperator() + " to integers", beginLine, beginColumn); 130 } 131 throw new MatchException("Can not apply " + classOnly(leftVal) + getOperator() + classOnly(rightVal), beginLine, 132 beginColumn); 133 } 134 135 } | Popular Tags |