KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > query > lib > QueryDefinitionImpl


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.query.lib;
19
20 import org.objectweb.speedo.query.api.QueryDefinition;
21
22 import javax.jdo.Extent;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 /**
29  * is a basic implementation of the QueryDefinition interface.
30  *
31  * @author S.Chassande-Barrioz
32  */

33 public class QueryDefinitionImpl implements QueryDefinition {
34     /**
35      * IgnoreCache option.
36      * The ignoreCache option setting specifies whether the query should execute
37      * entirely in the back end, instead of in the cache. If this flag is set
38      * to true, an implementation might be able to optimize the query
39      * execution by ignoring changed values in the cache. For optimistic
40      * transactions, this can dramatically improve query response times.
41      */

42     protected boolean ignoreCache = true;
43
44     /**
45      * Candidate classes or extent for the query.
46      */

47     protected Collection JavaDoc candidateInstances = null;
48     protected Extent extentClass = null;
49
50     /**
51      * Class of candadate classes.
52      */

53     protected Class JavaDoc candidateClass = null;
54
55     /**
56      * Query filter.
57      */

58     protected String JavaDoc filter = "true";
59
60     /**
61      * Query parameters and variables declaration
62      */

63     protected String JavaDoc parameters = null;
64     protected String JavaDoc variables = null;
65
66     /**
67      * the import statements is transformed into a simple vector
68      */

69     protected List JavaDoc importStatements = null;
70     protected List JavaDoc order = null;
71
72     transient protected long indexFirst = 0;
73
74     transient protected long indexLast = Long.MAX_VALUE;
75
76     protected boolean unique;
77
78     protected String JavaDoc result;
79
80     protected Class JavaDoc resultClass;
81     
82     protected boolean withPrefetch;
83     
84     boolean includeSubClasses = true;
85     
86     protected String JavaDoc grouping;
87
88     public QueryDefinitionImpl() {
89     }
90
91     public QueryDefinitionImpl(QueryDefinition qd) {
92         defineWith(qd);
93     }
94
95     public void defineWith(QueryDefinition qd) {
96         if (qd == null) {
97             this.candidateClass = null;
98             this.candidateInstances = null;
99             this.extentClass = null;
100             this.filter = null;
101             this.order = null;
102             this.variables = null;
103             this.parameters = null;
104             this.importStatements = null;
105             this.ignoreCache = true;
106             this.indexFirst = 0;
107             this.indexLast = Long.MAX_VALUE;
108             this.withPrefetch = true;
109             this.grouping = null;
110             this.result = null;
111             this.resultClass = null;
112             this.includeSubClasses = true;
113             this.unique = false;
114         } else {
115             this.candidateClass = qd.getCandidateClass();
116             this.candidateInstances = qd.getCollection();
117             this.extentClass = qd.getExtent();
118             this.filter = qd.getFilter();
119             this.order = qd.getOrder();
120             this.variables = qd.getVariables();
121             this.parameters = qd.getParameters();
122             this.importStatements = qd.getImportStatements();
123             this.ignoreCache = qd.isIgnoreCache();
124             this.indexFirst = qd.getIndexFirst();
125             this.indexLast = qd.getIndexLast();
126             this.withPrefetch = qd.withPrefetch();
127             this.grouping = qd.getGrouping();
128             this.result = qd.getResult();
129             this.resultClass = qd.getResultClass();
130             this.includeSubClasses = qd.getIncludeSubClasses();
131             this.unique = qd.getUnique();
132         }
133     }
134
135
136     public Class JavaDoc getCandidateClass() {
137         return candidateClass;
138     }
139
140     public boolean isCollection() {
141         return candidateInstances != null;
142     }
143
144     public Collection JavaDoc getCollection() {
145         return candidateInstances;
146     }
147
148     public String JavaDoc getParameters() {
149         return parameters;
150     }
151
152     public String JavaDoc getVariables() {
153         return variables;
154     }
155
156     public String JavaDoc getFilter() {
157         return filter;
158     }
159
160     public Extent getExtent() {
161         return extentClass;
162     }
163
164     public List JavaDoc getImportStatements() {
165         return importStatements;
166     }
167
168     public boolean isIgnoreCache() {
169         return ignoreCache;
170     }
171
172     public List JavaDoc getOrder() {
173         return order;
174     }
175
176     public long getIndexFirst() {
177         return indexFirst;
178     }
179
180     public long getIndexLast() {
181         return indexLast;
182     }
183
184     public boolean getUnique() {
185         return unique;
186     }
187
188     public String JavaDoc getResult() {
189         return result;
190     }
191
192     public Class JavaDoc getResultClass() {
193         return resultClass;
194     }
195
196     public boolean getIncludeSubClasses() {
197         return includeSubClasses;
198     }
199         
200     public String JavaDoc getGrouping() {
201         return grouping;
202     }
203     public boolean withPrefetch() {
204         return withPrefetch;
205     }
206     public void withPrefetch(boolean withPrefetch) {
207         this.withPrefetch = withPrefetch;
208     }
209     public String JavaDoc toString() {
210         return qdToString(true);
211     }
212     public String JavaDoc qdToString(final boolean oneLine) {
213         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
214         String JavaDoc sep = "";
215         final String JavaDoc sepNext = (oneLine ? ", " : "\n\t-");
216         if (candidateClass!=null) {
217             sb.append(sep).append("class=\"").append(candidateClass.getName()).append("\"");
218             sep = sepNext;
219         }
220         if (parameters!=null && parameters.length() >0) {
221             sb.append(sep).append("parameters=\"").append(parameters).append("\"");
222             sep = sepNext;
223         }
224         if (variables!=null && variables.length() >0) {
225             sb.append(sep).append("variables=\"").append(variables).append("\"");
226             sep = sepNext;
227         }
228         if (filter!=null && filter.length() >0 && !filter.equals("true")) {
229             sb.append(sep).append("filter=\"").append(filter).append("\"");
230             sep = sepNext;
231         }
232         if (order!=null && order.size() >0) {
233             sb.append(sep).append("order=\"");
234             for (Iterator JavaDoc iter = order.iterator(); iter.hasNext();) {
235                 sb.append(iter.next());
236             }
237             sb.append("\"");
238             sep = sepNext;
239         }
240         if (result != null) {
241             sb.append(sep).append("result=\"").append(result).append("\"");
242             sep = sepNext;
243         }
244         if (resultClass != null) {
245             sb.append(sep).append("resultClass=\"").append(resultClass).append("\"");
246             sep = sepNext;
247         }
248         if (grouping!=null && grouping.length() >0) {
249             sb.append(sep).append("grouping=\"").append(grouping).append("\"");
250             sep = sepNext;
251         }
252         if (unique) {
253             sb.append(sep).append("unique=\"").append(unique).append("\"");
254             sep = sepNext;
255         }
256         if (ignoreCache) {
257             sb.append(sep).append("ignoreCache=\"").append(ignoreCache).append("\"");
258             sep = sepNext;
259         }
260         if (!withPrefetch) {
261             sb.append(sep).append("withPrefetch=\"").append(withPrefetch).append("\"");
262             sep = sepNext;
263         }
264         return sb.toString();
265     }
266
267     public int hashCode() {
268         return candidateClass.hashCode();
269     }
270
271     public boolean equals(Object JavaDoc obj) {
272         if (obj == null || !(obj instanceof QueryDefinition)) {
273             return false;
274         }
275         QueryDefinition qd = (QueryDefinition) obj;
276         return (candidateClass==null
277                 ? qd.getCandidateClass()==null
278                 : candidateClass.equals(qd.getCandidateClass()))
279                 && (parameters == null
280                 ? qd.getParameters() == null
281                 : parameters.equals(qd.getParameters()))
282                 && (variables == null
283                 ? qd.getVariables() == null
284                 : variables.equals(qd.getVariables()))
285                 && (filter == null
286                 ? qd.getFilter() == null
287                 : filter.equals(qd.getFilter()))
288                 && (order == null
289                 ? qd.getOrder() == null
290                 : order.equals(qd.getOrder()))
291                 && (result == null
292                         ? qd.getResult() == null
293                         : result.equals(qd.getResult()))
294                 && (resultClass == null
295                         ? qd.getResultClass() == null
296                         : resultClass.equals(qd.getResultClass()))
297                 && (grouping == null
298                         ? qd.getGrouping() == null
299                         : grouping.equals(qd.getGrouping()))
300                 && (qd.isCollection()
301                     ? qd.getCollection().equals(candidateInstances) : true)
302                 && (qd.getUnique() == unique)
303                 && (qd.withPrefetch() == withPrefetch)
304                 && sameRangeCondition(qd)
305                 ;
306     }
307     
308     private boolean sameRangeCondition(QueryDefinition qd) {
309         //index of first must be the same thing 0 or not 0
310
if ((indexFirst == 0 && qd.getIndexFirst() != 0)
311                 || (indexFirst != 0 && qd.getIndexFirst() == 0)) {
312                 return false;
313         }
314         //index of the last must be the same thing Long.MAX_VALUE or not Long.MAX_VALUE
315
if ((indexLast == Long.MAX_VALUE && qd.getIndexLast() != Long.MAX_VALUE)
316                 || (indexLast != Long.MAX_VALUE && qd.getIndexLast() == Long.MAX_VALUE)) {
317                 return false;
318         }
319         return true;
320     }
321
322 }
323
Popular Tags