KickJava   Java API By Example, From Geeks To Geeks.

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


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 disjunction.
40  */

41 public class OrExpr extends AbstractAmberExpr {
42   private ArrayList JavaDoc<AmberExpr> _components = new ArrayList JavaDoc<AmberExpr>();
43
44   /**
45    * Returns true for a boolean expression.
46    */

47   public boolean isBoolean()
48   {
49     return true;
50   }
51
52   /**
53    * Adds a new component.
54    */

55   public void add(AmberExpr expr)
56     throws QueryParseException
57   {
58     _components.add(expr.createBoolean());
59   }
60
61   /**
62    * Binds the expression as a select item.
63    */

64   public AmberExpr bindSelect(QueryParser parser)
65   {
66     for (int i = 0; i < _components.size(); i++) {
67       AmberExpr expr = _components.get(i);
68
69       expr = expr.bindSelect(parser);
70
71       _components.set(i, expr);
72     }
73
74     return this;
75   }
76
77   /**
78    * Returns true if the expression uses the from item.
79    */

80   public boolean usesFrom(FromItem from, int type, boolean isNot)
81   {
82     if (type == IS_INNER_JOIN) {
83       // returns true if the from item is used in every term of the disjunction
84
for (int i = 0; i < _components.size(); i++) {
85         AmberExpr expr = _components.get(i);
86
87         if (! isNot && ! expr.usesFrom(from, type, isNot))
88           return false;
89         else if (isNot && expr.usesFrom(from, type, isNot))
90           return true;
91       }
92
93       return false;
94     }
95     else {
96       for (int i = 0; i < _components.size(); i++) {
97         AmberExpr expr = _components.get(i);
98
99         if (expr.usesFrom(from, type))
100           return true;
101       }
102
103       return false;
104     }
105   }
106
107   /**
108    * Returns true if the expression uses the from item.
109    */

110   public AmberExpr replaceJoin(JoinExpr join)
111   {
112     for (int i = 0; i < _components.size(); i++) {
113       AmberExpr expr = _components.get(i);
114
115       expr = expr.replaceJoin(join);
116
117       _components.set(i, expr);
118     }
119
120     return this;
121   }
122
123   /**
124    * Returns a single expression.
125    */

126   AmberExpr getSingle()
127   {
128     if (_components.size() == 0)
129       return null;
130     else if (_components.size() == 1)
131       return _components.get(0);
132     else
133       return this;
134   }
135
136   /**
137    * Generates the where expression.
138    */

139   public void generateWhere(CharBuffer cb)
140   {
141     generateInternalWhere(cb, true);
142   }
143
144   /**
145    * Generates the (update) where expression.
146    */

147   public void generateUpdateWhere(CharBuffer cb)
148   {
149     generateInternalWhere(cb, false);
150   }
151
152   /**
153    * Generates the having expression.
154    */

155   public void generateHaving(CharBuffer cb)
156   {
157     generateWhere(cb);
158   }
159
160   /**
161    * Generates the join expression.
162    */

163   public void generateJoin(CharBuffer cb)
164   {
165     cb.append('(');
166
167     for (int i = 0; i < _components.size(); i++) {
168       if (i != 0)
169         cb.append(" OR ");
170
171       AmberExpr expr = _components.get(i);
172
173       expr.generateJoin(cb);
174     }
175
176     cb.append(')');
177   }
178
179   public String JavaDoc toString()
180   {
181     if (_components.size() == 1) {
182       return _components.get(0).toString();
183     }
184     else {
185       CharBuffer cb = new CharBuffer();
186
187       cb.append('(');
188
189       for (int i = 0; i < _components.size(); i++) {
190         if (i != 0)
191           cb.append(" OR ");
192
193         AmberExpr expr = _components.get(i);
194
195         cb.append(expr);
196       }
197
198       cb.append(')');
199
200       return cb.toString();
201     }
202   }
203
204   //
205
// private
206

207   private void generateInternalWhere(CharBuffer cb,
208                                      boolean select)
209   {
210     cb.append('(');
211
212     for (int i = 0; i < _components.size(); i++) {
213       if (i != 0)
214         cb.append(" OR ");
215
216       AmberExpr expr = _components.get(i);
217
218       if (select)
219         expr.generateWhere(cb);
220       else
221         expr.generateUpdateWhere(cb);
222     }
223
224     cb.append(')');
225   }
226 }
227
Popular Tags