1 23 24 package org.objectweb.medor.expression.lib; 25 26 import org.objectweb.jorm.type.api.PType; 27 import org.objectweb.jorm.type.api.PTypeSpace; 28 import org.objectweb.medor.expression.api.Comparator; 29 import org.objectweb.medor.expression.api.ExpressionException; 30 import org.objectweb.medor.expression.api.MalformedExpressionException; 31 import org.objectweb.medor.expression.api.Operand; 32 import org.objectweb.medor.expression.api.ParameterOperand; 33 import org.objectweb.medor.expression.api.TypingException; 34 import org.objectweb.medor.expression.api.Expression; 35 import org.objectweb.medor.expression.type.ExpressionTypeHelper; 36 37 41 public abstract class BasicComparator 42 extends BasicBinaryOperator 43 implements Comparator { 44 45 public BasicComparator() { 46 super(PTypeSpace.BOOLEAN); 47 } 48 49 public BasicComparator(Expression e0, Expression e1) { 50 super(PTypeSpace.BOOLEAN, e0, e1); 51 } 52 53 56 59 public org.objectweb.medor.expression.api.Operand 60 evaluate(ParameterOperand[] pos, Object o) 61 throws ExpressionException { 62 Operand leftResult = expressions[0].evaluate(pos, o); 63 Operand rightResult = expressions[1].evaluate(pos, o); 64 PType rtype = ExpressionTypeHelper.getResultType(expressions[0].getType(), 65 expressions[1].getType()); 66 switch (rtype.getTypeCode()) { 67 case PType.TYPECODE_BOOLEAN: 68 result.setValue(evaluate(leftResult.getBoolean(), 69 rightResult.getBoolean())); 70 break; 71 case PType.TYPECODE_BYTE: 72 result.setValue(evaluate(leftResult.getByte(), 73 rightResult.getByte())); 74 break; 75 case PType.TYPECODE_CHAR: 76 result.setValue(evaluate(leftResult.getChar(), 77 rightResult.getChar())); 78 break; 79 case PType.TYPECODE_SHORT: 80 result.setValue(evaluate(leftResult.getShort(), 81 rightResult.getShort())); 82 break; 83 case PType.TYPECODE_INT: 84 result.setValue(evaluate(leftResult.getInt(), 85 rightResult.getInt())); 86 break; 87 case PType.TYPECODE_LONG: 88 result.setValue(evaluate(leftResult.getLong(), 89 rightResult.getLong())); 90 break; 91 case PType.TYPECODE_FLOAT: 92 result.setValue(evaluate(leftResult.getFloat(), 93 rightResult.getFloat())); 94 break; 95 case PType.TYPECODE_DOUBLE: 96 result.setValue(evaluate(leftResult.getDouble(), 97 rightResult.getDouble())); 98 break; 99 case PType.TYPECODE_STRING: 100 result.setValue(evaluate(leftResult.getString(), 101 rightResult.getString())); 102 break; 103 case PType.TYPECODE_OBJBOOLEAN: 104 case PType.TYPECODE_OBJBYTE: 105 case PType.TYPECODE_OBJCHAR: 106 case PType.TYPECODE_OBJSHORT: 107 case PType.TYPECODE_OBJINT: 108 case PType.TYPECODE_OBJLONG: 109 case PType.TYPECODE_OBJFLOAT: 110 case PType.TYPECODE_OBJDOUBLE: 111 case PType.TYPECODE_BIGDECIMAL: 112 case PType.TYPECODE_BIGINTEGER: 113 result.setValue(evaluate(leftResult.getObject(), 114 rightResult.getObject())); 115 break; 116 default: 117 throw new TypingException("Unmanaged the type: (" 118 + " typeCode: " + rtype.getTypeCode() 119 + " jormName: " + rtype.getJormName() 120 + " javaName: " + rtype.getJavaName() 121 + ") this=" + this 122 ); 123 } 124 return result; 125 } 126 127 128 131 public abstract boolean evaluate(Object op1, Object op2); 132 133 public abstract boolean evaluate(int op1, int op2); 134 135 public abstract boolean evaluate(int op1, short op2); 136 137 public abstract boolean evaluate(int op1, long op2); 138 139 public abstract boolean evaluate(int op1, float op2); 140 141 public abstract boolean evaluate(int op1, double op2); 142 143 public abstract boolean evaluate(float op1, float op2); 144 145 public abstract boolean evaluate(float op1, short op2); 146 147 public abstract boolean evaluate(float op1, int op2); 148 149 public abstract boolean evaluate(float op1, long op2); 150 151 public abstract boolean evaluate(float op1, double op2); 152 153 public abstract boolean evaluate(char op1, char op2); 154 155 public abstract boolean evaluate(long op1, long op2); 156 157 public abstract boolean evaluate(long op1, float op2); 158 159 public abstract boolean evaluate(long op1, double op2); 160 161 public abstract boolean evaluate(double op1, double op2); 162 163 public abstract boolean evaluate(double op1, short op2); 164 165 public abstract boolean evaluate(double op1, int op2); 166 167 public abstract boolean evaluate(double op1, float op2); 168 169 public abstract boolean evaluate(double op1, long op2); 170 171 public abstract boolean evaluate(String op1, String op2); 172 173 public abstract boolean evaluate(boolean op1, boolean op2); 174 175 public Operand compileExpression() 176 throws ExpressionException, MalformedExpressionException { 177 if ((expressions[0] != null) && (expressions[1] != null)) { 178 expressions[0].compileExpression(); 179 expressions[1].compileExpression(); 180 if (!(expressions[0].getType() == null || expressions[1].getType() == null)) { 181 if ((ExpressionTypeHelper.isArithmeticType(expressions[0].getType()) && 182 (ExpressionTypeHelper.isArithmeticType(expressions[1].getType()))) 183 || 185 (expressions[0].getType().getTypeCode() == 186 (expressions[1].getType().getTypeCode()))) { 187 if (!(expressions[0].getType().isa(expressions[1].getType())) && 189 !(expressions[1].getType().isa(expressions[0].getType()))) { 190 throw new TypingException("incompatible types"); 191 } else { 192 result = new BasicVariableOperand(PTypeSpace.BOOLEAN); 193 verified = true; 194 } 195 } else 196 throw new TypingException("Attempt an aritmetic type on a non arithmetic type"); 198 } else 199 throw new MalformedExpressionException("Null children value in Expression"); 201 } else 202 throw new MalformedExpressionException("Null children value in Expression"); 204 return result; 205 } 206 } 207 | Popular Tags |