KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > expression > lib > Sqrt


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

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 JavaDoc 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 JavaDoc 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             // Operands not yet assigned
121
throw new MalformedExpressionException("null childeren value");
122         return result;
123     }
124
125     public String JavaDoc getOperatorString() {
126         return Operator.SQRT;
127     }
128 }
129
Popular Tags