1 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.Expression; 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 34 39 public class Round extends BasicOperator { 40 41 int round = 0; 42 43 public Round() { 44 } 45 46 public Round(Expression e) { 47 this(e,0); 48 } 49 50 public Round(Expression e, int r) { 51 super(new Expression[]{e}); 52 setRound(r); 53 } 54 55 public int getRound() { 56 return round; 57 } 58 59 public void setRound(int r) { 60 round = r; 61 } 62 63 public Operand compileExpression() throws ExpressionException, 64 MalformedExpressionException { 65 type = expressions[0].compileExpression().getType(); 66 switch(type.getTypeCode()) { 67 case PType.TYPECODE_FLOAT: 68 case PType.TYPECODE_DOUBLE: 69 if (round <= 0) { 70 type = PTypeSpace.LONG; 71 } 72 } 73 result = new BasicVariableOperand(type); 74 return result; 75 } 76 public Operand evaluate(ParameterOperand[] values, Object o) 77 throws ExpressionException { 78 Operand sub = expressions[0].evaluate(values, o); 79 switch(sub.getType().getTypeCode()) { 80 case PType.TYPECODE_FLOAT: 81 result.setValue(Math.round(sub.getFloat())); 82 break; 83 case PType.TYPECODE_OBJFLOAT: 84 Float f = (Float ) sub.getObject(); 85 if (f == null) { 86 result.setValue((Object ) null); 87 } else { 88 result.setValue((long) Math.round(f.floatValue())); 89 } 90 break; 91 case PType.TYPECODE_DOUBLE: 92 result.setValue(Math.round(sub.getDouble())); 93 break; 94 case PType.TYPECODE_OBJDOUBLE: 95 Double d = (Double ) sub.getObject(); 96 if (d == null) { 97 result.setValue((Object ) null); 98 } else { 99 result.setValue((long) Math.round(d.doubleValue())); 100 } 101 break; 102 default: 103 return sub; 104 } 105 return result; 106 } 107 public String getOperatorString() { 108 return "round"; 109 } 110 } 111 | Popular Tags |