KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > UnaryExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.ql;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.CharBuffer;
33
34 /**
35  * A unary expression
36  */

37 class UnaryExpr extends Expr {
38   // unary operation
39
private int _op;
40   // main expression
41
private Expr _expr;
42
43   /**
44    * Creates a unary expression.
45    *
46    * @param op the operation
47    * @param expr the expression
48    */

49   UnaryExpr(int op, Expr expr)
50     throws ConfigException
51   {
52     _op = op;
53     _expr = expr;
54
55     evalTypes();
56   }
57
58   /**
59    * Evaluates the types for the expression
60    */

61   void evalTypes()
62     throws ConfigException
63   {
64     if (getJavaType() != null)
65       return;
66
67     switch (_op) {
68     case '+':
69       if (! _expr.isNumeric())
70         throw error(L.l("`+' expects numeric expression at `{0}'", _expr));
71       setJavaType(_expr.getJavaType());
72       break;
73     case '-':
74       if (! _expr.isNumeric())
75         throw error(L.l("`-' expects numeric expression at `{0}'", _expr));
76       setJavaType(_expr.getJavaType());
77       break;
78     case Query.NOT:
79       if (! _expr.isBoolean())
80         throw error(L.l("NOT expects boolean expression at `{0}'", _expr));
81       setJavaType(boolean.class);
82       break;
83
84     default:
85       throw new RuntimeException JavaDoc();
86     }
87   }
88   
89
90   /**
91    * Prints the where SQL for this expression
92    *
93    * @param gen the java code generator
94    */

95   void generateWhere(CharBuffer cb)
96   {
97     switch (_op) {
98     case '+':
99       cb.append("+");
100       break;
101     case '-':
102       cb.append("-");
103       break;
104     case Query.NOT:
105       cb.append("NOT ");
106       break;
107     }
108     
109     _expr.generateWhereSubExpr(cb);
110   }
111 }
112
Popular Tags