Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/asaph/model/opsemantics/SearchParameters.java


1   /*
2   ================================================================================
3   
4     FILE:  SearchParameters.java
5     
6     PROJECT:
7     
8       Asaph
9     
10    CONTENTS:
11    
12      Description of what to search for
13    
14    PROGRAMMERS:
15    
16      Daniel Azuma (DA)  <dazuma@kagi.com>
17    
18    COPYRIGHT:
19    
20      Copyright (C) 2003  Daniel Azuma  (dazuma@kagi.com)
21      
22      This program is free software; you can redistribute it and/or
23      modify it under the terms of the GNU General Public License as
24      published by the Free Software Foundation; either version 2
25      of the License, or (at your option) any later version.
26      
27      This program is distributed in the hope that it will be useful,
28      but WITHOUT ANY WARRANTY; without even the implied warranty of
29      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30      GNU General Public License for more details.
31      
32      You should have received a copy of the GNU General Public
33      License along with this program; if not, write to
34        Free Software Foundation, Inc.
35        59 Temple Place, Suite 330
36        Boston, MA 02111-1307 USA
37  
38  ================================================================================
39  */
40  
41  
42  package com.virtuosotechnologies.asaph.model.opsemantics;
43  
44  
45  import java.text.Collator;
46  import java.text.CollationKey;
47  
48  import com.virtuosotechnologies.lib.base.UniqueObject;
49  import com.virtuosotechnologies.lib.base.BasicEnumeratedType;
50  
51  
52  /**
53   * Description of what to search for
54   */
55  public class SearchParameters
56  {
57    /**
58     * Strength
59     */
60    public static final class Strength
61    extends BasicEnumeratedType
62    {
63      /*package*/ Strength(
64        String description)
65      {
66        super(description);
67      }
68    }
69    
70    
71    /**
72     * PositionConstraint
73     */
74    public static final class PositionConstraint
75    extends UniqueObject
76    {
77      /*package*/ PositionConstraint(
78        String description)
79      {
80        super(description);
81      }
82    }
83    
84    
85    /**
86     * Strength value indicating that only primary differences are significant.
87     * (e.g. "a" vs "b".)
88     * This is similar to Collator.PRIMARY.
89     */
90    public static final Strength PRIMARY_STRENGTH = new Strength("primary_strength");
91    
92    /**
93     * Strength value indicating that primary and secondary differences are significant.
94     * (e.g. "a" vs "b" and "a" vs "a-umlaut".)
95     * This is similar to Collator.SECONDARY.
96     */
97    public static final Strength SECONDARY_STRENGTH = new Strength("secondary_strength");
98    
99    /**
100    * Strength value indicating that primary, secondary and tertiary differences are significant.
101    * (e.g. "a" vs "b", "a" vs "a-umlaut", and "a" vs "A".)
102    * This is similar to Collator.TERTIARY.
103    */
104   public static final Strength TERTIARY_STRENGTH = new Strength("tertiary_strength");
105   
106   /**
107    * Synonym of PRIMARY_STRENGTH
108    */
109   public static final Strength INSENSITIVE_STRENGTH = PRIMARY_STRENGTH;
110   
111   /**
112    * Synonym of SECONDARY_STRENGTH
113    */
114   public static final Strength ACCENT_SENSITIVE_STRENGTH = SECONDARY_STRENGTH;
115   
116   /**
117    * Synonym of TERTIARY_STRENGTH
118    */
119   public static final Strength CASE_SENSITIVE_STRENGTH = TERTIARY_STRENGTH;
120   
121   
122   /**
123    * Indicates that matches should match the entire value
124    */
125   public static final PositionConstraint EQUALS = new PositionConstraint("equals");
126   
127   /**
128    * Indicates that matches should match "contains"
129    */
130   public static final PositionConstraint CONTAINS = new PositionConstraint("contains");
131   
132   /**
133    * Indicates that matches should match "starts with"
134    */
135   public static final PositionConstraint STARTS_WITH = new PositionConstraint("starts_with");
136   
137   /**
138    * Indicates that matches should match "ends with"
139    */
140   public static final PositionConstraint ENDS_WITH = new PositionConstraint("ends_with");
141   
142   
143   private String searchText_;
144   private Strength strength_;
145   private PositionConstraint positionConstraint_;
146   private Collator collator_;
147   private CollationKey collationKey_;
148   
149   
150   /**
151    * Constructor
152    */
153   public SearchParameters(
154     PositionConstraint position,
155     String text,
156     Strength strength)
157   {
158     positionConstraint_ = position;
159     searchText_ = text;
160     strength_ = strength;
161     collator_ = null;
162     collationKey_ = null;
163   }
164   
165   
166   /**
167    * Get text to search for
168    */
169   public String getText()
170   {
171     return searchText_;
172   }
173   
174   
175   /**
176    * Get the search strength (i.e. case-sensitivity, etc.)
177    */
178   public Strength getStrength()
179   {
180     return strength_;
181   }
182   
183   
184   /**
185    * Get the search position constraint
186    */
187   public PositionConstraint getPositionConstraint()
188   {
189     return positionConstraint_;
190   }
191   
192   
193   /**
194    * Match these search parameters against a string
195    */
196   public boolean matches(
197     String str)
198   {
199     if (collationKey_ == null)
200     {
201       collator_ = Collator.getInstance();
202       collator_.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
203       if (strength_ == PRIMARY_STRENGTH)
204       {
205         collator_.setStrength(Collator.PRIMARY);
206       }
207       else if (strength_ == SECONDARY_STRENGTH)
208       {
209         collator_.setStrength(Collator.SECONDARY);
210       }
211       else
212       {
213         collator_.setStrength(Collator.TERTIARY);
214       }
215       collationKey_ = collator_.getCollationKey(searchText_);
216     }
217     int searchLen = searchText_.length();
218     if (str.length() < searchLen)
219     {
220       return false;
221     }
222     if (positionConstraint_ == EQUALS)
223     {
224       return collator_.getCollationKey(str).equals(collationKey_);
225     }
226     if (positionConstraint_ == STARTS_WITH)
227     {
228       return collator_.getCollationKey(str.substring(0, searchLen)).equals(collationKey_);
229     }
230     if (positionConstraint_ == ENDS_WITH)
231     {
232       return collator_.getCollationKey(str.substring(str.length()-searchLen)).equals(collationKey_);
233     }
234     if (positionConstraint_ == CONTAINS)
235     {
236       for (int i=searchLen; i<=str.length(); ++i)
237       {
238         if (collator_.getCollationKey(str.substring(i-searchLen, i)).equals(collationKey_))
239         {
240           return true;
241         }
242       }
243     }
244     return false;
245   }
246   
247   
248   /**
249    * equals
250    */
251   public boolean equals(
252     Object obj)
253   {
254     if (obj instanceof SearchParameters)
255     {
256       SearchParameters other = (SearchParameters)obj;
257       return other.getText().equals(searchText_) &&
258         other.getStrength().equals(strength_) &&
259         other.getPositionConstraint().equals(positionConstraint_);
260     }
261     return false;
262   }
263   
264   
265   /**
266    * hashCode
267    */
268   public int hashCode()
269   {
270     return searchText_.hashCode() + strength_.hashCode() + positionConstraint_.hashCode();
271   }
272 }