1205 * A Comparator that orders {@code String} objects as by
1206 * {@code compareToIgnoreCase}. This comparator is serializable.
1207 * <p>
1208 * Note that this Comparator does <em>not</em> take locale into account,
1209 * and will result in an unsatisfactory ordering for certain locales.
1210 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1211 *
1212 * @see java.text.Collator
1213 * @since 1.2
1214 */
1215 public static final Comparator<String> CASE_INSENSITIVE_ORDER
1216 = new CaseInsensitiveComparator();
1217 private static class CaseInsensitiveComparator
1218 implements Comparator<String>, java.io.Serializable {
1219 // use serialVersionUID from JDK 1.2.2 for interoperability
1220 private static final long serialVersionUID = 8575799808933029326L;
1221
1222 public int compare(String s1, String s2) {
1223 byte v1[] = s1.value;
1224 byte v2[] = s2.value;
1225 int n1 = s1.length();
1226 int n2 = s2.length();
1227 boolean s1IsLatin1 = s1.isLatin1();
1228 boolean s2IsLatin1 = s2.isLatin1();
1229 int min = Math.min(n1, n2);
1230 for (int i = 0; i < min; i++) {
1231 char c1 = s1IsLatin1 ? StringLatin1.getChar(v1, i)
1232 : StringUTF16.getChar(v1, i);
1233 char c2 = s2IsLatin1 ? StringLatin1.getChar(v2, i)
1234 : StringUTF16.getChar(v2, i);
1235 if (c1 != c2) {
1236 c1 = Character.toUpperCase(c1);
1237 c2 = Character.toUpperCase(c2);
1238 if (c1 != c2) {
1239 c1 = Character.toLowerCase(c1);
1240 c2 = Character.toLowerCase(c2);
1241 if (c1 != c2) {
1242 // No overflow because of numeric promotion
1243 return c1 - c2;
1244 }
1245 }
1246 }
1247 }
1248 return n1 - n2;
1249 }
1250
1251 /** Replaces the de-serialized object. */
1252 private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1253 }
1254
1255 /**
1256 * Compares two strings lexicographically, ignoring case
1257 * differences. This method returns an integer whose sign is that of
1258 * calling {@code compareTo} with normalized versions of the strings
1259 * where case differences have been eliminated by calling
1260 * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1261 * each character.
1262 * <p>
1263 * Note that this method does <em>not</em> take locale into account,
1264 * and will result in an unsatisfactory ordering for certain locales.
1265 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1266 *
1267 * @param str the {@code String} to be compared.
1268 * @return a negative integer, zero, or a positive integer as the
|
1205 * A Comparator that orders {@code String} objects as by
1206 * {@code compareToIgnoreCase}. This comparator is serializable.
1207 * <p>
1208 * Note that this Comparator does <em>not</em> take locale into account,
1209 * and will result in an unsatisfactory ordering for certain locales.
1210 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1211 *
1212 * @see java.text.Collator
1213 * @since 1.2
1214 */
1215 public static final Comparator<String> CASE_INSENSITIVE_ORDER
1216 = new CaseInsensitiveComparator();
1217 private static class CaseInsensitiveComparator
1218 implements Comparator<String>, java.io.Serializable {
1219 // use serialVersionUID from JDK 1.2.2 for interoperability
1220 private static final long serialVersionUID = 8575799808933029326L;
1221
1222 public int compare(String s1, String s2) {
1223 byte v1[] = s1.value;
1224 byte v2[] = s2.value;
1225 if (s1.coder() == s2.coder()) {
1226 return s1.isLatin1() ? StringLatin1.compareToCI(v1, v2)
1227 : StringUTF16.compareToCI(v1, v2);
1228 }
1229 return s1.isLatin1() ? StringLatin1.compareToCI_UTF16(v1, v2)
1230 : StringUTF16.compareToCI_Latin1(v1, v2);
1231 }
1232
1233 /** Replaces the de-serialized object. */
1234 private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1235 }
1236
1237 /**
1238 * Compares two strings lexicographically, ignoring case
1239 * differences. This method returns an integer whose sign is that of
1240 * calling {@code compareTo} with normalized versions of the strings
1241 * where case differences have been eliminated by calling
1242 * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1243 * each character.
1244 * <p>
1245 * Note that this method does <em>not</em> take locale into account,
1246 * and will result in an unsatisfactory ordering for certain locales.
1247 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1248 *
1249 * @param str the {@code String} to be compared.
1250 * @return a negative integer, zero, or a positive integer as the
|