KickJava   Java API By Example, From Geeks To Geeks.

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


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 * the value for particular field. This is equivalent to =.
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  *
23  * @author steve Woodcock
24  */

25 public class MatchNSQuery implements IndexQuery,INameSpaceAware,QueryId {
26
27     public final Object JavaDoc alias;
28     private int hashCode =0;
29     Class JavaDoc className;
30     Object JavaDoc nameSpace;
31     String JavaDoc propertyName;
32     Comparable JavaDoc value;
33        static final QueryType QUERY_ID=QueryType.MATCH_NS_QUERY;
34     
35     /**
36      * Construct a query supplying the classname of the object type to be returned, the namespace
37      * under which to start the search. The field to be queried and the value to be used.
38      * <p>
39      * The field type in the object and the value must be of the same type.
40      * <p>
41      *
42      * An example usage (for JBossCache) would be:
43      * <p>
44      * new MatchNSQuery("org.package.Myclass", "/test", "myProperty" ,"some value");
45      * <p>
46      * @param className - the class of object to be returned.
47      * @param nameSpace - the name space to be searched.
48      * @param propertyName - the field name to use
49      * @param value - the value that is used as the search value.
50      */

51     public MatchNSQuery(Class JavaDoc className, Object JavaDoc nameSpace,String JavaDoc propertyName, Comparable JavaDoc value){
52         this(className,nameSpace,propertyName,value,null);
53     }
54     
55     public MatchNSQuery(Class JavaDoc className, Object JavaDoc nameSpace,String JavaDoc propertyName, Comparable JavaDoc value,Object JavaDoc alias){
56         this.className = className;
57         this.propertyName = propertyName;
58         this.value = value;
59         this.nameSpace = nameSpace;
60         this.alias = alias;
61     }
62     
63     
64     public MatchNSQuery(String JavaDoc className, Object JavaDoc nameSpace,String JavaDoc propertyName, Comparable JavaDoc value){
65         this(className, nameSpace,propertyName,value,null);
66     }
67     
68     public MatchNSQuery(String JavaDoc className, Object JavaDoc nameSpace,String JavaDoc propertyName, Comparable JavaDoc value,Object JavaDoc alias){
69         Class JavaDoc clazz = null;
70         try{
71             clazz = ReflectionUtil.classForName(className);
72         }catch (Exception JavaDoc e){
73             throw new RuntimeException JavaDoc(e);
74         }
75         this.className = clazz;
76         this.propertyName = propertyName;
77         this.value = value;
78         this.nameSpace = nameSpace;
79         this.alias = alias;
80     }
81     /**
82      * Construct a query supplying the value to be searched against and the namespace
83      * under which to start the search. This is a convenience method for classes (such as String,Integer etc)
84      * that have no property value as such. Instead the value is the class type.
85      * <p>
86      *
87      * An example usage (for JBossCache) would be:
88      * <p>
89      * new MatchNSQuery("/test","some value");
90      * <p>
91      * This is so you do not have to use the methods above in the manner of
92      * <p>
93      * new MatchNSQuery("java.lang.String", "/test", null ,"some value");
94      * <p>
95
96      * @param nameSpace - the name space to be searched.
97      * @param value - the value that is used as the search value.
98      */

99     
100     public MatchNSQuery(Object JavaDoc nameSpace,Comparable JavaDoc value){
101         this(nameSpace,value,null);
102     }
103     public MatchNSQuery(Object JavaDoc nameSpace,Comparable JavaDoc value,Object JavaDoc alias){
104         this.value = value;
105         this.nameSpace = nameSpace;
106         this.alias = alias;
107         
108     }
109     
110     public MatchNSQuery(Object JavaDoc nameSpace){
111         this(nameSpace,null);
112     }
113     public MatchNSQuery(Object JavaDoc nameSpace,Object JavaDoc alias){
114         this.nameSpace = nameSpace;
115         this.alias = alias;
116         
117     }
118     /**
119      * @return Returns the className.
120      */

121     public Class JavaDoc getClassName() {
122         return className;
123     }
124
125     /**
126      * @return Returns the propertyName.
127      */

128     public String JavaDoc getPropertyName() {
129         return propertyName;
130     }
131
132     /**
133      * @return Returns the value.
134      */

135     public Comparable JavaDoc getValue() {
136         return value;
137     }
138
139     
140     
141     
142     /**
143      * @return Returns the wrapper.
144      */

145     public synchronized INameSpace getNameSpaceWrapper() {
146         
147         if (nameSpace instanceof INameSpace){
148             return (INameSpace) nameSpace;
149         }else{
150             return new NameSpaceWrapper(nameSpace);
151         }
152     }
153     
154     /**
155      * @return Returns the nameSpace.
156      */

157     public synchronized Object JavaDoc getNameSpace() {
158         return nameSpace;
159     }
160     /**
161      * @param nameSpace The nameSpace to set.
162      */

163     public synchronized void setNameSpace(Object JavaDoc nameSpace) {
164         this.nameSpace = nameSpace;
165     }
166     
167     public boolean equals(Object JavaDoc o){
168         if (o instanceof MatchNSQuery){
169             MatchNSQuery temp = (MatchNSQuery)o;
170             boolean result = nameSpace.equals(temp.nameSpace);
171             if (!result){
172                 return result;
173             }
174             if (className != null && temp.className != null){
175                 result = className.equals(temp.className);
176                 if (!result){
177                     return result;
178                 }
179             }else {
180                 result = className == null & temp.className == null;
181                 if (!result){
182                     return result;
183                 }
184             }
185     
186             if (propertyName != null && temp.propertyName != null){
187                 result = propertyName.equals(temp.propertyName);
188                 if (!result){
189                     return result;
190                 }
191             }else {
192                 result = propertyName == null & temp.propertyName == null;
193                 if (!result){
194                     return result;
195                 }
196             }
197             if (value != null && temp.value != null){
198                 result = value.equals(temp.value);
199                 if (!result){
200                     return result;
201                 }
202             }else {
203                 result = value == null & temp.value == null;
204                 if (!result){
205                     return result;
206                 }
207             }
208             return result;
209         }
210         return false;
211     }
212     
213     public int hashCode(){
214         if (hashCode == 0){
215             int temp = nameSpace ==null?0:nameSpace.hashCode();
216             if (className != null){
217                 temp+= className.hashCode();
218             }
219             if (propertyName != null){
220                 temp+= propertyName.hashCode();
221             }
222             if (value != null){
223                 temp+= value.hashCode();
224             }
225             hashCode = temp;
226             
227             }
228         return hashCode;
229     }
230     public Object JavaDoc getAlias() {
231         return alias;
232     }
233
234     public QueryType getQueryType() {
235         
236         return QUERY_ID;
237     }
238     
239     public IndexQuery setParameter(String JavaDoc name, Object JavaDoc value) {
240         throw new UnsupportedOperationException JavaDoc("Parameters are not supported for convenience classes");
241     }
242     /* (non-Javadoc)
243      * @see com.jofti.api.IndexQuery#setParameter(int, java.lang.Object)
244      */

245     public IndexQuery setParameter(int position, Object JavaDoc value) {
246         throw new UnsupportedOperationException JavaDoc("Parameters are not supported for convenience classes");
247
248     }
249
250     public IndexQuery setFirstResult(int firstResult) {
251         throw new UnsupportedOperationException JavaDoc("Result limitation is not supported for convenience classes");
252     }
253
254     public IndexQuery setMaxResults(int maxResults) {
255         throw new UnsupportedOperationException JavaDoc("Result limitation is not supported for convenience classes");
256     }
257 }
258
Popular Tags