1 23 package org.objectweb.medor.expression.lib; 24 25 import org.objectweb.jorm.type.api.PType; 26 import org.objectweb.jorm.type.api.PTypeSpace; 27 import org.objectweb.medor.expression.api.Expression; 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.Operator; 32 import org.objectweb.medor.expression.api.ParameterOperand; 33 import org.objectweb.medor.expression.api.TypingException; 34 import org.objectweb.medor.expression.api.UnaryOperator; 35 import org.objectweb.medor.expression.type.ExpressionTypeHelper; 36 37 public class Sqrt extends BasicUnaryOperator implements UnaryOperator { 38 39 public Sqrt() { 40 } 41 42 public Sqrt(Expression e) { 43 super(e); 44 } 45 46 public org.objectweb.medor.expression.api.Operand evaluate(ParameterOperand[] pos, Object o) 47 throws ExpressionException { 48 try { 49 Operand subResult = expressions[0].evaluate(pos, o); 50 switch (subResult.getType().getTypeCode()) { 51 case PType.TYPECODE_DOUBLE: 52 case PType.TYPECODE_OBJDOUBLE: 53 result.setValue(evaluate(subResult.getDouble())); 54 break; 55 case PType.TYPECODE_FLOAT: 56 case PType.TYPECODE_OBJFLOAT: 57 result.setValue(evaluate(subResult.getFloat())); 58 break; 59 case PType.TYPECODE_BYTE: 60 case PType.TYPECODE_OBJBYTE: 61 result.setValue(evaluate(subResult.getByte())); 62 break; 63 case PType.TYPECODE_SHORT: 64 case PType.TYPECODE_OBJSHORT: 65 result.setValue(evaluate(subResult.getShort())); 66 break; 67 case PType.TYPECODE_INT: 68 case PType.TYPECODE_OBJINT: 69 result.setValue(evaluate(subResult.getInt())); 70 break; 71 case PType.TYPECODE_LONG: 72 case PType.TYPECODE_OBJLONG: 73 result.setValue(evaluate(subResult.getLong())); 74 break; 75 default: 76 throw new MalformedExpressionException( 77 "Unauthorized expression element"); 78 } 79 } catch (NullPointerException e) { 80 throw new ExpressionException("Unevaluable Expression: Not compiled"); 81 } 82 return result; 83 } 84 85 public double evaluate(int op) { 86 return Math.sqrt(op); 87 } 88 89 public double evaluate(short op) { 90 return Math.abs(op); 91 } 92 93 public double evaluate(long op) { 94 return Math.abs(op); 95 } 96 97 public double evaluate(float op) { 98 return Math.abs(op); 99 } 100 101 public double evaluate(byte op) { 102 return Math.abs(op); 103 } 104 105 public double evaluate(double op) { 106 return Math.abs(op); 107 } 108 109 public Operand compileExpression() 110 throws ExpressionException, MalformedExpressionException { 111 if (expressions[0] != null) { 112 expressions[0].compileExpression(); 113 if (ExpressionTypeHelper.isArithmeticType(expressions[0].getType())) { 114 type = PTypeSpace.DOUBLE; 115 result = new BasicVariableOperand(type); 116 verified = true; 117 } else 118 throw new TypingException("Attempt an aritmetic type"); 119 } else 120 throw new MalformedExpressionException("null childeren value"); 122 return result; 123 } 124 125 public String getOperatorString() { 126 return Operator.SQRT; 127 } 128 } 129 | Popular Tags |