|
1 |
package org.gvsig.gui.beans.swing.jComboBoxItemsSeeker;
|
|
2 |
|
|
3 |
import java.util.ArrayList;
|
|
4 |
import java.util.Arrays;
|
|
5 |
import java.util.Comparator;
|
|
6 |
import java.util.List;
|
|
7 |
import java.util.Vector;
|
|
8 |
|
|
9 |
|
|
10 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
|
11 |
*
|
|
12 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
|
13 |
*
|
|
14 |
* This program is free software; you can redistribute it and/or
|
|
15 |
* modify it under the terms of the GNU General Public License
|
|
16 |
* as published by the Free Software Foundation; either version 2
|
|
17 |
* of the License, or (at your option) any later version.
|
|
18 |
*
|
|
19 |
* This program is distributed in the hope that it will be useful,
|
|
20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22 |
* GNU General Public License for more details.
|
|
23 |
*
|
|
24 |
* You should have received a copy of the GNU General Public License
|
|
25 |
* along with this program; if not, write to the Free Software
|
|
26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
|
27 |
*
|
|
28 |
* For more information, contact:
|
|
29 |
*
|
|
30 |
* Generalitat Valenciana
|
|
31 |
* Conselleria d'Infraestructures i Transport
|
|
32 |
* Av. Blasco Ib??ez, 50
|
|
33 |
* 46010 VALENCIA
|
|
34 |
* SPAIN
|
|
35 |
*
|
|
36 |
* +34 963862235
|
|
37 |
* gvsig@gva.es
|
|
38 |
* www.gvsig.gva.es
|
|
39 |
*
|
|
40 |
* or
|
|
41 |
*
|
|
42 |
* IVER T.I. S.A
|
|
43 |
* Salamanca 50
|
|
44 |
* 46005 Valencia
|
|
45 |
* Spain
|
|
46 |
*
|
|
47 |
* +34 963163400
|
|
48 |
* dac@iver.es
|
|
49 |
*/
|
|
50 |
|
|
51 |
/**
|
|
52 |
* This class has static methods that return items that their beginning text value matches with a text pattern. <br>
|
|
53 |
* It's necessary that items of the parameter array (Vector) were sort ordered according to their text value. <br>
|
|
54 |
* Supports Vectors with and without repeated items.
|
|
55 |
*
|
|
56 |
* There are four methods, that are a modification of a binary search algorithm of search, getting a rank of items:
|
|
57 |
* <ul>
|
|
58 |
* <li>Considering case sensitive in the search.</li>
|
|
59 |
* <li>Ignoring case sensitive in the search.</li>
|
|
60 |
* <li>Considering case sensitive in the search and an object which implements the Comparable interface</li>
|
|
61 |
* <li>Ignoring case sensitive in the search, but yes an objecth which implements the Comparable interface</li>
|
|
62 |
* </ul>
|
|
63 |
*
|
|
64 |
* This class is a copy of a class with the same name located in <i>libIverUtiles</i>
|
|
65 |
*
|
|
66 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
|
67 |
*/
|
|
68 |
public class BinarySearchUsingFirstCharacters {
|
|
69 |
/**
|
|
70 |
* This method should be used when is wanted to distinguish small letters from capital letters during the search.
|
|
71 |
*
|
|
72 |
* It's necessary that all items of the array implement the {@link Comparable} interface.<br>
|
|
73 |
* And it's also necessary that the value returned by the <i>toString()</i> method of each item (supposing
|
|
74 |
* they inherit from Object) would be the expected value user saw (that would be used to compare the items).
|
|
75 |
*
|
|
76 |
* @param text java.lang.String
|
|
77 |
* @param sortOrderedItems java.util.Vector
|
|
78 |
*/
|
|
79 |
public synchronized static List doSearchConsideringCaseSensitive(String text, Vector sortOrderedItems) {
|
|
80 |
int currentIteration = 0;
|
|
81 |
int size = sortOrderedItems.size();
|
|
82 |
int maxNumberOfIterations = (int) MathExtensionReduced.log2(size);
|
|
83 |
int lowIndex = 0;
|
|
84 |
int highIndex = sortOrderedItems.size() - 1;
|
|
85 |
int mIndx;
|
|
86 |
|
|
87 |
while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
|
|
88 |
mIndx = ( lowIndex + highIndex ) / 2;
|
|
89 |
|
|
90 |
if ( sortOrderedItems.get( mIndx ).toString().startsWith( text ) ) {
|
|
91 |
lowIndex = highIndex = mIndx;
|
|
92 |
highIndex ++;
|
|
93 |
|
|
94 |
// Expand the rank to up
|
|
95 |
while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().startsWith( text ) ) ) {
|
|
96 |
highIndex ++;
|
|
97 |
}
|
|
98 |
|
|
99 |
// Expand the rank to down
|
|
100 |
while ( ( (lowIndex - 1) > -1 ) && ( sortOrderedItems.get( (lowIndex - 1) ).toString().startsWith( text ) ) ) {
|
|
101 |
lowIndex --;
|
|
102 |
}
|
|
103 |
|
|
104 |
// It's possible that items with different case, should be between the same case, then this item will be added individually:
|
|
105 |
List list = new Vector(sortOrderedItems.subList(lowIndex, highIndex));
|
|
106 |
|
|
107 |
// Expand to down
|
|
108 |
lowIndex --;
|
|
109 |
while ( ( lowIndex > -1 ) && ( sortOrderedItems.get( (lowIndex ) ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
110 |
if (sortOrderedItems.get( lowIndex ).toString().startsWith( text )) {
|
|
111 |
list.add(0, sortOrderedItems.get( lowIndex ));
|
|
112 |
}
|
|
113 |
|
|
114 |
lowIndex --;
|
|
115 |
}
|
|
116 |
|
|
117 |
// Expand to up
|
|
118 |
while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
119 |
if (sortOrderedItems.get( highIndex ).toString().startsWith( text )) {
|
|
120 |
list.add(list.size(), sortOrderedItems.get( highIndex ));
|
|
121 |
}
|
|
122 |
|
|
123 |
highIndex ++;
|
|
124 |
}
|
|
125 |
|
|
126 |
// Returns all items in the rank
|
|
127 |
return list; // Breaks the loop
|
|
128 |
}
|
|
129 |
else {
|
|
130 |
if ( sortOrderedItems.get( mIndx ).toString().compareTo( text ) > 0 ) {
|
|
131 |
highIndex = mIndx - 1;
|
|
132 |
}
|
|
133 |
else {
|
|
134 |
lowIndex = mIndx + 1;
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
currentIteration ++;
|
|
139 |
}
|
|
140 |
|
|
141 |
// If no item has been found -> return null
|
|
142 |
return null;
|
|
143 |
}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* This method should be used when is wanted not to distinguish small letters from capital letters during the search.
|
|
147 |
*
|
|
148 |
* It's necessary that all items of the array implement the {@link Comparable} interface.<br>
|
|
149 |
* And it's also necessary that the value returned by the <i>toString()</i> method of each item (supposing
|
|
150 |
* they inherit from Object) would be the expected value user saw (that would be used to compare the items).
|
|
151 |
*
|
|
152 |
* In this particular situation, it's supposed that the vector is sort ordered according the default algorithm of Java; this has the problem that
|
|
153 |
* it doesn't consideres the special characters and the orthographic rules of languages that aren't English, and then, for a particular
|
|
154 |
* <i>text</i> search, an incorrect result could be obtained. The solution decided for this problem has been to modify the algorithm, for seach
|
|
155 |
* into two ranks.
|
|
156 |
*
|
|
157 |
* @param text java.lang.String
|
|
158 |
* @param sortOrderedItems java.util.Vector
|
|
159 |
*/
|
|
160 |
public synchronized static List doSearchIgnoringCaseSensitive(String text, Vector sortOrderedItems) {
|
|
161 |
int currentIteration = 0;
|
|
162 |
int size = sortOrderedItems.size();
|
|
163 |
int maxNumberOfIterations = (int) MathExtensionReduced.log2(size);
|
|
164 |
int lowIndex = 0;
|
|
165 |
int highIndex = sortOrderedItems.size() - 1;
|
|
166 |
int mIndx;
|
|
167 |
List list = null;
|
|
168 |
List list2 = null;
|
|
169 |
|
|
170 |
// FIRST RANK
|
|
171 |
while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
|
|
172 |
mIndx = ( lowIndex + highIndex ) / 2;
|
|
173 |
|
|
174 |
if ( sortOrderedItems.get( mIndx ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) {
|
|
175 |
lowIndex = highIndex = mIndx;
|
|
176 |
highIndex ++;
|
|
177 |
|
|
178 |
// Expand the rank to up
|
|
179 |
while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
180 |
highIndex ++;
|
|
181 |
}
|
|
182 |
|
|
183 |
// Expand the rank to down
|
|
184 |
while ( ( (lowIndex - 1) > -1 ) && ( sortOrderedItems.get( (lowIndex - 1) ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
185 |
lowIndex --;
|
|
186 |
}
|
|
187 |
|
|
188 |
// Returns all items in the rank
|
|
189 |
list = Arrays.asList((sortOrderedItems.subList(lowIndex, highIndex)).toArray());
|
|
190 |
|
|
191 |
// list = new ArrayList(Arrays.asList((sortOrderedItems.subList(lowIndex, highIndex)).toArray())); // Breaks the loop
|
|
192 |
// break;
|
|
193 |
}
|
|
194 |
else {
|
|
195 |
if ( sortOrderedItems.get( mIndx ).toString().compareTo( text ) > 0 ) {
|
|
196 |
highIndex = mIndx - 1;
|
|
197 |
}
|
|
198 |
else {
|
|
199 |
lowIndex = mIndx + 1;
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
currentIteration ++;
|
|
204 |
}
|
|
205 |
|
|
206 |
// SECOND RANK
|
|
207 |
currentIteration = 0;
|
|
208 |
lowIndex = 0;
|
|
209 |
highIndex = sortOrderedItems.size() - 1;
|
|
210 |
|
|
211 |
while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
|
|
212 |
mIndx = ( lowIndex + highIndex ) / 2;
|
|
213 |
|
|
214 |
if ( sortOrderedItems.get( mIndx ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) {
|
|
215 |
lowIndex = highIndex = mIndx;
|
|
216 |
highIndex ++;
|
|
217 |
|
|
218 |
// Expand the rank to up
|
|
219 |
while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
220 |
highIndex ++;
|
|
221 |
}
|
|
222 |
|
|
223 |
// Expand the rank to down
|
|
224 |
while ( ( (lowIndex - 1) > -1 ) && ( sortOrderedItems.get( (lowIndex - 1) ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
225 |
lowIndex --;
|
|
226 |
}
|
|
227 |
|
|
228 |
// Returns all items in the rank
|
|
229 |
list2 = Arrays.asList((sortOrderedItems.subList(lowIndex, highIndex)).toArray()); // Breaks the loop
|
|
230 |
|
|
231 |
if (list == null)
|
|
232 |
return list2;
|
|
233 |
else {
|
|
234 |
if (list2 == null)
|
|
235 |
return null;
|
|
236 |
|
|
237 |
Object obj;
|
|
238 |
int j;
|
|
239 |
|
|
240 |
list = new ArrayList(list.subList(0, list.size()));
|
|
241 |
|
|
242 |
for (int i = 0; i < list2.size(); i ++) {
|
|
243 |
obj = list2.get(i);
|
|
244 |
|
|
245 |
// Don't add items which are already in the list
|
|
246 |
if (!list.contains(obj)) {
|
|
247 |
// Adds in sort order the new item:
|
|
248 |
for (j = 0; j < list.size(); j ++) {
|
|
249 |
if (list.get(j).toString().compareTo(obj.toString()) > 0)
|
|
250 |
break;
|
|
251 |
}
|
|
252 |
|
|
253 |
list.add(j, obj);
|
|
254 |
System.out.println("A?ade: " + obj);
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
return list;
|
|
259 |
}
|
|
260 |
}
|
|
261 |
else {
|
|
262 |
if ( sortOrderedItems.get( mIndx ).toString().toLowerCase().compareTo( text.toLowerCase() ) > 0 ) {
|
|
263 |
highIndex = mIndx - 1;
|
|
264 |
}
|
|
265 |
else {
|
|
266 |
lowIndex = mIndx + 1;
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
currentIteration ++;
|
|
271 |
}
|
|
272 |
|
|
273 |
return null;
|
|
274 |
}
|
|
275 |
|
|
276 |
/**
|
|
277 |
* This method should be used when is wanted distinguish small letters from capital letters during the search, and the comparation of items
|
|
278 |
* done according an algorithm we define.
|
|
279 |
*
|
|
280 |
* And it's also necessary that the value returned by the <i>toString()</i> method of each item (supposing
|
|
281 |
* they inherit from Object) would be the expected value user saw (that would be used to compare the items).
|
|
282 |
*
|
|
283 |
* @param text java.lang.String
|
|
284 |
* @param sortOrderedItems java.util.Vector
|
|
285 |
* @param comp An Comparator object which implements the <i><b>compareTo()</b><i> method.
|
|
286 |
*/
|
|
287 |
public synchronized static List doSearchConsideringCaseSensitive(String text, Vector sortOrderedItems, Comparator comp) {
|
|
288 |
int currentIteration = 0;
|
|
289 |
int size = sortOrderedItems.size();
|
|
290 |
int maxNumberOfIterations = (int) MathExtensionReduced.log2(size);
|
|
291 |
int lowIndex = 0;
|
|
292 |
int highIndex = sortOrderedItems.size() - 1;
|
|
293 |
int mIndx;
|
|
294 |
|
|
295 |
|
|
296 |
while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
|
|
297 |
mIndx = ( lowIndex + highIndex ) / 2;
|
|
298 |
|
|
299 |
if ( sortOrderedItems.get( mIndx ).toString().startsWith( text ) ) {
|
|
300 |
lowIndex = highIndex = mIndx;
|
|
301 |
highIndex ++;
|
|
302 |
|
|
303 |
// Expand the rank to up
|
|
304 |
while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().startsWith( text ) ) ) {
|
|
305 |
highIndex ++;
|
|
306 |
}
|
|
307 |
|
|
308 |
// Expand the rank to down
|
|
309 |
while ( ( (lowIndex - 1) > -1 ) && ( sortOrderedItems.get( (lowIndex - 1) ).toString().startsWith( text ) ) ) {
|
|
310 |
lowIndex --;
|
|
311 |
}
|
|
312 |
|
|
313 |
// It's possible that items with different case, should be between the same case, then this item will be added individually:
|
|
314 |
List list = new Vector(sortOrderedItems.subList(lowIndex, highIndex));
|
|
315 |
|
|
316 |
|
|
317 |
// Expand to down
|
|
318 |
lowIndex --;
|
|
319 |
while ( ( lowIndex > -1 ) && ( sortOrderedItems.get( (lowIndex ) ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
320 |
if (sortOrderedItems.get( lowIndex ).toString().startsWith( text )) {
|
|
321 |
list.add(0, sortOrderedItems.get( lowIndex ));
|
|
322 |
}
|
|
323 |
|
|
324 |
lowIndex --;
|
|
325 |
}
|
|
326 |
|
|
327 |
// Expand to up
|
|
328 |
while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
329 |
if (sortOrderedItems.get( highIndex ).toString().startsWith( text )) {
|
|
330 |
list.add(list.size(), sortOrderedItems.get( highIndex ));
|
|
331 |
}
|
|
332 |
|
|
333 |
highIndex ++;
|
|
334 |
}
|
|
335 |
|
|
336 |
// Returns all items in the rank
|
|
337 |
return list; // Breaks the loop
|
|
338 |
}
|
|
339 |
else {
|
|
340 |
if ( comp.compare(sortOrderedItems.get( mIndx ), text ) > 0 ) {
|
|
341 |
highIndex = mIndx - 1;
|
|
342 |
}
|
|
343 |
else {
|
|
344 |
lowIndex = mIndx + 1;
|
|
345 |
}
|
|
346 |
}
|
|
347 |
|
|
348 |
currentIteration ++;
|
|
349 |
}
|
|
350 |
|
|
351 |
// If no item has been found -> return null
|
|
352 |
return null;
|
|
353 |
}
|
|
354 |
|
|
355 |
/**
|
|
356 |
* This method should be used when is wanted not to distinguish small letters from capital letters during the search, and the comparation of items
|
|
357 |
* done according an algorithm we define.
|
|
358 |
*
|
|
359 |
* And it's also necessary that the value returned by the <i>toString()</i> method of each item (supposing
|
|
360 |
* they inherit from Object) would be the expected value user saw (that would be used to compare the items).
|
|
361 |
*
|
|
362 |
* @param text java.lang.String
|
|
363 |
* @param sortOrderedItems java.util.Vector
|
|
364 |
* @param comp An Comparator object which implements the <i><b>compareTo()</b><i> method.
|
|
365 |
*/
|
|
366 |
public synchronized static List doSearchIgnoringCaseSensitive(String text, Vector sortOrderedItems, Comparator comp) {
|
|
367 |
int currentIteration = 0;
|
|
368 |
int size = sortOrderedItems.size();
|
|
369 |
int maxNumberOfIterations = (int) MathExtensionReduced.log2(size);
|
|
370 |
int lowIndex = 0;
|
|
371 |
int highIndex = sortOrderedItems.size() - 1;
|
|
372 |
int mIndx;
|
|
373 |
|
|
374 |
while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
|
|
375 |
mIndx = ( lowIndex + highIndex ) / 2;
|
|
376 |
|
|
377 |
if ( sortOrderedItems.get( mIndx ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) {
|
|
378 |
lowIndex = highIndex = mIndx;
|
|
379 |
highIndex ++;
|
|
380 |
|
|
381 |
// Expand the rank to up
|
|
382 |
while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
383 |
highIndex ++;
|
|
384 |
}
|
|
385 |
|
|
386 |
// Expand the rank to down
|
|
387 |
while ( ( (lowIndex - 1) > -1 ) && ( sortOrderedItems.get( (lowIndex - 1) ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
|
|
388 |
lowIndex --;
|
|
389 |
}
|
|
390 |
|
|
391 |
// Returns all items in the rank
|
|
392 |
return Arrays.asList((sortOrderedItems.subList(lowIndex, highIndex)).toArray()); // Breaks the loop
|
|
393 |
}
|
|
394 |
else {
|
|
395 |
if ( comp.compare(sortOrderedItems.get( mIndx ).toString().toLowerCase(), text.toLowerCase() ) > 0 ) {
|
|
396 |
highIndex = mIndx - 1;
|
|
397 |
}
|
|
398 |
else {
|
|
399 |
lowIndex = mIndx + 1;
|
|
400 |
}
|
|
401 |
}
|
|
402 |
|
|
403 |
currentIteration ++;
|
|
404 |
}
|
|
405 |
|
|
406 |
return null;
|
|
407 |
}
|
|
408 |
}
|
0 |
409 |
|