KickJava   Java API By Example, From Geeks To Geeks.

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


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.BinaryArithmeticOperator;
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.ParameterOperand;
32 import org.objectweb.medor.expression.api.TypingException;
33 import org.objectweb.medor.expression.api.Expression;
34 import org.objectweb.medor.expression.type.ExpressionTypeHelper;
35
36 import java.math.BigDecimal JavaDoc;
37 import java.math.BigInteger JavaDoc;
38 import java.util.Date JavaDoc;
39
40 /**
41  *
42  * @author Sebastien Chassande-Barrioz
43  */

44 public abstract class BasicBinaryArithmeticOperator
45         extends BasicBinaryOperator
46         implements BinaryArithmeticOperator {
47     
48     public BasicBinaryArithmeticOperator() {
49         super();
50     }
51
52     public BasicBinaryArithmeticOperator(Expression e0, Expression e1) {
53         super(e0, e1);
54     }
55
56     public BasicBinaryArithmeticOperator(PType type) {
57         super(type);
58     }
59
60     public BasicBinaryArithmeticOperator(PType type, Expression e0, Expression e1) {
61         super(type, e0, e1);
62     }
63     // IMPLEMENTATION OF THE Expression INTERFACE //
64
//--------------------------------------------//
65

66     public org.objectweb.medor.expression.api.Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
67             throws ExpressionException {
68         try {
69             ;
70             expressions[1].evaluate(pos, o);
71             switch (type.getTypeCode()) {
72                 case PType.TYPECODE_BYTE:
73                 case PType.TYPECODE_OBJBYTE:
74                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getByte(),
75                                     expressions[1].evaluate(pos, o).getByte()));
76                     break;
77                 case PType.TYPECODE_CHAR:
78                 case PType.TYPECODE_OBJCHAR:
79                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getChar(),
80                                     expressions[1].evaluate(pos, o).getChar()));
81                     break;
82                 case PType.TYPECODE_SHORT:
83                 case PType.TYPECODE_OBJSHORT:
84                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getShort(),
85                                     expressions[1].evaluate(pos, o).getShort()));
86                     break;
87                 case PType.TYPECODE_INT:
88                 case PType.TYPECODE_OBJINT:
89                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getInt(),
90                                     expressions[1].evaluate(pos, o).getInt()));
91                     break;
92                 case PType.TYPECODE_LONG:
93                 case PType.TYPECODE_OBJLONG:
94                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getLong(),
95                                     expressions[1].evaluate(pos, o).getLong()));
96                     break;
97                 case PType.TYPECODE_FLOAT:
98                 case PType.TYPECODE_OBJFLOAT:
99                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getFloat(),
100                                     expressions[1].evaluate(pos, o).getFloat()));
101                     break;
102                 case PType.TYPECODE_DOUBLE:
103                 case PType.TYPECODE_OBJDOUBLE:
104                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getDouble(),
105                                     expressions[1].evaluate(pos, o).getDouble()));
106                     break;
107                 case PType.TYPECODE_STRING:
108                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getString(),
109                                     expressions[1].evaluate(pos, o).getString()));
110                     break;
111                 case PType.TYPECODE_BIGDECIMAL:
112                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getBigDecimal(),
113                                     expressions[1].evaluate(pos, o).getBigDecimal()));
114                     break;
115                 case PType.TYPECODE_BIGINTEGER:
116                     result.setValue(evaluate(expressions[0].evaluate(pos, o).getBigInteger(),
117                                     expressions[1].evaluate(pos, o).getBigInteger()));
118                     break;
119                 default:
120                     // This case don't occurre if we compile at first
121
throw new TypingException("Incompatible type");
122             }
123         } catch (NullPointerException JavaDoc e) {
124             // result == null or missing children objects
125
throw new IllegalStateException JavaDoc("Unevaluable Expression: Not compiled");
126         }
127         return result;
128     }
129
130     // IMPLEMENTATION OF THE BinaryArithmeticOperator INTERFACE //
131
//----------------------------------------------------------//
132
public abstract int evaluate(int op1, int op2);
133
134     public abstract int evaluate(int op1, short op2);
135
136     public abstract long evaluate(int op1, long op2);
137
138     public abstract float evaluate(int op1, float op2);
139
140     public abstract double evaluate(int op1, double op2);
141
142     public abstract float evaluate(float op1, float op2);
143
144     public abstract float evaluate(float op1, short op2);
145
146     public abstract float evaluate(float op1, int op2);
147
148     public abstract float evaluate(float op1, long op2);
149
150     public abstract double evaluate(float op1, double op2);
151
152     public abstract int evaluate(char op1, char op2)
153             throws TypingException;
154
155     public abstract String JavaDoc evaluate(char op1, String JavaDoc op2)
156             throws TypingException;
157
158     public abstract long evaluate(long op1, long op2);
159
160     public abstract long evaluate(long op1, short op2);
161
162     public abstract long evaluate(long op1, int op2);
163
164     public abstract float evaluate(long op1, float op2);
165
166     public abstract double evaluate(long op1, double op2);
167
168     public abstract double evaluate(double op1, double op2);
169
170     public abstract double evaluate(double op1, short op2);
171
172     public abstract double evaluate(double op1, int op2);
173
174     public abstract double evaluate(double op1, float op2);
175
176     public abstract double evaluate(double op1, long op2);
177
178     public abstract BigDecimal JavaDoc evaluate(BigDecimal JavaDoc op1, BigDecimal JavaDoc op2);
179
180     public abstract BigInteger JavaDoc evaluate(BigInteger JavaDoc op1, BigInteger JavaDoc op2);
181
182     public abstract String JavaDoc evaluate(String JavaDoc op1, String JavaDoc op2)
183             throws TypingException;
184
185     public abstract String JavaDoc evaluate(String JavaDoc op1, char op2)
186             throws TypingException;
187
188     public abstract Date JavaDoc evaluate(Date JavaDoc op1, Date JavaDoc op2) throws TypingException;
189
190     public Operand compileExpression()
191         throws ExpressionException, MalformedExpressionException {
192         if ((expressions[0] != null) && (expressions[1] != null)) {
193             expressions[0].compileExpression();
194             expressions[1].compileExpression();
195             if (!(expressions[0].getType() == null || expressions[1].getType() == null)) {
196                 if ((ExpressionTypeHelper.isArithmeticType(expressions[0].getType()))
197                     && (ExpressionTypeHelper.isArithmeticType(expressions[1].getType()))) {
198                     if (expressions[0].getType().isa(expressions[1].getType())) {
199                         type = expressions[1].getType();
200                     } else {
201                         type = expressions[0].getType();
202                     }
203                     result = new BasicVariableOperand(type);
204                     verified = true;
205                 } else
206                     throw new TypingException("Attempt an aritmetic type " + expressions[0].getType().getJavaName() + " " + expressions[1].getType().getJavaName()); // it is not a numeric type
207
} else
208                 throw new MalformedExpressionException("null children value");
209         } else
210         // Operands not yet assigned
211
throw new MalformedExpressionException("null children value");
212         return result;
213     }
214 }
215
Popular Tags