KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExpressionException;
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
33 import java.math.BigDecimal JavaDoc;
34 import java.math.BigInteger JavaDoc;
35 import java.util.Date JavaDoc;
36
37 /**
38  *
39  * @author Sebastien Chassande-Barrioz
40  */

41 public class BasicOperand extends BasicExpression implements Operand {
42
43     protected long longValue;
44     protected double doubleValue;
45     protected Object JavaDoc objectValue;
46
47     protected boolean isDefined = true;
48
49     public BasicOperand() {
50     }
51
52     public BasicOperand(BasicOperand bo) {
53         super(bo);
54         longValue = bo.longValue;
55         doubleValue = bo.doubleValue;
56         objectValue = bo.objectValue;
57         isDefined = bo.isDefined;
58     }
59     protected BasicOperand(PType p) {
60         super(p);
61     }
62
63     public BasicOperand(boolean p) {
64         super(PTypeSpace.BOOLEAN);
65         longValue = (p ? 1 : 0);
66         doubleValue = (p ? 1 : 0);
67     }
68
69     public BasicOperand(char p) {
70         super(PTypeSpace.CHAR);
71         longValue = p;
72         doubleValue = p;
73     }
74
75     public BasicOperand(char[] p) {
76         super(PTypeSpace.CHARARRAY);
77         objectValue = p;
78     }
79
80      public BasicOperand(byte p) {
81         super(PTypeSpace.BYTE);
82         longValue = p;
83         doubleValue = p;
84     }
85
86     public BasicOperand(byte[] p) {
87         super(PTypeSpace.BYTEARRAY);
88         objectValue = p;
89     }
90
91     public BasicOperand(short p) {
92         super(PTypeSpace.SHORT);
93         longValue = p;
94         doubleValue = p;
95     }
96
97     public BasicOperand(int p) {
98         super(PTypeSpace.INT);
99         longValue = p;
100         doubleValue = p;
101     }
102
103     public BasicOperand(long p) {
104         super(PTypeSpace.LONG);
105         longValue = p;
106         doubleValue = p;
107     }
108
109     public BasicOperand(float p) {
110         super(PTypeSpace.FLOAT);
111         doubleValue = p;
112     }
113
114     public BasicOperand(double p) {
115         super(PTypeSpace.DOUBLE);
116         doubleValue = p;
117     }
118
119     public BasicOperand(String JavaDoc p) {
120         super(PTypeSpace.STRING);
121         objectValue = p;
122     }
123
124     public BasicOperand(Date JavaDoc p) {
125         super(PTypeSpace.DATE);
126         objectValue = p;
127     }
128
129     public BasicOperand(Object JavaDoc p, PType type) {
130         super(type);
131         objectValue = p;
132         switch (type.getTypeCode()) {
133         case PType.TYPECODE_BOOLEAN:
134             longValue = ((Boolean JavaDoc) p).booleanValue() ? 1 : 0;
135             doubleValue = longValue;
136             break;
137         case PType.TYPECODE_BYTE:
138             longValue = ((Byte JavaDoc ) p).byteValue();
139             doubleValue = longValue;
140             break;
141         case PType.TYPECODE_SHORT:
142             longValue = ((Short JavaDoc) p).shortValue();
143             doubleValue = longValue;
144             break;
145         case PType.TYPECODE_INT:
146             longValue = ((Integer JavaDoc) p).intValue();
147             doubleValue = longValue;
148             break;
149         case PType.TYPECODE_LONG:
150             longValue = ((Long JavaDoc) p).intValue();
151             doubleValue = longValue;
152             break;
153         case PType.TYPECODE_CHAR:
154             longValue = ((Character JavaDoc) p).charValue();
155             doubleValue = longValue;
156             break;
157         case PType.TYPECODE_FLOAT:
158             doubleValue = ((Float JavaDoc) p).floatValue();
159             break;
160         case PType.TYPECODE_DOUBLE:
161             doubleValue = ((Double JavaDoc) p).doubleValue();
162             break;
163         }
164     }
165
166     public Object JavaDoc clone(Object JavaDoc clone, java.util.Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
167         clone = super.clone(clone, obj2clone);
168         BasicOperand bo = (BasicOperand) clone;
169         bo.longValue = longValue;
170         bo.doubleValue = doubleValue;
171         bo.objectValue = objectValue;
172         bo.type = type;
173         bo.isDefined = isDefined;
174         return clone;
175     }
176
177     /**
178      * It retrieves the value of the operand as a String.
179      */

180     public String JavaDoc getValueAsString() {
181         switch (type.getTypeCode()) {
182         case PType.TYPECODE_BOOLEAN:
183             return "" + (longValue == 1);
184         case PType.TYPECODE_BYTE:
185         case PType.TYPECODE_SHORT:
186         case PType.TYPECODE_INT:
187         case PType.TYPECODE_LONG:
188             return "" + longValue;
189         case PType.TYPECODE_CHAR:
190             return new Character JavaDoc((char) longValue).toString();
191         case PType.TYPECODE_FLOAT:
192         case PType.TYPECODE_DOUBLE:
193             return "" + doubleValue;
194         default:
195             return (objectValue==null ? null : objectValue.toString());
196         }
197     }
198
199     public String JavaDoc toString() {
200         return getValueAsString();
201     }
202
203     // IMPLEMENTATION OF THE Expression INTERFACE //
204
//--------------------------------------------//
205

206     public org.objectweb.medor.expression.api.Operand
207             evaluate(ParameterOperand[] pos, Object JavaDoc o) throws ExpressionException {
208         return this;
209     }
210
211     public PType getType() {
212         return type;
213     }
214
215     // IMPLEMENTATION OF THE Operand INTERFACE //
216
//-----------------------------------------//
217

218     public boolean isDefined() {
219         return isDefined;
220     }
221
222     public void setIsDefined(boolean isdefined) {
223         isDefined = isdefined;
224     }
225
226     public boolean getBoolean() throws TypingException {
227         if (type.getTypeCode() == PType.TYPECODE_BOOLEAN) {
228             return longValue == 1;
229         } if (type.getTypeCode() == PType.TYPECODE_OBJBOOLEAN
230             && objectValue instanceof Boolean JavaDoc) {
231             return ((Boolean JavaDoc) objectValue).booleanValue();
232         } else
233             throw new TypingException("Can not get a boolean value");
234     }
235
236     public int getInt() throws TypingException {
237         if (type.getTypeCode() == PType.TYPECODE_INT) {
238             return (int) longValue;
239         } else if (type.getTypeCode() == PType.TYPECODE_OBJINT
240             && objectValue instanceof Integer JavaDoc) {
241             return ((Integer JavaDoc) objectValue).intValue();
242         } else
243             throw new TypingException("Can not get an integer value " + type.getTypeCode());
244     }
245
246     public byte getByte() throws TypingException {
247         if (type.getTypeCode() == PType.TYPECODE_BYTE) {
248             return (byte) longValue;
249         } else if (type.getTypeCode() == PType.TYPECODE_OBJBYTE
250             && objectValue instanceof Byte JavaDoc) {
251             return ((Byte JavaDoc) objectValue).byteValue();
252         } else
253             throw new TypingException("Can not get a byte value");
254     }
255
256     public byte[] getByteArray() throws TypingException {
257          if (type.getTypeCode() == PType.TYPECODE_BYTEARRAY) {
258              return (byte[]) objectValue;
259          } else
260              throw new TypingException("Can not get a byteArray value");
261      }
262
263      public short getShort() throws TypingException {
264         if (type.getTypeCode() == PType.TYPECODE_SHORT) {
265             return (short) longValue;
266         } else if (type.getTypeCode() == PType.TYPECODE_OBJSHORT
267             && (objectValue == null || objectValue instanceof Short JavaDoc)) {
268             return ((Short JavaDoc) objectValue).shortValue();
269         } else
270             throw new TypingException("Can not get a short value");
271     }
272
273     public long getLong() throws TypingException {
274         if (type.getTypeCode() == PType.TYPECODE_LONG) {
275             return longValue;
276         } else if (type.getTypeCode() == PType.TYPECODE_OBJLONG
277             && (objectValue == null || objectValue instanceof Long JavaDoc)) {
278             return ((Long JavaDoc) objectValue).longValue();
279         } else
280             throw new TypingException("Can not get a long value");
281     }
282
283     public float getFloat() throws TypingException {
284         if (type.getTypeCode() == PType.TYPECODE_FLOAT) {
285             return (float) doubleValue;
286         } else if (type.getTypeCode() == PType.TYPECODE_OBJFLOAT
287             && (objectValue == null || objectValue instanceof Float JavaDoc)) {
288             return ((Float JavaDoc) objectValue).floatValue();
289         } else
290             throw new TypingException("Can not get a float value");
291     }
292
293     public double getDouble() throws TypingException {
294         if (type.getTypeCode() == PType.TYPECODE_DOUBLE) {
295             return doubleValue;
296         } else if (type.getTypeCode() == PType.TYPECODE_OBJDOUBLE
297             && (objectValue == null || objectValue instanceof Double JavaDoc)) {
298             return ((Double JavaDoc) objectValue).doubleValue();
299         } else
300             throw new TypingException("Can not get a double value " + type.getJavaName());
301     }
302
303     public char getChar() throws TypingException {
304         if (type.getTypeCode() == PType.TYPECODE_CHAR) {
305             return (char) longValue;
306         } else if (type.getTypeCode() == PType.TYPECODE_OBJCHAR
307             && (objectValue == null || objectValue instanceof Character JavaDoc)) {
308             return ((Character JavaDoc) objectValue).charValue();
309         } else
310             throw new TypingException("Can not get a char value");
311     }
312
313     public char[] getCharArray() throws TypingException {
314         if (type.getTypeCode() == PType.TYPECODE_CHARARRAY) {
315             return (char[]) objectValue;
316         }
317         else
318             throw new TypingException("Can not get a char value");
319     }
320
321       public String JavaDoc getString() throws TypingException {
322         if (type.getTypeCode() == PType.TYPECODE_STRING
323             && (objectValue == null || objectValue instanceof String JavaDoc)) {
324             return (String JavaDoc) objectValue;
325         } else
326             throw new TypingException("Can not get a string value: " + objectValue);
327     }
328
329     public BigDecimal JavaDoc getBigDecimal() throws TypingException {
330         if (type.getTypeCode() == PType.TYPECODE_BIGDECIMAL
331             && (objectValue == null || objectValue instanceof BigDecimal JavaDoc)) {
332             return (BigDecimal JavaDoc) objectValue;
333         } else
334             throw new TypingException("Can not get a BigDecimal value: " + objectValue);
335     }
336
337     public BigInteger JavaDoc getBigInteger() throws TypingException {
338         if (type.getTypeCode() == PType.TYPECODE_BIGINTEGER
339             && (objectValue == null || objectValue instanceof BigInteger JavaDoc)) {
340             return (BigInteger JavaDoc) objectValue;
341         } else
342             throw new TypingException("Can not get a BigInteger value: " + objectValue);
343     }
344
345     public Date JavaDoc getDate() throws TypingException {
346         // We will use the cast method. It will be changed soon
347
if (objectValue == null || objectValue instanceof Date JavaDoc) {
348             return (Date JavaDoc) objectValue;
349         } else
350             throw new TypingException("Can not get a Date value: "
351                 + (objectValue == null ? "null" : objectValue.getClass().getName()));
352     }
353
354     public Object JavaDoc getObject() {
355         if (type == null)
356             return objectValue;
357         switch (type.getTypeCode()) {
358         case PType.TYPECODE_BOOLEAN:
359             return new Boolean JavaDoc(longValue == 1);
360         case PType.TYPECODE_BYTE:
361             return new Byte JavaDoc((byte) longValue);
362         case PType.TYPECODE_CHAR:
363             return new Character JavaDoc((char) longValue);
364         case PType.TYPECODE_DOUBLE:
365             return new Double JavaDoc((double) longValue);
366         case PType.TYPECODE_FLOAT:
367             return new Float JavaDoc((float) longValue);
368         case PType.TYPECODE_INT:
369             return new Integer JavaDoc((int) longValue);
370         case PType.TYPECODE_LONG:
371             return new Long JavaDoc(longValue);
372         case PType.TYPECODE_SHORT:
373             return new Short JavaDoc((short) longValue);
374         default:
375             return objectValue;
376         }
377     }
378
379     public Operand compileExpression() {
380         return this;
381     }
382
383 }
384
Popular Tags