KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > server > store > LogicalClause


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.server.store;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import org.apache.commons.lang.builder.ToStringBuilder;
25
26 /**
27  * This class recognize the clause of type logical for the query.
28  * For logical clause mean AND, OR, NOT clause.
29  *
30  * @author Luigia Fassina @ Funambol
31  *
32  * @version $Id: LogicalClause.java,v 1.5 2005/03/02 20:57:38 harrie Exp $
33  *
34  */

35 public class LogicalClause
36 extends Clause
37 implements Serializable JavaDoc {
38
39     // --------------------------------------------------------------- Constants
40

41     public static final String JavaDoc OPT_AND = "AND";
42     public static final String JavaDoc OPT_OR = "OR" ;
43     public static final String JavaDoc OPT_NOT = "NOT";
44     
45     // ------------------------------------------------------------ Private data
46

47     private String JavaDoc operator;
48     private Clause[] operands;
49     
50     // ------------------------------------------------------------ constructors
51

52     /**
53      * The default constructor is disabled. We do not want it to be used.
54      */

55     public LogicalClause() {}
56
57     /**
58      * Create a new LogicalClause object with the given operator and operands.
59      * Operator must be one of
60      * <ul>
61      * <li> AND
62      * <li> OR
63      * <li> NOT
64      * </ul>
65      *
66      * operands must be of size 1 in the case operator is NOT and of size 2 in
67      * the case operator is AND or OR.
68      *
69      * @param operator the operator
70      * @param operands the operands - NOT NULL
71      *
72      * @throws IllegalArgumentException if the given values are not correct.
73      * @throws IllegalArgumentException if any required parameter is null.
74      */

75     public LogicalClause(String JavaDoc operator, Clause[] operands) {
76         if (!OPT_AND.equalsIgnoreCase(operator) &&
77             !OPT_OR.equalsIgnoreCase(operator) &&
78             !OPT_NOT.equalsIgnoreCase(operator)) {
79             throw new IllegalArgumentException JavaDoc(
80                 "operator " +
81                 operator +
82                 " not supported; it must be one of (" +
83                 OPT_AND +
84                 "," +
85                 OPT_OR +
86                 "," +
87                 OPT_NOT +
88                 ")"
89             );
90         }
91         if (operands == null) {
92             throw new NullPointerException JavaDoc("operands is null!");
93         }
94         
95         if (OPT_NOT.equalsIgnoreCase(operator)) {
96             if (operands.length != 1) {
97                 throw new IllegalArgumentException JavaDoc(
98                     "one and only one operand is required with " + OPT_NOT
99                 );
100             }
101         } else {
102             if (operands.length < 2) {
103                 throw new IllegalArgumentException JavaDoc(
104                     "two or more operands are required with " + OPT_AND + " or " + OPT_OR
105                 );
106             }
107         }
108         
109         this.operator = operator;
110         this.operands = operands;
111     }
112
113     /** Getter for property operator.
114      * @return Value of property operator.
115      *
116      */

117     public String JavaDoc getOperator() {
118         return operator;
119     }
120     
121     /**
122      * Setter for property operator.
123      * @param operator New value of property operator.
124      */

125     public void setOperator(java.lang.String JavaDoc operator) {
126         this.operator = operator;
127     }
128     
129     /** Getter for property operands.
130      * @return Value of property operands.
131      *
132      */

133     public Clause[] getOperands() {
134         return operands;
135     }
136     
137     /**
138      * Setter for property operands.
139      * @param operands New value of property operands.
140      */

141     public void setOperands(sync4j.framework.server.store.Clause[] operands) {
142         this.operands = operands;
143     }
144
145     
146     // ------------------------------------------------ Implementation of Clause
147

148     public PreparedWhere getPreparedWhere() {
149         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
150         ArrayList JavaDoc parameters = new ArrayList JavaDoc();
151         PreparedWhere pw = null;
152         
153         if (isUnaryOperator()) {
154             pw = operands[0].getPreparedWhere();
155             sql.append(operator.toLowerCase()).append(pw.sql);
156             for(int i=0; i<pw.parameters.length; ++i) {
157                 parameters.add(pw.parameters[i]);
158             }
159         } else {
160             for (int i=0; i<operands.length; ++i) {
161                 pw = operands[i].getPreparedWhere();
162                 
163                 if (i>0) {
164                     sql.append(' ').append(operator.toLowerCase()).append(' ');
165                 }
166                 sql.append(pw.sql);
167             
168                 for(int j=0; j<pw.parameters.length; ++j) {
169                     parameters.add(pw.parameters[j]);
170                 }
171             }
172         }
173         
174         PreparedWhere ret = new PreparedWhere();
175         
176         ret.sql = '(' + sql.toString() + ')';
177         ret.parameters = parameters.toArray();
178       
179         return ret;
180     }
181     
182     // --------------------------------------------------------- Private methods
183

184     private boolean isUnaryOperator() {
185         return OPT_NOT.equalsIgnoreCase(operator);
186     }
187
188     // -------------------------------------------------------------------------
189

190     public String JavaDoc toString() {
191         ToStringBuilder sb = new ToStringBuilder(this);
192         sb.append("operator:", operator);
193
194         return sb.toString();
195     }
196     
197 }
198
Popular Tags