KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package org.objectweb.medor.expression.lib;
25
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.medor.expression.api.ExpressionException;
28 import org.objectweb.medor.expression.api.MalformedExpressionException;
29 import org.objectweb.medor.expression.api.Operand;
30 import org.objectweb.medor.expression.api.ParameterOperand;
31 import org.objectweb.medor.expression.api.TypingException;
32 import org.objectweb.medor.expression.api.UnaryArithmeticOperator;
33 import org.objectweb.medor.expression.api.Expression;
34 import org.objectweb.medor.expression.type.ExpressionTypeHelper;
35
36 /**
37  *
38  * @author Sebastien Chassande-Barrioz
39  */

40 public abstract class BasicUnaryArithmeticOperator
41         extends BasicUnaryOperator
42         implements UnaryArithmeticOperator {
43
44     public BasicUnaryArithmeticOperator() {
45     }
46
47     public BasicUnaryArithmeticOperator(PType type) {
48         super(type);
49     }
50
51     public BasicUnaryArithmeticOperator(Expression e) {
52         super(e);
53     }
54
55     public BasicUnaryArithmeticOperator(PType type, Expression e) {
56         super(type, e);
57     }
58
59     // IMPLEMENTATION OF THE Expression INTERFACE //
60
//--------------------------------------------//
61

62     public org.objectweb.medor.expression.api.Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
63             throws ExpressionException {
64         try {
65             Operand subResult = expressions[0].evaluate(pos, o);
66             switch (subResult.getType().getTypeCode()) {
67                 case PType.TYPECODE_DOUBLE:
68                     result.setValue(evaluate(subResult.getDouble()));
69                     break;
70                 case PType.TYPECODE_FLOAT:
71                     result.setValue(evaluate(subResult.getFloat()));
72                     break;
73                 case PType.TYPECODE_BYTE:
74                     result.setValue(evaluate(subResult.getByte()));
75                     break;
76                 case PType.TYPECODE_SHORT:
77                     result.setValue(evaluate(subResult.getShort()));
78                     break;
79                 case PType.TYPECODE_INT:
80                     result.setValue(evaluate(subResult.getInt()));
81                     break;
82                 case PType.TYPECODE_LONG:
83                     result.setValue(evaluate(subResult.getLong()));
84                     break;
85                 default:
86                     throw new MalformedExpressionException(
87                             "Unauthorized expression element");
88             }
89         } catch (NullPointerException JavaDoc e) {
90             throw new IllegalStateException JavaDoc("Unevaluable Expression: Not compiled");
91         }
92         return result;
93     }
94
95     public Operand compileExpression()
96         throws ExpressionException, MalformedExpressionException {
97         if (expressions[0] != null) {
98             expressions[0].compileExpression();
99             if (ExpressionTypeHelper.isArithmeticType(expressions[0].getType())) {
100                 type = expressions[0].getType();
101                 result = new BasicVariableOperand(type);
102                 verified = true;
103             } else
104                 throw new TypingException("Attempt an aritmetic type");
105         } else
106             throw new MalformedExpressionException("null childeren value");// Operands not yet assigned
107
return result;
108     }
109 }
110
Popular Tags