KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 package org.objectweb.medor.expression.lib;
19
20 import org.objectweb.medor.expression.api.Operand;
21 import org.objectweb.medor.expression.api.ParameterOperand;
22 import org.objectweb.medor.expression.api.ExpressionException;
23 import org.objectweb.medor.expression.api.MalformedExpressionException;
24 import org.objectweb.medor.expression.api.Expression;
25 import org.objectweb.jorm.type.api.PType;
26
27 /**
28  * This unary operator permit to change the type of an expression to another.
29  * It is a kind of cast operation.
30  *
31  * @author S.Chassande-Barrioz
32  */

33 public class TypeConverter extends BasicUnaryOperator {
34
35     /**
36      * Builds a TypeConverter.
37      *
38      * @param newType is the new PType of the expression. (cannot be null).
39      */

40     public TypeConverter(PType newType) throws MalformedExpressionException {
41         super();
42         if (newType == null) {
43             throw new MalformedExpressionException("Null PType not supported");
44         }
45         type = newType;
46     }
47
48     /**
49      * Builds a TypeConverter.
50      *
51      * @param operand is the sub expression which the type must be converted.
52      * @param newType is the new PType of the expression. (cannot be null).
53      */

54     public TypeConverter(Expression operand,
55                          PType newType) throws MalformedExpressionException {
56         this(newType);
57         setExpression(0, operand);
58     }
59
60     public String JavaDoc getOperatorString() {
61         return "cast(" + type.getJavaName() + ")";
62     }
63
64     public Operand evaluate(ParameterOperand[] values, Object JavaDoc o)
65         throws ExpressionException {
66         if (expressions[0] == null) {
67             throw new ExpressionException("No inner operand specified");
68         }
69         Operand subOperand = expressions[0].evaluate(values, o);
70         PType subType = expressions[0].getType();
71
72         switch(type.getTypeCode()) {
73         case PType.TYPECODE_BOOLEAN:
74         case PType.TYPECODE_OBJBOOLEAN:
75             {
76                 boolean res;
77                 switch (subType.getTypeCode()) {
78                 case PType.TYPECODE_BOOLEAN:
79                 case PType.TYPECODE_OBJBOOLEAN:
80                     res = subOperand.getBoolean();
81                     break;
82                 case PType.TYPECODE_BYTE:
83                 case PType.TYPECODE_OBJBYTE:
84                     res = subOperand.getByte() > 0;
85                     break;
86                 case PType.TYPECODE_SHORT:
87                 case PType.TYPECODE_OBJSHORT:
88                     res = subOperand.getShort() > 0;
89                     break;
90                 case PType.TYPECODE_INT:
91                 case PType.TYPECODE_OBJINT:
92                     res = subOperand.getInt() > 0;
93                     break;
94                 case PType.TYPECODE_LONG:
95                 case PType.TYPECODE_OBJLONG:
96                     res = subOperand.getLong() > 0;
97                     break;
98                 case PType.TYPECODE_FLOAT:
99                 case PType.TYPECODE_OBJFLOAT:
100                     res = subOperand.getFloat() > 0;
101                     break;
102                 case PType.TYPECODE_DOUBLE:
103                 case PType.TYPECODE_OBJDOUBLE:
104                     res = subOperand.getDouble() > 0;
105                     break;
106                 case PType.TYPECODE_STRING:
107                     res = Boolean.valueOf(subOperand.getString()).booleanValue();
108                     break;
109                 default:
110                     throw new ExpressionException("Impossible to convert "
111                         + subType.getJavaName() + " to " + type.getJavaName());
112                 }
113                 if (type.getTypeCode() == PType.TYPECODE_BOOLEAN) {
114                     result.setValue(res);
115                 } else {
116                     result.setValue(res ? Boolean.TRUE : Boolean.FALSE);
117                 }
118             }
119
120         case PType.TYPECODE_CHAR:
121         case PType.TYPECODE_OBJCHAR:
122             {
123                 char res;
124                 switch (subType.getTypeCode()) {
125                 case PType.TYPECODE_CHAR:
126                 case PType.TYPECODE_OBJCHAR:
127                     res = subOperand.getChar();
128                     break;
129                 case PType.TYPECODE_BYTE:
130                 case PType.TYPECODE_OBJBYTE:
131                     res = (char) subOperand.getByte();
132                     break;
133                 case PType.TYPECODE_SHORT:
134                 case PType.TYPECODE_OBJSHORT:
135                     res = (char) subOperand.getShort();
136                     break;
137                 case PType.TYPECODE_INT:
138                 case PType.TYPECODE_OBJINT:
139                     res = (char) subOperand.getInt();
140                     break;
141                 case PType.TYPECODE_LONG:
142                 case PType.TYPECODE_OBJLONG:
143                     res = (char) subOperand.getLong();
144                     break;
145                 case PType.TYPECODE_FLOAT:
146                 case PType.TYPECODE_OBJFLOAT:
147                     res = (char) subOperand.getFloat();
148                     break;
149                 case PType.TYPECODE_DOUBLE:
150                 case PType.TYPECODE_OBJDOUBLE:
151                     res = (char) subOperand.getDouble();
152                     break;
153                 case PType.TYPECODE_STRING:
154                     {
155                         String JavaDoc str = subOperand.getString();
156                         if (str == null) {
157                             res = (char) 0;
158                         } else {
159                             res = str.charAt(0);
160                         }
161                         break;
162                     }
163                 default :
164                     throw new ExpressionException("Impossible to convert "
165                         + subType.getJavaName() + " to " + type.getJavaName());
166                 }
167                 if (type.getTypeCode() == PType.TYPECODE_CHAR) {
168                     result.setValue(res);
169                 } else {
170                     result.setValue(new Character JavaDoc(res));
171                 }
172             }
173
174         case PType.TYPECODE_BYTE:
175         case PType.TYPECODE_OBJBYTE:
176             {
177                 byte res;
178                 switch (subType.getTypeCode()) {
179                 case PType.TYPECODE_BOOLEAN:
180                 case PType.TYPECODE_OBJBOOLEAN:
181                     res = (byte) (subOperand.getBoolean() ? 1 : 0);
182                     break;
183                 case PType.TYPECODE_BYTE:
184                 case PType.TYPECODE_OBJBYTE:
185                     res = subOperand.getByte();
186                     break;
187                 case PType.TYPECODE_SHORT:
188                 case PType.TYPECODE_OBJSHORT:
189                     res = (byte) subOperand.getShort();
190                     break;
191                 case PType.TYPECODE_INT:
192                 case PType.TYPECODE_OBJINT:
193                     res = (byte) subOperand.getInt();
194                     break;
195                 case PType.TYPECODE_LONG:
196                 case PType.TYPECODE_OBJLONG:
197                     res = (byte) subOperand.getLong();
198                     break;
199                 case PType.TYPECODE_FLOAT:
200                 case PType.TYPECODE_OBJFLOAT:
201                     res = (byte) subOperand.getFloat();
202                     break;
203                 case PType.TYPECODE_DOUBLE:
204                 case PType.TYPECODE_OBJDOUBLE:
205                     res = (byte) subOperand.getDouble();
206                     break;
207                 case PType.TYPECODE_STRING:
208                     res = Byte.valueOf(subOperand.getString()).byteValue();
209                     break;
210                 default:
211                     throw new ExpressionException("Impossible to convert "
212                         + subType.getJavaName() + " to " + type.getJavaName());
213                 }
214                 if (type.getTypeCode() == PType.TYPECODE_BYTE) {
215                     result.setValue(res);
216                 } else {
217                     result.setValue(new Byte JavaDoc(res));
218                 }
219             }
220
221         case PType.TYPECODE_SHORT:
222         case PType.TYPECODE_OBJSHORT:
223             {
224                 short res;
225                 switch (subType.getTypeCode()) {
226                 case PType.TYPECODE_BOOLEAN:
227                 case PType.TYPECODE_OBJBOOLEAN:
228                     res = (short) (subOperand.getBoolean() ? 1 : 0);
229                     break;
230                 case PType.TYPECODE_BYTE:
231                 case PType.TYPECODE_OBJBYTE:
232                     res = subOperand.getByte();
233                     break;
234                 case PType.TYPECODE_SHORT:
235                 case PType.TYPECODE_OBJSHORT:
236                     res = subOperand.getShort();
237                     break;
238                 case PType.TYPECODE_INT:
239                 case PType.TYPECODE_OBJINT:
240                     res = (short) subOperand.getInt();
241                     break;
242                 case PType.TYPECODE_LONG:
243                 case PType.TYPECODE_OBJLONG:
244                     res = (short) subOperand.getLong();
245                     break;
246                 case PType.TYPECODE_FLOAT:
247                 case PType.TYPECODE_OBJFLOAT:
248                     res = (short) subOperand.getFloat();
249                     break;
250                 case PType.TYPECODE_DOUBLE:
251                 case PType.TYPECODE_OBJDOUBLE:
252                     res = (short) subOperand.getDouble();
253                     break;
254                 case PType.TYPECODE_STRING:
255                     res = Short.valueOf(subOperand.getString()).shortValue();
256                     break;
257                 default:
258                     throw new ExpressionException("Impossible to convert "
259                         + subType.getJavaName() + " to " + type.getJavaName());
260                 }
261                 if (type.getTypeCode() == PType.TYPECODE_SHORT) {
262                     result.setValue(res);
263                 } else {
264                     result.setValue(new Short JavaDoc(res));
265                 }
266             }
267
268         case PType.TYPECODE_INT:
269         case PType.TYPECODE_OBJINT:
270             {
271                 int res;
272                 switch (subType.getTypeCode()) {
273                 case PType.TYPECODE_BOOLEAN:
274                 case PType.TYPECODE_OBJBOOLEAN:
275                     res = (subOperand.getBoolean() ? 1 : 0);
276                     break;
277                 case PType.TYPECODE_BYTE:
278                 case PType.TYPECODE_OBJBYTE:
279                     res = subOperand.getByte();
280                     break;
281                 case PType.TYPECODE_SHORT:
282                 case PType.TYPECODE_OBJSHORT:
283                     res = subOperand.getShort();
284                     break;
285                 case PType.TYPECODE_INT:
286                 case PType.TYPECODE_OBJINT:
287                     res = subOperand.getInt();
288                     break;
289                 case PType.TYPECODE_LONG:
290                 case PType.TYPECODE_OBJLONG:
291                     res = (int) subOperand.getLong();
292                     break;
293                 case PType.TYPECODE_FLOAT:
294                 case PType.TYPECODE_OBJFLOAT:
295                     res = (int) subOperand.getFloat();
296                     break;
297                 case PType.TYPECODE_DOUBLE:
298                 case PType.TYPECODE_OBJDOUBLE:
299                     res = (int) subOperand.getDouble();
300                     break;
301                 case PType.TYPECODE_STRING:
302                     res = Integer.valueOf(subOperand.getString()).intValue();
303                     break;
304                 default:
305                     throw new ExpressionException("Impossible to convert "
306                         + subType.getJavaName() + " to " + type.getJavaName());
307                 }
308                 if (type.getTypeCode() == PType.TYPECODE_INT) {
309                     result.setValue(res);
310                 } else {
311                     result.setValue(new Integer JavaDoc(res));
312                 }
313             }
314
315         case PType.TYPECODE_LONG:
316         case PType.TYPECODE_OBJLONG:
317             {
318                 long res;
319                 switch (subType.getTypeCode()) {
320                 case PType.TYPECODE_BOOLEAN:
321                 case PType.TYPECODE_OBJBOOLEAN:
322                     res = (subOperand.getBoolean() ? 1 : 0);
323                     break;
324                 case PType.TYPECODE_BYTE:
325                 case PType.TYPECODE_OBJBYTE:
326                     res = subOperand.getByte();
327                     break;
328                 case PType.TYPECODE_SHORT:
329                 case PType.TYPECODE_OBJSHORT:
330                     res = subOperand.getShort();
331                     break;
332                 case PType.TYPECODE_INT:
333                 case PType.TYPECODE_OBJINT:
334                     res = subOperand.getInt();
335                     break;
336                 case PType.TYPECODE_LONG:
337                 case PType.TYPECODE_OBJLONG:
338                     res = subOperand.getLong();
339                     break;
340                 case PType.TYPECODE_FLOAT:
341                 case PType.TYPECODE_OBJFLOAT:
342                     res = (long) subOperand.getFloat();
343                     break;
344                 case PType.TYPECODE_DOUBLE:
345                 case PType.TYPECODE_OBJDOUBLE:
346                     res = (long) subOperand.getDouble();
347                     break;
348                 case PType.TYPECODE_STRING:
349                     res = Long.valueOf(subOperand.getString()).longValue();
350                     break;
351                 default:
352                     throw new ExpressionException("Impossible to convert "
353                         + subType.getJavaName() + " to " + type.getJavaName());
354                 }
355                 if (type.getTypeCode() == PType.TYPECODE_LONG) {
356                     result.setValue(res);
357                 } else {
358                     result.setValue(new Long JavaDoc(res));
359                 }
360             }
361
362         case PType.TYPECODE_FLOAT:
363         case PType.TYPECODE_OBJFLOAT:
364             {
365                 float res;
366                 switch (subType.getTypeCode()) {
367                 case PType.TYPECODE_BOOLEAN:
368                 case PType.TYPECODE_OBJBOOLEAN:
369                     res = (subOperand.getBoolean() ? 1 : 0);
370                     break;
371                 case PType.TYPECODE_BYTE:
372                 case PType.TYPECODE_OBJBYTE:
373                     res = subOperand.getByte();
374                     break;
375                 case PType.TYPECODE_SHORT:
376                 case PType.TYPECODE_OBJSHORT:
377                     res = subOperand.getShort();
378                     break;
379                 case PType.TYPECODE_INT:
380                 case PType.TYPECODE_OBJINT:
381                     res = subOperand.getInt();
382                     break;
383                 case PType.TYPECODE_LONG:
384                 case PType.TYPECODE_OBJLONG:
385                     res = subOperand.getLong();
386                     break;
387                 case PType.TYPECODE_FLOAT:
388                 case PType.TYPECODE_OBJFLOAT:
389                     res = subOperand.getFloat();
390                     break;
391                 case PType.TYPECODE_DOUBLE:
392                 case PType.TYPECODE_OBJDOUBLE:
393                     res = (float) subOperand.getDouble();
394                     break;
395                 case PType.TYPECODE_STRING:
396                     res = Float.valueOf(subOperand.getString()).floatValue();
397                     break;
398                 default:
399                     throw new ExpressionException("Impossible to convert "
400                         + subType.getJavaName() + " to " + type.getJavaName());
401                 }
402                 if (type.getTypeCode() == PType.TYPECODE_LONG) {
403                     result.setValue(res);
404                 } else {
405                     result.setValue(new Float JavaDoc(res));
406                 }
407             }
408
409         case PType.TYPECODE_DOUBLE:
410         case PType.TYPECODE_OBJDOUBLE:
411             {
412                 double res;
413                 switch (subType.getTypeCode()) {
414                 case PType.TYPECODE_BOOLEAN:
415                 case PType.TYPECODE_OBJBOOLEAN:
416                     res = (subOperand.getBoolean() ? 1 : 0);
417                     break;
418                 case PType.TYPECODE_BYTE:
419                 case PType.TYPECODE_OBJBYTE:
420                     res = subOperand.getByte();
421                     break;
422                 case PType.TYPECODE_SHORT:
423                 case PType.TYPECODE_OBJSHORT:
424                     res = subOperand.getShort();
425                     break;
426                 case PType.TYPECODE_INT:
427                 case PType.TYPECODE_OBJINT:
428                     res = subOperand.getInt();
429                     break;
430                 case PType.TYPECODE_LONG:
431                 case PType.TYPECODE_OBJLONG:
432                     res = subOperand.getLong();
433                     break;
434                 case PType.TYPECODE_FLOAT:
435                 case PType.TYPECODE_OBJFLOAT:
436                     res = subOperand.getFloat();
437                     break;
438                 case PType.TYPECODE_DOUBLE:
439                 case PType.TYPECODE_OBJDOUBLE:
440                     res = subOperand.getDouble();
441                     break;
442                 case PType.TYPECODE_STRING:
443                     res = Double.valueOf(subOperand.getString()).doubleValue();
444                     break;
445                 default:
446                     throw new ExpressionException("Impossible to convert "
447                         + subType.getJavaName() + " to " + type.getJavaName());
448                 }
449                 if (type.getTypeCode() == PType.TYPECODE_LONG) {
450                     result.setValue(res);
451                 } else {
452                     result.setValue(new Float JavaDoc(res));
453                 }
454             }
455             break;
456
457         case PType.TYPECODE_STRING:
458             {
459                 String JavaDoc res;
460                 switch (subType.getTypeCode()) {
461                 case PType.TYPECODE_BOOLEAN:
462                 case PType.TYPECODE_OBJBOOLEAN:
463                     res = "" + subOperand.getBoolean();
464                     break;
465                 case PType.TYPECODE_BYTE:
466                 case PType.TYPECODE_OBJBYTE:
467                     res = "" + subOperand.getByte();
468                     break;
469                 case PType.TYPECODE_SHORT:
470                 case PType.TYPECODE_OBJSHORT:
471                     res = "" + subOperand.getShort();
472                     break;
473                 case PType.TYPECODE_INT:
474                 case PType.TYPECODE_OBJINT:
475                     res = "" + subOperand.getInt();
476                     break;
477                 case PType.TYPECODE_LONG:
478                 case PType.TYPECODE_OBJLONG:
479                     res = "" + subOperand.getLong();
480                     break;
481                 case PType.TYPECODE_FLOAT:
482                 case PType.TYPECODE_OBJFLOAT:
483                     res = "" + subOperand.getFloat();
484                     break;
485                 case PType.TYPECODE_DOUBLE:
486                 case PType.TYPECODE_OBJDOUBLE:
487                     res = "" + subOperand.getDouble();
488                     break;
489                 case PType.TYPECODE_STRING:
490                     res = subOperand.getString();
491                     break;
492                 default:
493                     throw new ExpressionException("Impossible to convert "
494                         + subType.getJavaName() + " to " + type.getJavaName());
495                 }
496                 result.setValue(res);
497             }
498             break;
499
500         //Unsupported type
501
case PType.TYPECODE_SERIALIZED:
502         case PType.TYPECODE_DATE:
503         case PType.TYPECODE_REFERENCE:
504         case PType.TYPECODE_BIGDECIMAL:
505         case PType.TYPECODE_BIGINTEGER:
506         case PType.TYPECODE_BYTEARRAY:
507         case PType.TYPECODE_CHARARRAY:
508         default:
509             throw new ExpressionException("Impossible to convert "
510                 + subType.getJavaName() + " to " + type.getJavaName());
511         }
512         return result;
513     }
514
515     public Operand compileExpression()
516         throws ExpressionException, MalformedExpressionException {
517         if (result == null) {
518             result = new BasicVariableOperand(type);
519         }
520         expressions[0].compileExpression();
521         return result;
522     }
523 }
524
Popular Tags