1 23 24 package org.objectweb.medor.expression.lib; 25 26 import org.objectweb.jorm.type.api.PType; 27 import org.objectweb.medor.expression.api.BinaryArithmeticOperator; 28 import org.objectweb.medor.expression.api.ExpressionException; 29 import org.objectweb.medor.expression.api.MalformedExpressionException; 30 import org.objectweb.medor.expression.api.Operand; 31 import org.objectweb.medor.expression.api.ParameterOperand; 32 import org.objectweb.medor.expression.api.TypingException; 33 import org.objectweb.medor.expression.api.Expression; 34 import org.objectweb.medor.expression.type.ExpressionTypeHelper; 35 36 import java.math.BigDecimal ; 37 import java.math.BigInteger ; 38 import java.util.Date ; 39 40 44 public abstract class BasicBinaryArithmeticOperator 45 extends BasicBinaryOperator 46 implements BinaryArithmeticOperator { 47 48 public BasicBinaryArithmeticOperator() { 49 super(); 50 } 51 52 public BasicBinaryArithmeticOperator(Expression e0, Expression e1) { 53 super(e0, e1); 54 } 55 56 public BasicBinaryArithmeticOperator(PType type) { 57 super(type); 58 } 59 60 public BasicBinaryArithmeticOperator(PType type, Expression e0, Expression e1) { 61 super(type, e0, e1); 62 } 63 66 public org.objectweb.medor.expression.api.Operand evaluate(ParameterOperand[] pos, Object o) 67 throws ExpressionException { 68 try { 69 ; 70 expressions[1].evaluate(pos, o); 71 switch (type.getTypeCode()) { 72 case PType.TYPECODE_BYTE: 73 case PType.TYPECODE_OBJBYTE: 74 result.setValue(evaluate(expressions[0].evaluate(pos, o).getByte(), 75 expressions[1].evaluate(pos, o).getByte())); 76 break; 77 case PType.TYPECODE_CHAR: 78 case PType.TYPECODE_OBJCHAR: 79 result.setValue(evaluate(expressions[0].evaluate(pos, o).getChar(), 80 expressions[1].evaluate(pos, o).getChar())); 81 break; 82 case PType.TYPECODE_SHORT: 83 case PType.TYPECODE_OBJSHORT: 84 result.setValue(evaluate(expressions[0].evaluate(pos, o).getShort(), 85 expressions[1].evaluate(pos, o).getShort())); 86 break; 87 case PType.TYPECODE_INT: 88 case PType.TYPECODE_OBJINT: 89 result.setValue(evaluate(expressions[0].evaluate(pos, o).getInt(), 90 expressions[1].evaluate(pos, o).getInt())); 91 break; 92 case PType.TYPECODE_LONG: 93 case PType.TYPECODE_OBJLONG: 94 result.setValue(evaluate(expressions[0].evaluate(pos, o).getLong(), 95 expressions[1].evaluate(pos, o).getLong())); 96 break; 97 case PType.TYPECODE_FLOAT: 98 case PType.TYPECODE_OBJFLOAT: 99 result.setValue(evaluate(expressions[0].evaluate(pos, o).getFloat(), 100 expressions[1].evaluate(pos, o).getFloat())); 101 break; 102 case PType.TYPECODE_DOUBLE: 103 case PType.TYPECODE_OBJDOUBLE: 104 result.setValue(evaluate(expressions[0].evaluate(pos, o).getDouble(), 105 expressions[1].evaluate(pos, o).getDouble())); 106 break; 107 case PType.TYPECODE_STRING: 108 result.setValue(evaluate(expressions[0].evaluate(pos, o).getString(), 109 expressions[1].evaluate(pos, o).getString())); 110 break; 111 case PType.TYPECODE_BIGDECIMAL: 112 result.setValue(evaluate(expressions[0].evaluate(pos, o).getBigDecimal(), 113 expressions[1].evaluate(pos, o).getBigDecimal())); 114 break; 115 case PType.TYPECODE_BIGINTEGER: 116 result.setValue(evaluate(expressions[0].evaluate(pos, o).getBigInteger(), 117 expressions[1].evaluate(pos, o).getBigInteger())); 118 break; 119 default: 120 throw new TypingException("Incompatible type"); 122 } 123 } catch (NullPointerException e) { 124 throw new IllegalStateException ("Unevaluable Expression: Not compiled"); 126 } 127 return result; 128 } 129 130 public abstract int evaluate(int op1, int op2); 133 134 public abstract int evaluate(int op1, short op2); 135 136 public abstract long evaluate(int op1, long op2); 137 138 public abstract float evaluate(int op1, float op2); 139 140 public abstract double evaluate(int op1, double op2); 141 142 public abstract float evaluate(float op1, float op2); 143 144 public abstract float evaluate(float op1, short op2); 145 146 public abstract float evaluate(float op1, int op2); 147 148 public abstract float evaluate(float op1, long op2); 149 150 public abstract double evaluate(float op1, double op2); 151 152 public abstract int evaluate(char op1, char op2) 153 throws TypingException; 154 155 public abstract String evaluate(char op1, String op2) 156 throws TypingException; 157 158 public abstract long evaluate(long op1, long op2); 159 160 public abstract long evaluate(long op1, short op2); 161 162 public abstract long evaluate(long op1, int op2); 163 164 public abstract float evaluate(long op1, float op2); 165 166 public abstract double evaluate(long op1, double op2); 167 168 public abstract double evaluate(double op1, double op2); 169 170 public abstract double evaluate(double op1, short op2); 171 172 public abstract double evaluate(double op1, int op2); 173 174 public abstract double evaluate(double op1, float op2); 175 176 public abstract double evaluate(double op1, long op2); 177 178 public abstract BigDecimal evaluate(BigDecimal op1, BigDecimal op2); 179 180 public abstract BigInteger evaluate(BigInteger op1, BigInteger op2); 181 182 public abstract String evaluate(String op1, String op2) 183 throws TypingException; 184 185 public abstract String evaluate(String op1, char op2) 186 throws TypingException; 187 188 public abstract Date evaluate(Date op1, Date op2) throws TypingException; 189 190 public Operand compileExpression() 191 throws ExpressionException, MalformedExpressionException { 192 if ((expressions[0] != null) && (expressions[1] != null)) { 193 expressions[0].compileExpression(); 194 expressions[1].compileExpression(); 195 if (!(expressions[0].getType() == null || expressions[1].getType() == null)) { 196 if ((ExpressionTypeHelper.isArithmeticType(expressions[0].getType())) 197 && (ExpressionTypeHelper.isArithmeticType(expressions[1].getType()))) { 198 if (expressions[0].getType().isa(expressions[1].getType())) { 199 type = expressions[1].getType(); 200 } else { 201 type = expressions[0].getType(); 202 } 203 result = new BasicVariableOperand(type); 204 verified = true; 205 } else 206 throw new TypingException("Attempt an aritmetic type " + expressions[0].getType().getJavaName() + " " + expressions[1].getType().getJavaName()); } else 208 throw new MalformedExpressionException("null children value"); 209 } else 210 throw new MalformedExpressionException("null children value"); 212 return result; 213 } 214 } 215 | Popular Tags |