KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > query > BinaryExpr


1 /*
2  * Copyright (c) 1998-2004 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.amber.query;
30
31 import com.caucho.util.L10N;
32 import com.caucho.util.CharBuffer;
33
34 /**
35  * Bound identifier expression.
36  */

37 class BinaryExpr extends AbstractAmberExpr {
38   private static final L10N L = new L10N(BinaryExpr.class);
39   
40   private AmberExpr _left;
41   private AmberExpr _right;
42   private int _token;
43
44   /**
45    * Creates a new cmp expression
46    */

47   BinaryExpr(int token, AmberExpr left, AmberExpr right)
48   {
49     _token = token;
50     _left = left;
51     _right = right;
52   }
53
54   /**
55    * Returns true for a boolean expr.
56    */

57   public boolean isBoolean()
58   {
59     switch (_token) {
60     case QueryParser.EQ:
61     case QueryParser.NE:
62     case QueryParser.LT:
63     case QueryParser.LE:
64     case QueryParser.GT:
65     case QueryParser.GE:
66       return true;
67     default:
68       return false;
69     }
70   }
71
72   /**
73    * Returns the java type.
74    */

75   public Class getJavaType()
76   {
77     switch (_token) {
78     case QueryParser.EQ:
79     case QueryParser.NE:
80     case QueryParser.LT:
81     case QueryParser.LE:
82     case QueryParser.GT:
83     case QueryParser.GE:
84       return boolean.class;
85     default:
86       return double.class;
87     }
88   }
89
90   /**
91    * Binds the expression as a select item.
92    */

93   public AmberExpr bindSelect(QueryParser parser)
94   {
95     _left = _left.bindSelect(parser);
96     _right = _right.bindSelect(parser);
97
98     return this;
99   }
100
101   /**
102    * Returns true if the expression uses the from item.
103    */

104   public boolean usesFrom(FromItem from, int type, boolean isNot)
105   {
106     return (_left.usesFrom(from, type) || _right.usesFrom(from, type));
107   }
108
109   /**
110    * Returns true if the expression uses the from item.
111    */

112   public AmberExpr replaceJoin(JoinExpr join)
113   {
114     _left = _left.replaceJoin(join);
115     _right = _right.replaceJoin(join);
116     
117     return this;
118   }
119   
120   /**
121    * Generates the where expression.
122    */

123   public void generateWhere(CharBuffer cb)
124   {
125     cb.append('(');
126     
127     _left.generateWhere(cb);
128     
129     switch (_token) {
130     case QueryParser.EQ:
131       cb.append(" = ");
132       break;
133     case QueryParser.NE:
134       cb.append(" <> ");
135       break;
136     case QueryParser.LT:
137       cb.append(" < ");
138       break;
139     case QueryParser.LE:
140       cb.append(" <= ");
141       break;
142     case QueryParser.GT:
143       cb.append(" > ");
144       break;
145     case QueryParser.GE:
146       cb.append(" >= ");
147       break;
148     case '+':
149       cb.append(" + ");
150       break;
151     case '-':
152       cb.append(" - ");
153       break;
154     case '*':
155       cb.append(" * ");
156       break;
157     case '/':
158       cb.append(" / ");
159       break;
160     case '%':
161       cb.append(" % ");
162       break;
163     default:
164       throw new IllegalStateException();
165     }
166
167     _right.generateWhere(cb);
168     
169     cb.append(')');
170   }
171
172   public String toString()
173   {
174     String str = "(" + _left;
175
176     switch (_token) {
177     case QueryParser.EQ:
178       str += " = ";
179       break;
180     case QueryParser.NE:
181       str += " <> ";
182       break;
183     case QueryParser.LT:
184       str += " < ";
185       break;
186     case QueryParser.LE:
187       str += " <= ";
188       break;
189     case QueryParser.GT:
190       str += " > ";
191       break;
192     case QueryParser.GE:
193       str += " >= ";
194       break;
195     case '+':
196       str += " + ";
197       break;
198     case '-':
199       str += " - ";
200       break;
201     case '*':
202       str += " * ";
203       break;
204     case '/':
205       str += " / ";
206       break;
207     case '%':
208       str += " % ";
209       break;
210     }
211     
212     return str + _right + ")";
213   }
214 }
215
Popular Tags