KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > expression > type > ExpressionTypeHelper


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, S. Chassande-Barrioz, A. Lefebvre
22  */

23 package org.objectweb.medor.expression.type;
24
25 import org.objectweb.jorm.type.api.PType;
26 import org.objectweb.medor.expression.api.TypingException;
27
28 /**
29  * @author A. Lefebvre
30  */

31 public class ExpressionTypeHelper {
32     /**
33      * This method tells if a given Type is an arithmetic type
34      * @param t The PType to be checked.
35      * @return true if the tested PType is an arithmetic type.
36      */

37     public static final boolean isArithmeticType(PType t) {
38         switch (t.getTypeCode()) {
39         case PType.TYPECODE_BYTE:
40             return true;
41         case PType.TYPECODE_SHORT:
42             return true;
43         case PType.TYPECODE_INT:
44             return true;
45         case PType.TYPECODE_LONG:
46             return true;
47         case PType.TYPECODE_FLOAT:
48             return true;
49         case PType.TYPECODE_DOUBLE:
50             return true;
51         default:
52             return false;
53         }
54     }
55
56     /**
57      * This method returns the type which would result from applying a binary
58      * operator to 2 objects of given PType.
59      * If the two PTypes are incompatible, a TypingException is thrown.
60      * @param type1 The PType of the first object.
61      * @param type2 The PType of the second object.
62      * @return The PType of the result.
63      */

64     public static PType getResultType(PType type1, PType type2)
65         throws TypingException {
66         if (type1.isa(type2)) {
67             return type2;
68         }
69         else if (type2.isa(type1)) {
70             return type1;
71         }
72         else {
73             throw new TypingException("Incompatible types");
74         }
75     }
76 }
77
Popular Tags