KickJava   Java API By Example, From Geeks To Geeks.

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


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.bridge.Field;
14 import org.mmbase.storage.search.*;
15 import org.mmbase.storage.search.StringSearchConstraint;
16
17 /**
18  * Basic implementation.
19  *
20  * @author Rob van Maris
21  * @version $Id: BasicStringSearchConstraint.java,v 1.8 2005/07/08 12:23:45 pierre Exp $
22  * @since MMBase-1.7
23  */

24 public class BasicStringSearchConstraint extends BasicFieldConstraint implements StringSearchConstraint {
25
26     /** The search type. */
27     private int searchType = 0;
28
29     /** The match type. */
30     private int matchType = 0;
31
32     /** Map storing additional parameters. */
33     private Map parameters = new HashMap(3);
34
35     /** List of searchterms. */
36     private List searchTerms = null;
37
38     /**
39      * Creates a new instance of BasicStringSearchConstraint.
40      *
41      * @param field The associated field.
42      * @param searchType The search type.
43      * @param matchType The match type.
44      * @param searchTerms the searchterms
45      * @throws IllegalArgumentValue when an invalid argument is supplied.
46      * @see #getSearchType
47      * @see #getMatchType
48      */

49     public BasicStringSearchConstraint(StepField field, int searchType,
50     int matchType, List searchTerms) {
51         this(field, searchType, matchType);
52         setSearchTerms(searchTerms);
53     }
54
55     /**
56      * Creates a new instance of BasicStringSearchConstraint.
57      *
58      * @param field The associated field.
59      * @param searchType The search type.
60      * @param matchType The match type.
61      * @param searchTerms String containing searchterms as words separated
62      * by white space.
63      * @throws IllegalArgumentValue when an invalid argument is supplied.
64      * @see #getSearchType
65      * @see #getMatchType
66      */

67     public BasicStringSearchConstraint(StepField field, int searchType,
68     int matchType, String JavaDoc searchTerms) {
69         this(field, searchType, matchType);
70         setSearchTerms(searchTerms);
71     }
72
73     /**
74      * Creates a new instance of BasicStringSearchConstraint.
75      * Private, is to be called from all other creators.
76      *
77      * @param field The associated field.
78      * @param searchType The search type.
79      * @param matchType The match type.
80      * @throws IllegalArgumentValue when an invalid argument is supplied.
81      * @see #getSearchType
82      * @see #getMatchType
83      */

84     private BasicStringSearchConstraint(StepField field, int searchType,
85     int matchType) {
86         super(field);
87         if (field.getType() != Field.TYPE_STRING
88         && field.getType() != Field.TYPE_XML) {
89             throw new IllegalArgumentException JavaDoc(
90             "StringSearchConstraint not allowed for this field type: "
91             + getField().getType());
92         }
93         setSearchType(searchType);
94         setMatchType(matchType);
95     }
96
97     /**
98      * Sets the match type.
99      *
100      * @param matchType The matchtype.
101      * @return This <code>BasicStringSearchConstraint</code> instance.
102      * @throws IllegalArgumentValue when an invalid argument is supplied.
103      * @see #getMatchType
104      */

105     public BasicStringSearchConstraint setMatchType(int matchType) {
106         if (matchType != StringSearchConstraint.MATCH_TYPE_LITERAL
107         && matchType != StringSearchConstraint.MATCH_TYPE_FUZZY
108         && matchType != StringSearchConstraint.MATCH_TYPE_SYNONYM) {
109             throw new IllegalArgumentException JavaDoc(
110             "Invalid match type value: " + matchType);
111         }
112         this.matchType = matchType;
113         if (matchType != StringSearchConstraint.MATCH_TYPE_FUZZY) {
114             parameters.remove(StringSearchConstraint.PARAM_FUZZINESS);
115         }
116         return this;
117     }
118
119     /**
120      * Sets the search type.
121      *
122      * @param searchType The searchType.
123      * @return This <code>BasicStringSearchConstraint</code> instance.
124      * @throws IllegalArgumentValue when an invalid argument is supplied.
125      * @see #getSearchType
126      */

127     public BasicStringSearchConstraint setSearchType(int searchType) {
128         if (searchType != StringSearchConstraint.SEARCH_TYPE_WORD_ORIENTED
129         && searchType != StringSearchConstraint.SEARCH_TYPE_PHRASE_ORIENTED
130         && searchType != StringSearchConstraint.SEARCH_TYPE_PROXIMITY_ORIENTED) {
131             throw new IllegalArgumentException JavaDoc(
132             "Invalid search type value: " + searchType);
133         }
134         this.searchType = searchType;
135         if (searchType != StringSearchConstraint.SEARCH_TYPE_PROXIMITY_ORIENTED) {
136             parameters.remove(StringSearchConstraint.PARAM_PROXIMITY_LIMIT);
137         }
138         return this;
139     }
140
141     /**
142      * Adds searchterm to list of searchterms.
143      *
144      * @param searchTerm the searchterms
145      * @return This <code>BasicStringSearchConstraint</code> instance.
146      * @throws IllegalArgumentException when an invalid argument is supplied.
147      */

148     public BasicStringSearchConstraint addSearchTerm(String JavaDoc searchTerm) {
149         if (searchTerm.trim().length() == 0) {
150             throw new IllegalArgumentException JavaDoc(
151             "Invalid search term value: \"" + searchTerm + "\"");
152         }
153         searchTerms.add(searchTerm);
154         return this;
155     }
156
157     /**
158      * Sets searchterms to elements in specified list.
159      *
160      * @param searchTerms the searchterms
161      * @return This <code>BasicStringSearchConstraint</code> instance.
162      * @throws IllegalArgumentException when an invalid argument is supplied.
163      */

164     public BasicStringSearchConstraint setSearchTerms(List searchTerms) {
165         if (searchTerms.size() == 0) {
166             throw new IllegalArgumentException JavaDoc(
167             "Invalid search terms value: " + searchTerms);
168         }
169         List newSearchTerms = new ArrayList();
170         Iterator iSearchTerms = searchTerms.iterator();
171         while (iSearchTerms.hasNext()) {
172             Object JavaDoc searchTerm = iSearchTerms.next();
173             if (!(searchTerm instanceof String JavaDoc)) {
174                 throw new IllegalArgumentException JavaDoc(
175                 "Invalid search term value: " + searchTerm);
176             }
177             newSearchTerms.add(searchTerm);
178         }
179         this.searchTerms = newSearchTerms;
180         return this;
181     }
182
183     /**
184      * Sets searchterms to searchterms in string.
185      *
186      * @param searchTerms String containing searchterms as words separated
187      * by white space.
188      * @return This <code>BasicStringSearchConstraint</code> instance.
189      * @throws IllegalArgumentException when an invalid argument is supplied.
190      */

191     public BasicStringSearchConstraint setSearchTerms(String JavaDoc searchTerms) {
192         if (searchTerms.trim().length() == 0) {
193             throw new IllegalArgumentException JavaDoc(
194             "Invalid search terms value: \"" + searchTerms + "\"");
195         }
196         List newSearchTerms = new ArrayList();
197         StringTokenizer st = new StringTokenizer(searchTerms);
198         while (st.hasMoreTokens()) {
199             newSearchTerms.add(st.nextToken());
200         }
201         this.searchTerms = newSearchTerms;
202         return this;
203     }
204
205     /**
206      * Sets parameter. Ignored if parameter is not relavant to the present
207      * search- and matchtype.
208      *
209      * @param name The parameter name.
210      * @param value The parameter value.
211      * @return This <code>BasicStringSearchConstraint</code> instance.
212      * @throws IllegalArgumentValue when an invalid argument is supplied.
213      * @see #getParameters
214      */

215     public BasicStringSearchConstraint setParameter(String JavaDoc name, Object JavaDoc value) {
216         if (name.equals(StringSearchConstraint.PARAM_FUZZINESS)
217         && matchType == StringSearchConstraint.MATCH_TYPE_FUZZY) {
218             if (!(value instanceof Float JavaDoc)) {
219                 throw new IllegalArgumentException JavaDoc(
220                 "Invalid type for parameter \"" + name + "\": "
221                 + value.getClass().getName());
222             }
223             float floatValue = ((Float JavaDoc) value).floatValue();
224             if (floatValue < 0 || floatValue > 1) {
225                 throw new IllegalArgumentException JavaDoc(
226                 "Invalid fuzziness value: " + floatValue);
227             }
228         } else if (name.equals(StringSearchConstraint.PARAM_PROXIMITY_LIMIT)
229         && searchType == StringSearchConstraint.SEARCH_TYPE_PROXIMITY_ORIENTED) {
230             if (!(value instanceof Integer JavaDoc)) {
231                 throw new IllegalArgumentException JavaDoc(
232                 "Invalid type for parameter \"" + name + "\": "
233                 + value.getClass().getName());
234             }
235             int intValue = ((Integer JavaDoc) value).intValue();
236             if (intValue < 1) {
237                 throw new IllegalArgumentException JavaDoc(
238                 "Invalid proximity limit value: " + intValue);
239             }
240         } else {
241             throw new IllegalArgumentException JavaDoc(
242             "Invalid parameter name: \"" + name + "\"");
243         }
244         parameters.put(name, value);
245         return this;
246     }
247
248     // javadoc is inherited
249
public Map getParameters() {
250         return Collections.unmodifiableMap(parameters);
251     }
252
253     // javadoc is inherited
254
public int getMatchType() {
255         return matchType;
256     }
257
258     /**
259      * Returns a description of the match type
260      */

261     public String JavaDoc getMatchTypeDescription() {
262         try {
263             return StringSearchConstraint.MATCH_TYPE_DESCRIPTIONS[matchType];
264         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
265             return null;
266         }
267     }
268
269     // javadoc is inherited
270
public int getSearchType() {
271         return searchType;
272     }
273
274     /**
275      * Returns a description of the search type
276      */

277     public String JavaDoc getSearchTypeDescription() {
278         try {
279             return StringSearchConstraint.SEARCH_TYPE_DESCRIPTIONS[searchType];
280         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
281             return null;
282         }
283     }
284
285     // javadoc is inherited
286
public List getSearchTerms() {
287         return Collections.unmodifiableList(searchTerms);
288     }
289
290     // javadoc is inherited
291
public int getBasicSupportLevel() {
292         // no basic support
293
return SearchQueryHandler.SUPPORT_NONE;
294     }
295
296     // javadoc is inherited
297
public boolean equals(Object JavaDoc obj) {
298         // Must be same class (subclasses should override this)!
299
if (obj != null && obj.getClass() == getClass()) {
300             BasicStringSearchConstraint constraint = (BasicStringSearchConstraint) obj;
301             return isInverse() == constraint.isInverse()
302                 && isCaseSensitive() == constraint.isCaseSensitive()
303                 && getField().getFieldName().equals(constraint.getField().getFieldName())
304                 && getField().getStep().getAlias().equals(
305                     constraint.getField().getStep().getAlias())
306                 && searchType == constraint.getSearchType()
307                 && matchType == constraint.getMatchType()
308                 && parameters.equals(constraint.parameters)
309                 && searchTerms.equals(constraint.searchTerms);
310         } else {
311             return false;
312         }
313     }
314
315     // javadoc is inherited
316
public int hashCode() {
317         return super.hashCode()
318         + 117 * searchType
319         + 127 * matchType
320         + 131 * parameters.hashCode()
321         + 137 + searchTerms.hashCode();
322     }
323
324     // javadoc is inherited
325
public String JavaDoc toString() {
326         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("StringSearchConstraint(inverse:").
327         append(isInverse()).
328         append("field:").append(getFieldName()).
329         append(", casesensitive:").append(isCaseSensitive()).
330         append(", searchtype:").append(getSearchTypeDescription()).
331         append(", matchtype:").append(getMatchTypeDescription()).
332         append(", parameters:").append(parameters).
333         append(", searchterms:").append(searchTerms).
334         append(")");
335         return sb.toString();
336     }
337 }
338
Popular Tags