Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / StringComparator.java @ 40561

History | View | Annotate | Download (5.23 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.utils;
25

    
26
import java.text.Collator;
27
import java.util.Comparator;
28

    
29
/**
30
 * Compares two chain of characters alphabetically
31
 *
32
 * @author Fernando Gonz?lez Cort?s
33
 * @author Pablo Piqueras Bartolom?
34
 */
35
public class StringComparator implements Comparator {
36
        protected boolean caseSensitive = true;
37
        protected LocaleRules localeRules = null;
38

    
39
    /**
40
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
41
     */
42
    public int compare(Object o1, Object o2) {
43
        String s1 = o1.toString();
44
        String s2 = o2.toString();
45

    
46
        // If localeRules is null -> use the default rules
47
        if (localeRules == null) {
48
                if (caseSensitive) {
49
                        return s1.compareTo(s2);
50
                }
51
                else {
52
                        return s1.compareToIgnoreCase(s2);
53
                }
54
        }
55
        else {
56
                if (localeRules.isUseLocaleRules()) {
57
                        Collator collator = localeRules.getCollator();
58
                        
59
                        if (caseSensitive) {
60
                                return collator.compare(s1, s2);
61
                        }
62
                        else {
63
                                //return collator.compare(s1.toLowerCase(), s2.toLowerCase());
64
                                return collator.compare(s1.toUpperCase(), s2.toUpperCase());
65
                        }
66
                }
67
                else {
68
                    if (caseSensitive) {
69
                            return s1.compareTo(s2);
70
                    }
71
                    else {
72
                            return s1.compareToIgnoreCase(s2);
73
                    }
74
                }
75
        }
76
    }
77

    
78
    /**
79
     * Returns if the comparator is sensitive to small and big letters
80
     *
81
     * @return
82
     */
83
    public boolean isCaseSensitive() {
84
        return caseSensitive;
85
    }
86

    
87
    /**
88
     * Establece la sensibilidad del comparador a las mayusculas y minusculas
89
     *
90
     * @param b
91
     */
92
    public void setCaseSensitive(boolean b) {
93
        caseSensitive = b;
94
    }
95
    
96
    /**
97
     * Gets an object with the information for use the locale rules in comparation between strings. <br>
98
     * <ul>
99
     * <li>A boolean value -> if want or not use the locale rules</li>
100
     * <li>A reference to the locale rules</li>
101
     * </ul>
102
     * 
103
     * @return @see LocaleRules
104
     */
105
    public LocaleRules getLocaleRules() {
106
            return localeRules;
107
    }    
108
    /**
109
     * Sets an object with the information for use the locale rules in comparation between strings. <br>
110
     * <ul>
111
     * <li>A boolean value -> if want or not use the locale rules</li>
112
     * <li>A reference to the locale rules</li>
113
     * </ul>
114
     * 
115
     * @param @see LocaleRules
116
     */
117
    public void setLocaleRules(LocaleRules locRules) {
118
            localeRules = locRules;
119
    }
120
    
121
    /**
122
     * Represents the information needed by <i>StringComparator</i> for use or not locale-sensitive String comparison-rules in the <b><i>compare</i></b> method
123
     * 
124
     * @author Pablo Piqueras Bartolom?
125
     */
126
    public class LocaleRules {
127
             private boolean useLocaleRules;
128
             private Collator _collator;
129
             
130
             /**
131
              * Default constructor without parameters
132
              */
133
             public LocaleRules() {
134
                     useLocaleRules = false;
135
                     _collator = null;
136
             }
137
             
138
             /**
139
              * Default constructor with two parameters
140
              * 
141
              * @param b Use locale rules
142
              * @param collator A reference to an object configurated for locale-sensitive String comparison
143
              */
144
             public LocaleRules(boolean b, Collator collator) {
145
                     useLocaleRules = b;
146
                     _collator = collator;
147
             }
148
             
149
                 /**
150
                  * Gets the value of the inner attribute <i>_collator</i>
151
                  * 
152
                  * @return Returns A reference to an object configurated for locale-sensitive String comparison
153
                  */
154
                 public Collator getCollator() {
155
                         return _collator;
156
                 }
157

    
158
                 /**
159
                  * Sets a value to the inner attribute <i>_collator</i>
160
                  * 
161
                  * @param collator A reference to an object configurated for locale-sensitive String comparison
162
                  */
163
                 public void setCollator(Collator collator) {
164
                         this._collator = collator;
165
                 }
166

    
167
                /**
168
                 * Gets the value of the inner attribute <i>useLocaleRules</i>
169
                 * 
170
                 * @return Returns the useLocaleRules.
171
                 */
172
                public boolean isUseLocaleRules() {
173
                        return useLocaleRules;
174
                }
175

    
176
                /**
177
                 * Sets a value to the inner attribute <i>useLocaleRules</i>
178
                 * 
179
                 * @param useLocaleRules The useLocaleRules to set.
180
                 */
181
                public void setUseLocaleRules(boolean useLocaleRules) {
182
                        this.useLocaleRules = useLocaleRules;
183
                }
184
    }
185
}