Revision 18281
branches/v10/libraries/libIverUtiles/src/com/iver/utiles/StringComparator.java | ||
---|---|---|
40 | 40 |
*/ |
41 | 41 |
package com.iver.utiles; |
42 | 42 |
|
43 |
import java.text.Collator; |
|
43 | 44 |
import java.util.Comparator; |
44 | 45 |
|
45 | 46 |
|
... | ... | |
47 | 48 |
* Comparator que compara dos cadenas alfab?ticamente |
48 | 49 |
* |
49 | 50 |
* @author Fernando Gonz?lez Cort?s |
51 |
* @author Pablo Piqueras Bartolom? |
|
50 | 52 |
*/ |
51 | 53 |
public class StringComparator implements Comparator { |
52 |
boolean caseSensitive = true; |
|
54 |
private boolean caseSensitive = true; |
|
55 |
private LocaleRules localeRules = null; |
|
53 | 56 |
|
54 | 57 |
/** |
55 | 58 |
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
56 | 59 |
*/ |
57 | 60 |
public int compare(Object o1, Object o2) { |
58 |
String s1 = (String) o1;
|
|
59 |
String s2 = (String) o2;
|
|
61 |
String s1 = o1.toString();
|
|
62 |
String s2 = o2.toString();
|
|
60 | 63 |
|
61 |
if (caseSensitive) { |
|
62 |
return s1.compareTo(s2); |
|
63 |
} else { |
|
64 |
return s1.compareToIgnoreCase(s2); |
|
64 |
// If localeRules is null -> use the default rules |
|
65 |
if (localeRules == null) { |
|
66 |
if (caseSensitive) { |
|
67 |
return s1.compareTo(s2); |
|
68 |
} |
|
69 |
else { |
|
70 |
return s1.compareToIgnoreCase(s2); |
|
71 |
} |
|
65 | 72 |
} |
73 |
else { |
|
74 |
if (localeRules.isUseLocaleRules()) { |
|
75 |
Collator collator = localeRules.getCollator(); |
|
76 |
|
|
77 |
if (caseSensitive) { |
|
78 |
return collator.compare(s1, s2); |
|
79 |
} |
|
80 |
else { |
|
81 |
//return collator.compare(s1.toLowerCase(), s2.toLowerCase()); |
|
82 |
return collator.compare(s1.toUpperCase(), s2.toUpperCase()); |
|
83 |
} |
|
84 |
} |
|
85 |
else { |
|
86 |
if (caseSensitive) { |
|
87 |
return s1.compareTo(s2); |
|
88 |
} |
|
89 |
else { |
|
90 |
return s1.compareToIgnoreCase(s2); |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
66 | 94 |
} |
67 | 95 |
|
68 | 96 |
/** |
69 |
* Devuelve si el comparador es sensible a las minusculas y may?sculas
|
|
97 |
* Returns if the comparator is sensitive to small and big letters
|
|
70 | 98 |
* |
71 | 99 |
* @return |
72 | 100 |
*/ |
... | ... | |
82 | 110 |
public void setCaseSensitive(boolean b) { |
83 | 111 |
caseSensitive = b; |
84 | 112 |
} |
113 |
|
|
114 |
/** |
|
115 |
* Gets an object with the information for use the locale rules in comparation between strings. <br> |
|
116 |
* <ul> |
|
117 |
* <li>A boolean value -> if want or not use the locale rules</li> |
|
118 |
* <li>A reference to the locale rules</li> |
|
119 |
* </ul> |
|
120 |
* |
|
121 |
* @return @see LocaleRules |
|
122 |
*/ |
|
123 |
public LocaleRules getLocaleRules() { |
|
124 |
return localeRules; |
|
125 |
} |
|
126 |
/** |
|
127 |
* Sets an object with the information for use the locale rules in comparation between strings. <br> |
|
128 |
* <ul> |
|
129 |
* <li>A boolean value -> if want or not use the locale rules</li> |
|
130 |
* <li>A reference to the locale rules</li> |
|
131 |
* </ul> |
|
132 |
* |
|
133 |
* @param @see LocaleRules |
|
134 |
*/ |
|
135 |
public void setLocaleRules(LocaleRules locRules) { |
|
136 |
localeRules = locRules; |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* 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 |
|
141 |
* |
|
142 |
* @author Pablo Piqueras Bartolom? |
|
143 |
*/ |
|
144 |
public class LocaleRules { |
|
145 |
private boolean useLocaleRules; |
|
146 |
private Collator _collator; |
|
147 |
|
|
148 |
/** |
|
149 |
* Default constructor without parameters |
|
150 |
*/ |
|
151 |
public LocaleRules() { |
|
152 |
useLocaleRules = false; |
|
153 |
_collator = null; |
|
154 |
} |
|
155 |
|
|
156 |
/** |
|
157 |
* Default constructor with two parameters |
|
158 |
* |
|
159 |
* @param b Use locale rules |
|
160 |
* @param collator A reference to an object configurated for locale-sensitive String comparison |
|
161 |
*/ |
|
162 |
public LocaleRules(boolean b, Collator collator) { |
|
163 |
useLocaleRules = b; |
|
164 |
_collator = collator; |
|
165 |
} |
|
166 |
|
|
167 |
/** |
|
168 |
* Gets the value of the inner attribute <i>_collator</i> |
|
169 |
* |
|
170 |
* @return Returns A reference to an object configurated for locale-sensitive String comparison |
|
171 |
*/ |
|
172 |
public Collator getCollator() { |
|
173 |
return _collator; |
|
174 |
} |
|
175 |
|
|
176 |
/** |
|
177 |
* Sets a value to the inner attribute <i>_collator</i> |
|
178 |
* |
|
179 |
* @param collator A reference to an object configurated for locale-sensitive String comparison |
|
180 |
*/ |
|
181 |
public void setCollator(Collator collator) { |
|
182 |
this._collator = collator; |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* Gets the value of the inner attribute <i>useLocaleRules</i> |
|
187 |
* |
|
188 |
* @return Returns the useLocaleRules. |
|
189 |
*/ |
|
190 |
public boolean isUseLocaleRules() { |
|
191 |
return useLocaleRules; |
|
192 |
} |
|
193 |
|
|
194 |
/** |
|
195 |
* Sets a value to the inner attribute <i>useLocaleRules</i> |
|
196 |
* |
|
197 |
* @param useLocaleRules The useLocaleRules to set. |
|
198 |
*/ |
|
199 |
public void setUseLocaleRules(boolean useLocaleRules) { |
|
200 |
this.useLocaleRules = useLocaleRules; |
|
201 |
} |
|
202 |
} |
|
85 | 203 |
} |
Also available in: Unified diff