KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > 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 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 import java.util.ArrayList JavaDoc;
35
36 /**
37  * A binary expression
38  */

39 class AndExpr extends Expr {
40   // components to the and
41
private ArrayList JavaDoc<Expr> _components = new ArrayList JavaDoc<Expr>();
42
43   /**
44    * Creates an and expression.
45    */

46   AndExpr(Query query)
47     throws ConfigException
48   {
49     setJavaType(boolean.class);
50   }
51
52   /**
53    * Evaluates the types for the expression
54    */

55   void evalTypes()
56     throws ConfigException
57   {
58     setJavaType(boolean.class);
59   }
60
61   /**
62    * Adds a subexpression.
63    */

64   void add(Expr expr)
65   {
66     _components.add(expr);
67   }
68
69   /**
70    * Return the expression if single.
71    */

72   Expr getSingleExpr()
73   {
74     if (_components.size() == 1)
75       return _components.get(0);
76     else
77       return this;
78   }
79   
80   /**
81    * Prints the where SQL for this expression
82    *
83    * @param gen the java code generator
84    */

85   void generateWhere(CharBuffer cb)
86   {
87     if (_components.size() == 1) {
88       Expr comp = _components.get(0);
89
90       comp.generateWhere(cb);
91       return;
92     }
93
94     for (int i = 0; i < _components.size(); i++) {
95       Expr comp = _components.get(i);
96
97       if (i != 0)
98     cb.append(" AND ");
99
100       comp.generateWhereSubExpr(cb);
101     }
102   }
103
104   /**
105    * Prints the where SQL for this expression
106    */

107   void generateWhereSubExpr(CharBuffer cb)
108   {
109     cb.append("(");
110     generateWhere(cb);
111     cb.append(")");
112   }
113   
114   void printSelect(CharBuffer cb)
115     throws ConfigException
116   {
117     cb.append("(");
118     generateWhere(cb);
119     cb.append(")");
120   }
121
122   public String JavaDoc toString()
123   {
124     CharBuffer value = CharBuffer.allocate();
125
126     value.append("(");
127
128     for (int i = 0; i < _components.size(); i++) {
129       Expr comp = _components.get(i);
130
131       if (i != 0)
132     value.append(" AND ");
133
134       value.append(comp.toString());
135     }
136
137     value.append(")");
138
139     return value.close();
140   }
141 }
142
Popular Tags