KickJava   Java API By Example, From Geeks To Geeks.

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


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 everything does not match
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  *@author steve Woodcock
23  */

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

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

97     public MatchNSNotQuery(Object JavaDoc nameSpace,Comparable JavaDoc value){
98         this(nameSpace,value,null);
99     }
100     public MatchNSNotQuery(Object JavaDoc nameSpace,Comparable JavaDoc value,Object JavaDoc alias){
101         this.value = value;
102         this.nameSpace = nameSpace;
103         this.alias = alias;
104         
105     }
106     
107     /**
108      * @return Returns the className.
109      */

110     public Class JavaDoc getClassName() {
111         return className;
112     }
113
114     /**
115      * @return Returns the propertyName.
116      */

117     public String JavaDoc getPropertyName() {
118         return propertyName;
119     }
120
121     /**
122      * @return Returns the value.
123      */

124     public Comparable JavaDoc getValue() {
125         return value;
126     }
127
128     
129     
130     
131     /**
132      * @return Returns the wrapper.
133      */

134     public synchronized INameSpace getNameSpaceWrapper() {
135         
136         if (nameSpace instanceof INameSpace){
137             return (INameSpace) nameSpace;
138         }else{
139             return new NameSpaceWrapper(nameSpace);
140         }
141     }
142     
143     /**
144      * @return Returns the nameSpace.
145      */

146     public synchronized Object JavaDoc getNameSpace() {
147         return nameSpace;
148     }
149     /**
150      * @param nameSpace The nameSpace to set.
151      */

152     public synchronized void setNameSpace(Object JavaDoc nameSpace) {
153         this.nameSpace = nameSpace;
154     }
155     
156     public boolean equals(Object JavaDoc o){
157         if (o instanceof MatchNSNotQuery){
158             MatchNSNotQuery temp = (MatchNSNotQuery)o;
159             boolean result = nameSpace.equals(temp.nameSpace);
160             if (!result){
161                 return result;
162             }
163             if (className != null && temp.className != null){
164                 result = className.equals(temp.className);
165                 if (!result){
166                     return result;
167                 }
168             }else {
169                 result = className == null & temp.className == null;
170                 if (!result){
171                     return result;
172                 }
173             }
174     
175             if (propertyName != null && temp.propertyName != null){
176                 result = propertyName.equals(temp.propertyName);
177                 if (!result){
178                     return result;
179                 }
180             }else {
181                 result = propertyName == null & temp.propertyName == null;
182                 if (!result){
183                     return result;
184                 }
185             }
186             if (value != null && temp.value != null){
187                 result = value.equals(temp.value);
188                 if (!result){
189                     return result;
190                 }
191             }else {
192                 result = value == null & temp.value == null;
193                 if (!result){
194                     return result;
195                 }
196             }
197             return result;
198         }
199         return false;
200     }
201     
202     public int hashCode(){
203         int temp = nameSpace.hashCode();
204         if (className != null){
205             temp+= className.hashCode();
206         }
207         if (propertyName != null){
208             temp+= propertyName.hashCode();
209         }
210         if (value != null){
211             temp+= value.hashCode();
212         }
213         return temp;
214         
215         
216     }
217     public Object JavaDoc getAlias() {
218         return alias;
219     }
220     /* (non-Javadoc)
221      * @see com.jofti.core.QueryId#getQueryType()
222      */

223     public QueryType getQueryType() {
224         
225         return QUERY_ID;
226     }
227     
228     public IndexQuery setParameter(String JavaDoc name, Object JavaDoc value) {
229         throw new UnsupportedOperationException JavaDoc("Parameters are not supported for convenience classes");
230     }
231     /* (non-Javadoc)
232      * @see com.jofti.api.IndexQuery#setParameter(int, java.lang.Object)
233      */

234     public IndexQuery setParameter(int position, Object JavaDoc value) {
235         throw new UnsupportedOperationException JavaDoc("Parameters are not supported for convenience classes");
236
237     }
238     public IndexQuery setFirstResult(int firstResult) {
239         throw new UnsupportedOperationException JavaDoc("Result limitation is not supported for convenience classes");
240     }
241     public IndexQuery setMaxResults(int maxResults) {
242         throw new UnsupportedOperationException JavaDoc("Result limitation is not supported for convenience classes");
243     }
244 }
245
Popular Tags