Revision 11448 trunk/libraries/libIverUtiles/src/com/iver/utiles/StringComparator.java
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 | 46 |
/** |
47 |
* Comparator que compara dos cadenas alfab?ticamente
|
|
47 |
* Compares two chain of characters alphabetically
|
|
48 | 48 |
* |
49 | 49 |
* @author Fernando Gonz?lez Cort?s |
50 |
* @author Pablo Piqueras Bartolom? |
|
50 | 51 |
*/ |
51 | 52 |
public class StringComparator implements Comparator { |
52 |
boolean caseSensitive = true; |
|
53 |
private boolean caseSensitive = true; |
|
54 |
private LocaleRules localeRules = null; |
|
53 | 55 |
|
54 | 56 |
/** |
55 | 57 |
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
... | ... | |
58 | 60 |
String s1 = (String) o1; |
59 | 61 |
String s2 = (String) o2; |
60 | 62 |
|
61 |
if (caseSensitive) { |
|
62 |
return s1.compareTo(s2); |
|
63 |
} else { |
|
64 |
return s1.compareToIgnoreCase(s2); |
|
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 |
} |
|
65 | 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 |
} |
|
66 | 92 |
} |
67 | 93 |
|
68 | 94 |
/** |
69 |
* Devuelve si el comparador es sensible a las minusculas y may?sculas
|
|
95 |
* Returns if the comparator is sensitive to small and big letters
|
|
70 | 96 |
* |
71 | 97 |
* @return |
72 | 98 |
*/ |
... | ... | |
82 | 108 |
public void setCaseSensitive(boolean b) { |
83 | 109 |
caseSensitive = b; |
84 | 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 |
} |
|
85 | 201 |
} |
Also available in: Unified diff