KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > query > namespace > MatchNSRangeQuery


1 package com.jofti.query.namespace;
2
3 import com.jofti.api.IndexQuery;
4 import com.jofti.cache.adapter.NameSpaceWrapper;
5 import com.jofti.core.INameSpace;
6 import com.jofti.core.INameSpaceAware;
7 import com.jofti.core.QueryId;
8 import com.jofti.core.QueryType;
9 import com.jofti.util.ReflectionUtil;
10
11 /**
12  *
13  *
14 * A utility class to enable simple queries to be done easily. The Query matches
15 * ranges for particular field. This is equivalent to (>= and <=) or (> and <).
16  * <p>
17  * All nameSpace queries must provide a correct namespace type for the implementation. Some nameSpaces
18  * are hierachical and so the search will use the nameSpace as starting point. Others are flat and so there is no
19  * traversal step.
20  * <p>
21  * This query cannot be combined with any other. iF you want to construct more complex queries use the @link com.jofti.query.Query class.
22  *<p>
23  * @author steve Woodcock
24  */

25 public class MatchNSRangeQuery implements IndexQuery, INameSpaceAware,QueryId {
26
27     public final Object JavaDoc alias;
28     Object JavaDoc nameSpace;
29     Class JavaDoc className;
30     String JavaDoc propertyName;
31     Comparable JavaDoc startValue;
32     Comparable JavaDoc endValue;
33     NameSpaceWrapper wrapper;
34     boolean inclusive =true;
35     
36     static final QueryType QUERY_ID=QueryType.RANGE_NS_QUERY;
37     /**
38      * Construct a query supplying the classname of the object type to be returned, the namespace
39      * under which to start the search. The field to be queried and the start and finish values to be used.
40      * <p>
41      * The field type in the object and the values must be of the same type. In addition the end value must
42      * be greater (comparaison >0 ) than the start value;
43      * <p>
44      * An example usage (for JBossCache) would be:
45      * <p>
46      * new MatchNSQuery("org.package.Myclass", "/test", "myProperty" ,new Integer(10),new Integer(20));
47      * <p>
48      * @param className - the class of object to be returned.
49      * @param nameSpace - the name space to be searched.
50      * @param propertyName - the field name to use
51      * @param startValue - the value that is used as the start search value.
52      * @param endValue - the value that is used as the end search value.
53      */

54     public MatchNSRangeQuery(Class JavaDoc className, Object JavaDoc nameSpace, String JavaDoc propertyName, Comparable JavaDoc startValue,Comparable JavaDoc endValue){
55         this(className,nameSpace,propertyName,startValue,endValue,null);
56     }
57     public MatchNSRangeQuery(Class JavaDoc className, Object JavaDoc nameSpace, String JavaDoc propertyName, Comparable JavaDoc startValue,Comparable JavaDoc endValue,Object JavaDoc alias){
58         this.className = className;
59         this.propertyName = propertyName;
60         this.startValue = startValue;
61         this.endValue = endValue;
62         this.nameSpace = nameSpace;
63         this.alias=alias;
64     }
65     
66     public MatchNSRangeQuery(String JavaDoc className, Object JavaDoc nameSpace, String JavaDoc propertyName, Comparable JavaDoc startValue,Comparable JavaDoc endValue){
67         this(className,nameSpace,propertyName,startValue,endValue,null);
68     }
69     public MatchNSRangeQuery(String JavaDoc className, Object JavaDoc nameSpace, String JavaDoc propertyName, Comparable JavaDoc startValue,Comparable JavaDoc endValue,Object JavaDoc alias){
70         Class JavaDoc clazz = null;
71         try{
72             clazz = ReflectionUtil.classForName(className);
73         }catch (Exception JavaDoc e){
74             throw new RuntimeException JavaDoc(e);
75         }
76         this.className = clazz;
77         this.propertyName = propertyName;
78         this.startValue = startValue;
79         this.endValue = endValue;
80         this.nameSpace = nameSpace;
81         this.alias =alias;
82     }
83     
84     /**
85      * Construct a query supplying the classname of the object type to be returned, the namespace
86      * under which to start the search. The field to be queried and the start and finish values to be used.
87      * This method also allows inclusivity to be set.
88      * <p>
89      * The field type in the object and the values must be of the same type. In addition the end value must
90      * be greater (comparaison >0 ) than the start value;
91      * <p>
92      * An example usage (for JBossCache) would be:
93      * <p>
94      * new MatchNSRangeQuery("org.package.Myclass", "/test", "myProperty" ,new Integer(10),new Integer(20), true);
95      * <p>
96      * @param className - the class of object to be returned.
97      * @param nameSpace - the name space to be searched.
98      * @param propertyName - the field name to use
99      * @param startValue - the value that is used as the start search value.
100      * @param endValue - the value that is used as the end search value.
101      * @param inclusive - whether the seacrh should be inclusive of values.
102      */

103     
104     public MatchNSRangeQuery(Class JavaDoc className, Object JavaDoc nameSpace, String JavaDoc propertyName, Comparable JavaDoc startValue,Comparable JavaDoc endValue, boolean inclusive){
105         this(className,nameSpace,propertyName,startValue,endValue,inclusive,null);
106     }
107     public MatchNSRangeQuery(Class JavaDoc className, Object JavaDoc nameSpace, String JavaDoc propertyName, Comparable JavaDoc startValue,Comparable JavaDoc endValue, boolean inclusive,Object JavaDoc alias){
108         this.className = className;
109         this.propertyName = propertyName;
110         this.startValue = startValue;
111         this.endValue = endValue;
112         this.nameSpace = nameSpace;
113         this.inclusive = inclusive;
114         this.alias = alias;
115     }
116     
117     public MatchNSRangeQuery(String JavaDoc className, Object JavaDoc nameSpace, String JavaDoc propertyName, Comparable JavaDoc startValue,Comparable JavaDoc endValue, boolean inclusive){
118         this(className,nameSpace,propertyName,startValue,endValue,inclusive,null);
119     }
120     public MatchNSRangeQuery(String JavaDoc className, Object JavaDoc nameSpace, String JavaDoc propertyName, Comparable JavaDoc startValue,Comparable JavaDoc endValue, boolean inclusive,Object JavaDoc alias){
121         Class JavaDoc clazz = null;
122         try{
123             clazz = ReflectionUtil.classForName(className);
124         }catch (Exception JavaDoc e){
125             throw new RuntimeException JavaDoc(e);
126         }
127         this.className = clazz;
128         this.propertyName = propertyName;
129         this.startValue = startValue;
130         this.endValue = endValue;
131         this.nameSpace = nameSpace;
132         this.inclusive = inclusive;
133         this.alias = alias;
134     }
135     
136     /**
137      * Construct a query supplying the value to be searched against and the namespace
138      * under which to start the search. This is a convenience method for classes (such as String,Integer etc)
139      * that have no property value as such. Instead the value is the class type.
140      * <p>
141      *
142      * An example usage (for JBossCache) would be:
143      * <p>
144      * new MatchNSRangeQuery("/test", "ab","zz",true);
145      * <p>
146      * This is so you do not have to use the methods above in the manner of
147      * <p>
148      * new MatchNSRangeQuery("java.lang.String", "/test", null ,"ab","zz",true);
149      * <p>
150      * @param nameSpace - the name space to be searched.
151      * @param startValue - the value that is used as the start search value.
152      * @param endValue - the value that is used as the end search value.
153      * @param inclusive - whether the seacrh should be inclusive of values.
154      */

155     
156     public MatchNSRangeQuery(Object JavaDoc nameSpace,Comparable JavaDoc startValue,Comparable JavaDoc endValue, boolean inclusive){
157         this(nameSpace,startValue,endValue,inclusive,null);
158     }
159     public MatchNSRangeQuery(Object JavaDoc nameSpace,Comparable JavaDoc startValue,Comparable JavaDoc endValue, boolean inclusive,Object JavaDoc alias){
160         this.nameSpace = nameSpace;
161         this.startValue = startValue;
162         this.endValue = endValue;
163         this.inclusive = inclusive;
164         this.alias=alias;
165     }
166     /**
167      * @return Returns the className.
168      */

169     public Class JavaDoc getClassName() {
170         return className;
171     }
172
173     /**
174      * @return Returns the propertyName.
175      */

176     public String JavaDoc getPropertyName() {
177         return propertyName;
178     }
179
180     /**
181      * @return Returns the value.
182      */

183     
184     
185     /**
186      * @return Returns the endValue.
187      */

188     public Comparable JavaDoc getEndValue() {
189         return endValue;
190     }
191     
192     /**
193      * @return Returns the startValue.
194      */

195     public Comparable JavaDoc getStartValue() {
196         return startValue;
197     }
198     
199     public synchronized INameSpace getNameSpaceWrapper() {
200         
201         if (nameSpace instanceof INameSpace){
202             return (INameSpace) nameSpace;
203         }else{
204             return new NameSpaceWrapper(nameSpace);
205         }
206     }
207     /**
208      * @return Returns the nameSpace.
209      */

210     public synchronized Object JavaDoc getNameSpace() {
211         return nameSpace;
212     }
213     /**
214      * @param nameSpace The nameSpace to set.
215      */

216     public synchronized void setNameSpace(Object JavaDoc nameSpace) {
217         this.nameSpace = nameSpace;
218     }
219     public boolean isInclusive() {
220         return inclusive;
221     }
222     public void setInclusive(boolean inclusive) {
223         this.inclusive = inclusive;
224     }
225     public Object JavaDoc getAlias() {
226         return alias;
227     }
228     
229     public QueryType getQueryType() {
230         
231         return QUERY_ID;
232     }
233     
234     public IndexQuery setParameter(String JavaDoc name, Object JavaDoc value) {
235         throw new UnsupportedOperationException JavaDoc("Parameters are not supported for convenience classes");
236     }
237     /* (non-Javadoc)
238      * @see com.jofti.api.IndexQuery#setParameter(int, java.lang.Object)
239      */

240     public IndexQuery setParameter(int position, Object JavaDoc value) {
241         throw new UnsupportedOperationException JavaDoc("Parameters are not supported for convenience classes");
242
243     }
244     public IndexQuery setFirstResult(int firstResult) {
245         throw new UnsupportedOperationException JavaDoc("Result limitation is not supported for convenience classes");
246     }
247     public IndexQuery setMaxResults(int maxResults) {
248         throw new UnsupportedOperationException JavaDoc("Result limitation is not supported for convenience classes");
249     }
250 }
251
Popular Tags