Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / comboboxconfigurablelookup / agents / StartsWithLookUpAgent.java @ 40561

History | View | Annotate | Download (4.17 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.comboboxconfigurablelookup.agents;
25

    
26
import java.util.ArrayList;
27
import java.util.Arrays;
28
import java.util.List;
29
import java.util.Vector;
30

    
31
import org.gvsig.gui.beans.comboboxconfigurablelookup.ILookUp;
32
import org.gvsig.gui.beans.comboboxconfigurablelookup.StringComparator;
33

    
34
/**
35
 * <p>Agent that looks up items of an locale-rules alphabetically sort ordered <code>Vector</code> that
36
 *  start with a text. Those items will be returned as a {@link List List}.</p>
37
 * 
38
 * <p>Supports two versions: with or without considering case sensitive.</p>
39
 * 
40
 * @version 07/02/2008
41
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es) 
42
 */
43
public class StartsWithLookUpAgent implements ILookUp {
44
        /**
45
         * <p>Creates a new instance of the class <code>StartsWithLookUpAgent</code>.</p>
46
         */
47
        public StartsWithLookUpAgent() {
48
        }
49

    
50
        /*
51
         * (non-Javadoc)
52
         * @see org.gvsig.gui.beans.comboboxconfigurablelookup.ILookUp#doLookUpConsideringCaseSensitive(java.lang.String, java.util.Vector, org.gvsig.gui.beans.comboboxconfigurablelookup.StringComparator)
53
         */
54
        public synchronized List<Object> doLookUpConsideringCaseSensitive(String text, Vector<Object> sortOrderedItems, StringComparator comp) {
55
                if (text == null)
56
                        return null;
57
                
58
                List<Object> results_list = doLookUpIgnoringCaseSensitive(text, sortOrderedItems, comp);
59

    
60
                if (results_list == null)
61
                        return null;
62

    
63
                List<Object> results = new ArrayList<Object>();
64

    
65
                for (int i = 0; i < (results_list.size()); i++) {
66
                        if (results_list.get(i).toString().startsWith(text)) {
67
                                results.add(results_list.get(i));
68
                        }
69
                }
70

    
71
                return results;
72
        }
73

    
74
        /*
75
         * (non-Javadoc)
76
         * @see org.gvsig.gui.beans.comboboxconfigurablelookup.ILookUp#doLookUpIgnoringCaseSensitive(java.lang.String, java.util.Vector, org.gvsig.gui.beans.comboboxconfigurablelookup.StringComparator)
77
         */
78
        public synchronized List<Object> doLookUpIgnoringCaseSensitive(String text, Vector<Object> sortOrderedItems, StringComparator comp) {
79
                if (text == null)
80
                        return null;
81
                
82
                int currentIteration = 0;
83
                int size = sortOrderedItems.size();
84
                int maxNumberOfIterations = (int) (Math.log(size) / Math.log(2));
85
                int lowIndex = 0;
86
                int highIndex = sortOrderedItems.size() - 1;
87
                int mIndx;
88
                
89
                while ((lowIndex <= highIndex) && (currentIteration <= maxNumberOfIterations)) {
90
                        mIndx = ( lowIndex + highIndex ) / 2;
91

    
92
                        if ( sortOrderedItems.get( mIndx ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) {
93
                                lowIndex = highIndex = mIndx;
94
                                highIndex ++;
95
                                
96
                                // Expand the rank to up
97
                                while ( ( highIndex < size ) && ( sortOrderedItems.get( highIndex ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
98
                                        highIndex ++;
99
                                }
100

    
101
                                // Expand the rank to down
102
                                while ( ( (lowIndex - 1) > -1 ) && ( sortOrderedItems.get( (lowIndex - 1) ).toString().toLowerCase().startsWith( text.toLowerCase() ) ) ) {
103
                                        lowIndex --;
104
                                }
105
                                
106
                                // Returns all items in the rank                        
107
                                return Arrays.asList((sortOrderedItems.subList(lowIndex, highIndex)).toArray()); // Breaks the loop
108
                        }
109
                        else {
110
                                if ( comp.compare(sortOrderedItems.get( mIndx ).toString().toLowerCase(), text.toLowerCase() ) > 0 ) {
111
                                        highIndex = mIndx - 1;
112
                                }
113
                                else {
114
                                        lowIndex = mIndx + 1;
115
                                }
116
                        }
117
                        
118
                        currentIteration ++;
119
                }
120
                
121
                return null;
122
        }
123
}