KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > storage > search > implementation > ModifiableQuery


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.storage.search.implementation;
11
12 import java.util.*;
13 import org.mmbase.storage.search.*;
14
15 /**
16  * A <code>ModifiedQuery</code> enables a modifiable lightweight copy of a
17  * {@link org.mmbase.storage.search.SearchQuery SearchQuery} to be created
18  * by wrapping the original query.
19  * <p>
20  * This class is provided primarily for use by core-, security- and
21  * storage layer classes, in those rare cases where modifications may be
22  * appropriate to a query before processing it.
23  * <p>
24  * The <code>ModifiedQuery</code> wraps the original query, and can be modified
25  * without affecting the original query. Modifications are not validated, and
26  * may lead to inconsistent data in the query (e.g. sorting on fields
27  * that are not in the query), resulting in a query that can not be processed
28  * by the storage.
29  * Avoiding such inconsistencies is the responsibility of the user.
30  *
31  * @author Rob van Maris
32  * @version $Id: ModifiableQuery.java,v 1.5 2004/05/07 13:23:42 michiel Exp $
33  * @since MMBase-1.7
34  */

35 public class ModifiableQuery implements SearchQuery {
36     
37     private SearchQuery query = null;
38     
39     /**
40      * The value of the maxNumber property, -1 means: use
41      * <code>query.getMaxNumber()</code>.
42      */

43     private int maxNumber = -1;
44     
45     /**
46      * The value of the offset property, -1 means: use
47      * <code>query.getOffset()</code>.
48      */

49     private int offset = -1;
50     
51     /**
52      * The constraint, <code>null</code> means: use
53      * <code>query.getConstraint()</code>.
54      */

55     private Constraint constraint = null;
56
57     /**
58      * The fields, <code>null</code> means: use
59      * <code>query.getFields()</code>.
60      */

61     private List fields = null;
62     
63     /**
64      * The sortorders, <code>null</code> means: use
65      * <code>query.getSortOrders()</code>.
66      */

67     private List sortOrders = null;
68     
69     /**
70      * The steps, <code>null</code> means: use
71      * <code>query.getSteps()</code.
72      */

73     private List steps = null;
74     
75     /**
76      * The value of the distinct property, <code>null</code> means: use
77      * <code>query.isDistinct()</code>.
78      */

79     private Boolean JavaDoc distinct = null;
80     
81     /**
82      * The value of the aggregating property, <code>null</code> means: use
83      * <code>query.isAggregating()</code>.
84      */

85     private Boolean JavaDoc aggregating = null;
86     
87     /** Creates a new instance of ModifiedQuery */
88     public ModifiableQuery(SearchQuery query) {
89         this.query = query;
90     }
91     
92     /**
93      * Sets the maxNumber property.
94      *
95      * @param maxNumber The maxNumber value, -1 means: use
96      * <code>query.getMaxNumber()</code>.
97      * @return This <code>ModifiableQuery</code> instance.
98      */

99     public ModifiableQuery setMaxNumber(int maxNumber) {
100         this.maxNumber = maxNumber;
101         return this;
102     }
103     
104     /**
105      * Sets the offset property.
106      *
107      * @param offset The offset value, -1 means: use
108      * <code>query.getOffset()</code>.
109      * @return This <code>ModifiableQuery</code> instance.
110      */

111     public ModifiableQuery setOffset(int offset) {
112         this.offset = offset;
113         return this;
114     }
115     
116     /**
117      * Sets the constraint property.
118      *
119      * @param constraint The constraint, <code>null</code> means: use
120      * <code>query.getConstraint()</code>.
121      * @return This <code>ModifiableQuery</code> instance.
122      */

123     public ModifiableQuery setConstraint(Constraint constraint) {
124         this.constraint = constraint;
125         return this;
126     }
127     
128     /**
129      * Sets the fields property.
130      *
131      * @param fields The fields, <code>null</code> means: use
132      * <code>query.getFields()</code>.
133      * @return This <code>ModifiableQuery</code> instance.
134      */

135     public ModifiableQuery setFields(List fields) {
136         this.fields = fields;
137         return this;
138     }
139     
140     /**
141      * Sets the sortOrders property.
142      *
143      * @param sortOrders The sortorders, <code>null</code> means: use
144      * <code>query.getSortOrders()</code>.
145      * @return This <code>ModifiableQuery</code> instance.
146      */

147     public ModifiableQuery setSortOrders(List sortOrders) {
148         this.sortOrders = sortOrders;
149         return this;
150     }
151     
152     /**
153      * Sets the steps property.
154      *
155      * @param steps The steps, <code>null</code> means: use
156      * <code>query.getSteps()</code.
157      * @return This <code>ModifiableQuery</code> instance.
158      */

159     public ModifiableQuery setSteps(List steps) {
160         this.steps = steps;
161         return this;
162     }
163     
164     /**
165      * Sets the distinct property.
166      *
167      * @param distinct The value of the distinct property,
168      * <code>null</code> means: use <code>query.isDistinct()</code>.
169      * @return This <code>ModifiableQuery</code> instance.
170      */

171     public ModifiableQuery setDistinct(Boolean JavaDoc distinct) {
172         this.distinct = distinct;
173         return this;
174     }
175     
176     /**
177      * Sets the aggregating property.
178      *
179      * @param aggregating The value of the aggregating property,
180      * <code>null</code> means: use <code>query.isAggregating()</code>.
181      * @return This <code>ModifiableQuery</code> instance.
182      */

183     public ModifiableQuery setAggregating(Boolean JavaDoc aggregating) {
184         this.aggregating = aggregating;
185         return this;
186     }
187     
188     // javadoc is inherited
189
public int getMaxNumber() {
190         if (maxNumber != -1) {
191             return maxNumber;
192         } else {
193             return query.getMaxNumber();
194         }
195     }
196     
197     // javadoc is inherited
198
public int getOffset() {
199         if (offset != -1) {
200             return offset;
201         } else {
202             return query.getOffset();
203         }
204     }
205     
206     // javadoc is inherited
207
public Constraint getConstraint() {
208         if (constraint != null) {
209             return constraint;
210         } else {
211             return query.getConstraint();
212         }
213     }
214     
215     // javadoc is inherited
216
public List getFields() {
217         if (fields != null) {
218             return fields;
219         } else {
220             return query.getFields();
221         }
222     }
223     
224     // javadoc is inherited
225
public List getSortOrders() {
226         if (sortOrders != null) {
227             return sortOrders;
228         } else {
229             return query.getSortOrders();
230         }
231     }
232     
233     // javadoc is inherited
234
public List getSteps() {
235         if (steps != null) {
236             return steps;
237         } else {
238             return query.getSteps();
239         }
240     }
241     
242     // javadoc is inherited
243
public boolean isDistinct() {
244         if (distinct != null) {
245             return distinct.booleanValue();
246         } else {
247             return query.isDistinct();
248         }
249     }
250     
251     // javadoc is inherited
252
public boolean isAggregating() {
253         if (aggregating != null) {
254             return aggregating.booleanValue();
255         } else {
256             return query.isAggregating();
257         }
258     }
259
260
261     /**
262      * {@inheritDoc}
263      * Should correspond to {@link BasicSearchQuery#equals}
264      */

265     public boolean equals(Object JavaDoc obj) {
266         if (obj == this) {
267             return true;
268         }
269         if (obj instanceof SearchQuery) {
270             SearchQuery query = (SearchQuery) obj;
271             Constraint constraint = getConstraint();
272             return isDistinct() == query.isDistinct()
273                 && getMaxNumber() == query.getMaxNumber()
274                 && getOffset() == query.getOffset()
275                 && getSteps().equals(query.getSteps())
276                 && getFields().equals(query.getFields())
277                 && getSortOrders().equals(query.getSortOrders())
278                 && (constraint == null?
279                     query.getConstraint() == null:
280                     constraint.equals(query.getConstraint()));
281         } else {
282             return false;
283         }
284     }
285
286     /**
287      * {@inheritDoc}
288      * Should correspond to {@link BasicSearchQuery#hashCode}
289      */

290     public int hashCode() {
291         Constraint constraint = getConstraint();
292         return (isDistinct()? 0: 101)
293         + getMaxNumber() * 17 + getOffset() * 19
294         + 23 * getSteps().hashCode()
295         + 29 * getFields().hashCode()
296         + 31 * getSortOrders().hashCode()
297         + 37 * (constraint == null ? 0 : constraint.hashCode());
298     }
299
300     // javadoc is inherited
301
public String JavaDoc toString() {
302         return "ModifiableSearchQuery(distinct:" + isDistinct()
303         + ", steps:" + getSteps()
304         + ", fields:" + getFields()
305         + ", constraint:" + getConstraint()
306         + ", sortorders:" + getSortOrders()
307         + ", max:" + getMaxNumber()
308         + ", offset:" + getOffset() + ")";
309     }
310
311
312     
313 }
314
Popular Tags