jdk/src/share/classes/sun/misc/ASCIICaseInsensitiveComparator.java
author chegar
Mon, 08 Apr 2013 06:15:18 +0100
changeset 18223 35a5c2462991
parent 5506 202f599c92aa
permissions -rw-r--r--
8008593: Better URLClassLoader resource management Reviewed-by: alanb, sherman, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/** Implements a locale and case insensitive comparator suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    strings that are known to only contain ASCII characters. Some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    tables internal to the JDK contain only ASCII data and are using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    the "generalized" java.lang.String case-insensitive comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    which converts each character to both upper and lower case. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class ASCIICaseInsensitiveComparator implements Comparator<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static final Comparator<String> CASE_INSENSITIVE_ORDER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        new ASCIICaseInsensitiveComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public int compare(String s1, String s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        int n1=s1.length(), n2=s2.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        int minLen = n1 < n2 ? n1 : n2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        for (int i=0; i < minLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            char c1 = s1.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            char c2 = s2.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            assert c1 <= '\u007F' && c2 <= '\u007F';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            if (c1 != c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                c1 = (char)toLower(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                c2 = (char)toLower(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                if (c1 != c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                    return c1 - c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return n1 - n2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * A case insensitive hash code method to go with the case insensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * compare() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Returns a hash code for this ASCII string as if it were lower case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * returns same answer as:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <code>s.toLowerCase(Locale.US).hashCode();</code><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * but does not allocate memory (it does NOT have the special
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * case Turkish rules).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param s a String to compute the hashcode on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @return  a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static int lowerCaseHashCode(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            h = 31*h + toLower(s.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /* If java.util.regex.ASCII ever becomes public or sun.*, use its code instead:*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static boolean isLower(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return ((ch-'a')|('z'-ch)) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static boolean isUpper(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return ((ch-'A')|('Z'-ch)) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static int toLower(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return isUpper(ch) ? (ch + 0x20) : ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static int toUpper(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return isLower(ch) ? (ch - 0x20) : ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}