KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > AndExpr


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 Software Foundation, 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.expr;
30
31 import com.caucho.amber.query.FromItem;
32 import com.caucho.amber.query.QueryParseException;
33 import com.caucho.amber.query.QueryParser;
34 import com.caucho.util.CharBuffer;
35
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * A conjunction.
40  */

41 public class AndExpr extends AbstractAmberExpr {
42   private ArrayList JavaDoc<AmberExpr> _components = new ArrayList JavaDoc<AmberExpr>();
43
44   /**
45    * Creates the and.
46    */

47   public static AmberExpr create(AmberExpr left, AmberExpr right)
48     throws QueryParseException
49   {
50     if (left == null && right == null)
51       return null;
52     else if (left == null)
53       return right.createBoolean();
54     else if (right == null)
55       return left.createBoolean();
56     else if (left instanceof AndExpr) {
57       AndExpr and = (AndExpr) left;
58       and.add(right.createBoolean());
59       return and;
60     }
61     else if (right instanceof AndExpr) {
62       AndExpr and = (AndExpr) right;
63       and.add(left.createBoolean());
64       return and;
65     }
66     else {
67       AndExpr and = new AndExpr();
68       and.add(left.createBoolean());
69       and.add(right.createBoolean());
70
71       return and;
72     }
73   }
74   /**
75    * Returns true for a boolean expression.
76    */

77   public boolean isBoolean()
78   {
79     return true;
80   }
81
82   /**
83    * Adds a new component.
84    */

85   public void add(AmberExpr expr)
86   {
87     _components.add(expr);
88   }
89
90   /**
91    * Returns the components.
92    */

93   public ArrayList JavaDoc<AmberExpr> getComponents()
94   {
95     return _components;
96   }
97
98   /**
99    * Binds the expression as a select item.
100    */

101   public AmberExpr bindSelect(QueryParser parser)
102   {
103     for (int i = 0; i < _components.size(); i++) {
104       AmberExpr expr = _components.get(i);
105
106       expr = expr.bindSelect(parser);
107
108       _components.set(i, expr);
109     }
110
111     return this;
112   }
113
114   /**
115    * Returns a single expression.
116    */

117   public AmberExpr getSingle()
118   {
119     if (_components.size() == 0)
120       return null;
121     else if (_components.size() == 1)
122       return _components.get(0);
123     else
124       return this;
125   }
126
127
128   /**
129    * Returns true if the expression uses the from item.
130    */

131   public boolean usesFrom(FromItem from, int type, boolean isNot)
132   {
133     if (type == IS_INNER_JOIN) {
134       // returns true if the from item is used in any term of the conjunction
135
for (int i = 0; i < _components.size(); i++) {
136         AmberExpr expr = _components.get(i);
137
138         if (! isNot && expr.usesFrom(from, type, isNot))
139           return true;
140         else if (isNot && ! expr.usesFrom(from, type, isNot))
141           return false;
142       }
143
144       return false;
145     }
146     else {
147       for (int i = 0; i < _components.size(); i++) {
148         AmberExpr expr = _components.get(i);
149
150         if (expr.usesFrom(from, type))
151           return true;
152       }
153
154       return false;
155     }
156   }
157
158   /**
159    * Returns true if the from item is used in the expression
160    */

161   @Override JavaDoc
162   public boolean exists(FromItem from)
163   {
164     for (int i = 0; i < _components.size(); i++) {
165       if (_components.get(i).exists(from))
166     return true;
167     }
168
169     return false;
170   }
171
172   /**
173    * Returns true if the expression uses the from item.
174    */

175   public AmberExpr replaceJoin(JoinExpr join)
176   {
177     for (int i = 0; i < _components.size(); i++) {
178       AmberExpr expr = _components.get(i);
179
180       expr = expr.replaceJoin(join);
181
182       _components.set(i, expr);
183     }
184
185     return this;
186   }
187
188   /**
189    * Generates the where expression.
190    */

191   public void generateWhere(CharBuffer cb)
192   {
193     generateInternalWhere(cb, true);
194   }
195
196   /**
197    * Generates the (update) where expression.
198    */

199   public void generateUpdateWhere(CharBuffer cb)
200   {
201     generateInternalWhere(cb, false);
202   }
203
204   /**
205    * Generates the having expression.
206    */

207   public void generateHaving(CharBuffer cb)
208   {
209     generateWhere(cb);
210   }
211
212   /**
213    * Generates the join expression.
214    */

215   public void generateJoin(CharBuffer cb)
216   {
217     cb.append('(');
218
219     for (int i = 0; i < _components.size(); i++) {
220       if (i != 0)
221         cb.append(" and ");
222
223       AmberExpr expr = _components.get(i);
224
225       expr.generateJoin(cb);
226     }
227
228     cb.append(')');
229   }
230
231   //
232
// private
233

234   private void generateInternalWhere(CharBuffer cb,
235                                      boolean select)
236   {
237     cb.append('(');
238
239     for (int i = 0; i < _components.size(); i++) {
240       if (i != 0)
241         cb.append(" and ");
242
243       AmberExpr expr = _components.get(i);
244
245       if (select)
246         expr.generateWhere(cb);
247       else
248         expr.generateUpdateWhere(cb);
249     }
250
251     cb.append(')');
252   }
253 }
254
Popular Tags