root / trunk / libraries / libIverUtiles / src / com / iver / utiles / StringComparator.java @ 11448
History | View | Annotate | Download (5.41 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.utiles; |
42 |
|
43 |
import java.text.Collator; |
44 |
import java.util.Comparator; |
45 |
|
46 |
/**
|
47 |
* Compares two chain of characters alphabetically
|
48 |
*
|
49 |
* @author Fernando Gonz?lez Cort?s
|
50 |
* @author Pablo Piqueras Bartolom?
|
51 |
*/
|
52 |
public class StringComparator implements Comparator { |
53 |
private boolean caseSensitive = true; |
54 |
private LocaleRules localeRules = null; |
55 |
|
56 |
/**
|
57 |
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
58 |
*/
|
59 |
public int compare(Object o1, Object o2) { |
60 |
String s1 = (String) o1; |
61 |
String s2 = (String) o2; |
62 |
|
63 |
// If localeRules is null -> use the default rules
|
64 |
if (localeRules == null) { |
65 |
if (caseSensitive) {
|
66 |
return s1.compareTo(s2);
|
67 |
} |
68 |
else {
|
69 |
return s1.compareToIgnoreCase(s2);
|
70 |
} |
71 |
} |
72 |
else {
|
73 |
if (localeRules.isUseLocaleRules()) {
|
74 |
Collator collator = localeRules.getCollator();
|
75 |
|
76 |
if (caseSensitive) {
|
77 |
return collator.compare(s1, s2);
|
78 |
} |
79 |
else {
|
80 |
return collator.compare(s1.toLowerCase(), s2.toLowerCase());
|
81 |
} |
82 |
} |
83 |
else {
|
84 |
if (caseSensitive) {
|
85 |
return s1.compareTo(s2);
|
86 |
} |
87 |
else {
|
88 |
return s1.compareToIgnoreCase(s2);
|
89 |
} |
90 |
} |
91 |
} |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Returns if the comparator is sensitive to small and big letters
|
96 |
*
|
97 |
* @return
|
98 |
*/
|
99 |
public boolean isCaseSensitive() { |
100 |
return caseSensitive;
|
101 |
} |
102 |
|
103 |
/**
|
104 |
* Establece la sensibilidad del comparador a las mayusculas y minusculas
|
105 |
*
|
106 |
* @param b
|
107 |
*/
|
108 |
public void setCaseSensitive(boolean b) { |
109 |
caseSensitive = b; |
110 |
} |
111 |
|
112 |
/**
|
113 |
* Gets an object with the information for use the locale rules in comparation between strings. <br>
|
114 |
* <ul>
|
115 |
* <li>A boolean value -> if want or not use the locale rules</li>
|
116 |
* <li>A reference to the locale rules</li>
|
117 |
* </ul>
|
118 |
*
|
119 |
* @return @see LocaleRules
|
120 |
*/
|
121 |
public LocaleRules getLocaleRules() {
|
122 |
return localeRules;
|
123 |
} |
124 |
/**
|
125 |
* Sets an object with the information for use the locale rules in comparation between strings. <br>
|
126 |
* <ul>
|
127 |
* <li>A boolean value -> if want or not use the locale rules</li>
|
128 |
* <li>A reference to the locale rules</li>
|
129 |
* </ul>
|
130 |
*
|
131 |
* @param @see LocaleRules
|
132 |
*/
|
133 |
public void setLocaleRules(LocaleRules locRules) { |
134 |
localeRules = locRules; |
135 |
} |
136 |
|
137 |
/**
|
138 |
* 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
|
139 |
*
|
140 |
* @author Pablo Piqueras Bartolom?
|
141 |
*/
|
142 |
public class LocaleRules { |
143 |
private boolean useLocaleRules; |
144 |
private Collator _collator; |
145 |
|
146 |
/**
|
147 |
* Default constructor without parameters
|
148 |
*/
|
149 |
public LocaleRules() {
|
150 |
useLocaleRules = false;
|
151 |
_collator = null;
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* Default constructor with two parameters
|
156 |
*
|
157 |
* @param b Use locale rules
|
158 |
* @param collator A reference to an object configurated for locale-sensitive String comparison
|
159 |
*/
|
160 |
public LocaleRules(boolean b, Collator collator) { |
161 |
useLocaleRules = b; |
162 |
_collator = collator; |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Gets the value of the inner attribute <i>_collator</i>
|
167 |
*
|
168 |
* @return Returns A reference to an object configurated for locale-sensitive String comparison
|
169 |
*/
|
170 |
public Collator getCollator() { |
171 |
return _collator;
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* Sets a value to the inner attribute <i>_collator</i>
|
176 |
*
|
177 |
* @param collator A reference to an object configurated for locale-sensitive String comparison
|
178 |
*/
|
179 |
public void setCollator(Collator collator) { |
180 |
this._collator = collator;
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Gets the value of the inner attribute <i>useLocaleRules</i>
|
185 |
*
|
186 |
* @return Returns the useLocaleRules.
|
187 |
*/
|
188 |
public boolean isUseLocaleRules() { |
189 |
return useLocaleRules;
|
190 |
} |
191 |
|
192 |
/**
|
193 |
* Sets a value to the inner attribute <i>useLocaleRules</i>
|
194 |
*
|
195 |
* @param useLocaleRules The useLocaleRules to set.
|
196 |
*/
|
197 |
public void setUseLocaleRules(boolean useLocaleRules) { |
198 |
this.useLocaleRules = useLocaleRules;
|
199 |
} |
200 |
} |
201 |
} |