19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.io.ObjectStreamField;
29 import java.io.UnsupportedEncodingException;
30 import java.nio.charset.Charset;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Comparator;
34 import java.util.Formatter;
35 import java.util.Locale;
36 import java.util.Objects;
37 import java.util.Spliterator;
38 import java.util.StringJoiner;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41 import java.util.regex.PatternSyntaxException;
42 import java.util.stream.IntStream;
43 import java.util.stream.StreamSupport;
44 import jdk.internal.HotSpotIntrinsicCandidate;
45 import jdk.internal.vm.annotation.Stable;
46
47 /**
48 * The {@code String} class represents character strings. All
49 * string literals in Java programs, such as {@code "abc"}, are
50 * implemented as instances of this class.
51 * <p>
52 * Strings are constant; their values cannot be changed after they
53 * are created. String buffers support mutable strings.
54 * Because String objects are immutable they can be shared. For example:
55 * <blockquote><pre>
56 * String str = "abc";
57 * </pre></blockquote><p>
58 * is equivalent to:
1212 return isLatin1() ? StringLatin1.compareTo(v1, v2)
1213 : StringUTF16.compareTo(v1, v2);
1214 }
1215 return isLatin1() ? StringLatin1.compareToUTF16(v1, v2)
1216 : StringUTF16.compareToLatin1(v1, v2);
1217 }
1218
1219 /**
1220 * A Comparator that orders {@code String} objects as by
1221 * {@code compareToIgnoreCase}. This comparator is serializable.
1222 * <p>
1223 * Note that this Comparator does <em>not</em> take locale into account,
1224 * and will result in an unsatisfactory ordering for certain locales.
1225 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1226 *
1227 * @see java.text.Collator
1228 * @since 1.2
1229 */
1230 public static final Comparator<String> CASE_INSENSITIVE_ORDER
1231 = new CaseInsensitiveComparator();
1232 private static class CaseInsensitiveComparator
1233 implements Comparator<String>, java.io.Serializable {
1234 // use serialVersionUID from JDK 1.2.2 for interoperability
1235 private static final long serialVersionUID = 8575799808933029326L;
1236
1237 public int compare(String s1, String s2) {
1238 byte v1[] = s1.value;
1239 byte v2[] = s2.value;
1240 if (s1.coder() == s2.coder()) {
1241 return s1.isLatin1() ? StringLatin1.compareToCI(v1, v2)
1242 : StringUTF16.compareToCI(v1, v2);
1243 }
1244 return s1.isLatin1() ? StringLatin1.compareToCI_UTF16(v1, v2)
1245 : StringUTF16.compareToCI_Latin1(v1, v2);
1246 }
1247
1248 /** Replaces the de-serialized object. */
1249 private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1250 }
1251
1252 /**
1253 * Compares two strings lexicographically, ignoring case
1254 * differences. This method returns an integer whose sign is that of
1255 * calling {@code compareTo} with normalized versions of the strings
1256 * where case differences have been eliminated by calling
1257 * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1258 * each character.
1259 * <p>
1260 * Note that this method does <em>not</em> take locale into account,
1261 * and will result in an unsatisfactory ordering for certain locales.
1262 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1263 *
1264 * @param str the {@code String} to be compared.
1265 * @return a negative integer, zero, or a positive integer as the
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.io.ObjectStreamField;
29 import java.io.UnsupportedEncodingException;
30 import java.nio.charset.Charset;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Comparator;
34 import java.util.Formatter;
35 import java.util.Locale;
36 import java.util.Objects;
37 import java.util.Spliterator;
38 import java.util.StringJoiner;
39 import java.util.function.ToIntFunction;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42 import java.util.regex.PatternSyntaxException;
43 import java.util.stream.IntStream;
44 import java.util.stream.StreamSupport;
45 import jdk.internal.HotSpotIntrinsicCandidate;
46 import jdk.internal.vm.annotation.Stable;
47
48 /**
49 * The {@code String} class represents character strings. All
50 * string literals in Java programs, such as {@code "abc"}, are
51 * implemented as instances of this class.
52 * <p>
53 * Strings are constant; their values cannot be changed after they
54 * are created. String buffers support mutable strings.
55 * Because String objects are immutable they can be shared. For example:
56 * <blockquote><pre>
57 * String str = "abc";
58 * </pre></blockquote><p>
59 * is equivalent to:
1213 return isLatin1() ? StringLatin1.compareTo(v1, v2)
1214 : StringUTF16.compareTo(v1, v2);
1215 }
1216 return isLatin1() ? StringLatin1.compareToUTF16(v1, v2)
1217 : StringUTF16.compareToLatin1(v1, v2);
1218 }
1219
1220 /**
1221 * A Comparator that orders {@code String} objects as by
1222 * {@code compareToIgnoreCase}. This comparator is serializable.
1223 * <p>
1224 * Note that this Comparator does <em>not</em> take locale into account,
1225 * and will result in an unsatisfactory ordering for certain locales.
1226 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1227 *
1228 * @see java.text.Collator
1229 * @since 1.2
1230 */
1231 public static final Comparator<String> CASE_INSENSITIVE_ORDER
1232 = new CaseInsensitiveComparator();
1233
1234 /**
1235 * A hashCode computing function that is consistent with
1236 * {@link #CASE_INSENSITIVE_ORDER} {@code Comparator}.
1237 *
1238 * @since 9
1239 */
1240 @SuppressWarnings("unchecked")
1241 public static final ToIntFunction<String> CASE_INSENSITIVE_HASHER
1242 = (ToIntFunction<String>) CASE_INSENSITIVE_ORDER;
1243
1244 private static class CaseInsensitiveComparator
1245 implements Comparator<String>, java.io.Serializable,
1246 ToIntFunction<String> /* hasher */ {
1247 // use serialVersionUID from JDK 1.2.2 for interoperability
1248 private static final long serialVersionUID = 8575799808933029326L;
1249
1250 @Override
1251 public int compare(String s1, String s2) {
1252 byte v1[] = s1.value;
1253 byte v2[] = s2.value;
1254 if (s1.coder() == s2.coder()) {
1255 return s1.isLatin1() ? StringLatin1.compareToCI(v1, v2)
1256 : StringUTF16.compareToCI(v1, v2);
1257 }
1258 return s1.isLatin1() ? StringLatin1.compareToCI_UTF16(v1, v2)
1259 : StringUTF16.compareToCI_Latin1(v1, v2);
1260 }
1261
1262 /**
1263 * Compute hashCode consistent with {@link #compare(String, String)}
1264 */
1265 @Override
1266 public int applyAsInt(String s) {
1267 return s.isLatin1() ? StringLatin1.hashCodeCI(s.value)
1268 : StringUTF16.hashCodeCI(s.value);
1269 }
1270
1271 /** Replaces the de-serialized object. */
1272 private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1273 }
1274
1275 /**
1276 * Compares two strings lexicographically, ignoring case
1277 * differences. This method returns an integer whose sign is that of
1278 * calling {@code compareTo} with normalized versions of the strings
1279 * where case differences have been eliminated by calling
1280 * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1281 * each character.
1282 * <p>
1283 * Note that this method does <em>not</em> take locale into account,
1284 * and will result in an unsatisfactory ordering for certain locales.
1285 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1286 *
1287 * @param str the {@code String} to be compared.
1288 * @return a negative integer, zero, or a positive integer as the
|