KickJava   Java API By Example, From Geeks To Geeks.

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


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.jorm.type.api.PTypeSpace;
28 import org.objectweb.medor.expression.api.Comparator;
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 import org.objectweb.medor.expression.api.TypingException;
34 import org.objectweb.medor.expression.api.Expression;
35 import org.objectweb.medor.expression.type.ExpressionTypeHelper;
36
37 /**
38  *
39  * @author Sebastien Chassande-Barrioz
40  */

41 public abstract class BasicComparator
42         extends BasicBinaryOperator
43         implements Comparator {
44
45     public BasicComparator() {
46         super(PTypeSpace.BOOLEAN);
47     }
48
49     public BasicComparator(Expression e0, Expression e1) {
50         super(PTypeSpace.BOOLEAN, e0, e1);
51     }
52     
53     // IMPLEMENTATION OF THE Expression INTERFACE //
54
//--------------------------------------------//
55

56     /**
57      * We supose that the comparisons can be performed between all types.
58      */

59     public org.objectweb.medor.expression.api.Operand
60             evaluate(ParameterOperand[] pos, Object JavaDoc o)
61             throws ExpressionException {
62         Operand leftResult = expressions[0].evaluate(pos, o);
63         Operand rightResult = expressions[1].evaluate(pos, o);
64         PType rtype = ExpressionTypeHelper.getResultType(expressions[0].getType(),
65                 expressions[1].getType());
66         switch (rtype.getTypeCode()) {
67         case PType.TYPECODE_BOOLEAN:
68             result.setValue(evaluate(leftResult.getBoolean(),
69                 rightResult.getBoolean()));
70             break;
71         case PType.TYPECODE_BYTE:
72             result.setValue(evaluate(leftResult.getByte(),
73                 rightResult.getByte()));
74             break;
75         case PType.TYPECODE_CHAR:
76             result.setValue(evaluate(leftResult.getChar(),
77                 rightResult.getChar()));
78             break;
79         case PType.TYPECODE_SHORT:
80             result.setValue(evaluate(leftResult.getShort(),
81                 rightResult.getShort()));
82             break;
83         case PType.TYPECODE_INT:
84             result.setValue(evaluate(leftResult.getInt(),
85                 rightResult.getInt()));
86             break;
87         case PType.TYPECODE_LONG:
88             result.setValue(evaluate(leftResult.getLong(),
89                 rightResult.getLong()));
90             break;
91         case PType.TYPECODE_FLOAT:
92             result.setValue(evaluate(leftResult.getFloat(),
93                 rightResult.getFloat()));
94             break;
95         case PType.TYPECODE_DOUBLE:
96             result.setValue(evaluate(leftResult.getDouble(),
97                 rightResult.getDouble()));
98             break;
99         case PType.TYPECODE_STRING:
100             result.setValue(evaluate(leftResult.getString(),
101                 rightResult.getString()));
102             break;
103         case PType.TYPECODE_OBJBOOLEAN:
104         case PType.TYPECODE_OBJBYTE:
105         case PType.TYPECODE_OBJCHAR:
106         case PType.TYPECODE_OBJSHORT:
107         case PType.TYPECODE_OBJINT:
108         case PType.TYPECODE_OBJLONG:
109         case PType.TYPECODE_OBJFLOAT:
110         case PType.TYPECODE_OBJDOUBLE:
111         case PType.TYPECODE_BIGDECIMAL:
112         case PType.TYPECODE_BIGINTEGER:
113             result.setValue(evaluate(leftResult.getObject(),
114                 rightResult.getObject()));
115             break;
116         default:
117             throw new TypingException("Unmanaged the type: ("
118                 + " typeCode: " + rtype.getTypeCode()
119                 + " jormName: " + rtype.getJormName()
120                 + " javaName: " + rtype.getJavaName()
121                 + ") this=" + this
122             );
123         }
124         return result;
125     }
126
127
128 // IMPLEMENTATION OF THE Comparator INTERFACE //
129
//--------------------------------------------//
130

131     public abstract boolean evaluate(Object JavaDoc op1, Object JavaDoc op2);
132
133     public abstract boolean evaluate(int op1, int op2);
134
135     public abstract boolean evaluate(int op1, short op2);
136
137     public abstract boolean evaluate(int op1, long op2);
138
139     public abstract boolean evaluate(int op1, float op2);
140
141     public abstract boolean evaluate(int op1, double op2);
142
143     public abstract boolean evaluate(float op1, float op2);
144
145     public abstract boolean evaluate(float op1, short op2);
146
147     public abstract boolean evaluate(float op1, int op2);
148
149     public abstract boolean evaluate(float op1, long op2);
150
151     public abstract boolean evaluate(float op1, double op2);
152
153     public abstract boolean evaluate(char op1, char op2);
154
155     public abstract boolean evaluate(long op1, long op2);
156
157     public abstract boolean evaluate(long op1, float op2);
158
159     public abstract boolean evaluate(long op1, double op2);
160
161     public abstract boolean evaluate(double op1, double op2);
162
163     public abstract boolean evaluate(double op1, short op2);
164
165     public abstract boolean evaluate(double op1, int op2);
166
167     public abstract boolean evaluate(double op1, float op2);
168
169     public abstract boolean evaluate(double op1, long op2);
170
171     public abstract boolean evaluate(String JavaDoc op1, String JavaDoc op2);
172
173     public abstract boolean evaluate(boolean op1, boolean op2);
174
175     public Operand compileExpression()
176             throws ExpressionException, MalformedExpressionException {
177         if ((expressions[0] != null) && (expressions[1] != null)) {
178             expressions[0].compileExpression();
179             expressions[1].compileExpression();
180             if (!(expressions[0].getType() == null || expressions[1].getType() == null)) {
181                 if ((ExpressionTypeHelper.isArithmeticType(expressions[0].getType()) &&
182                         (ExpressionTypeHelper.isArithmeticType(expressions[1].getType())))
183                         //Equal operations OK between arithmetic types
184
||
185                         (expressions[0].getType().getTypeCode() ==
186                         (expressions[1].getType().getTypeCode()))) {
187                     //between operands which have the same types
188
if (!(expressions[0].getType().isa(expressions[1].getType())) &&
189                             !(expressions[1].getType().isa(expressions[0].getType()))) {
190                         throw new TypingException("incompatible types");
191                     } else {
192                         result = new BasicVariableOperand(PTypeSpace.BOOLEAN);
193                         verified = true;
194                     }
195                 } else
196                 // it is not a numeric type
197
throw new TypingException("Attempt an aritmetic type on a non arithmetic type");
198             } else
199             //syntax error
200
throw new MalformedExpressionException("Null children value in Expression");
201         } else
202         // Operands not yet assigned
203
throw new MalformedExpressionException("Null children value in Expression");
204         return result;
205     }
206 }
207
Popular Tags