jdk/src/share/classes/java/text/RuleBasedCollationKey.java
author naoto
Tue, 31 Aug 2010 11:27:10 -0700
changeset 6489 9e7015635425
parent 5506 202f599c92aa
permissions -rw-r--r--
4700857: RFE: separating user locale and user interface locale Reviewed-by: okutsu
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) 2005, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *   The original version of this source code and documentation is copyrighted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * materials are provided under terms of a License Agreement between Taligent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and Sun. This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * patents. This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
package java.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A RuleBasedCollationKey is a concrete implementation of CollationKey class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The RuleBasedCollationKey class is used by the RuleBasedCollator class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final class RuleBasedCollationKey extends CollationKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Compare this RuleBasedCollationKey to target. The collation rules of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Collator object which created these keys are applied. <strong>Note:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * RuleBasedCollationKeys created by different Collators can not be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @param target target RuleBasedCollationKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @return Returns an integer value. Value is less than zero if this is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * than target, value is zero if this and target are equal and value is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * zero if this is greater than target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @see java.text.Collator#compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public int compareTo(CollationKey target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int result = key.compareTo(((RuleBasedCollationKey)(target)).key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (result <= Collator.LESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return Collator.LESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        else if (result >= Collator.GREATER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return Collator.GREATER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return Collator.EQUAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Compare this RuleBasedCollationKey and the target for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The collation rules of the Collator object which created these keys are applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <strong>Note:</strong> RuleBasedCollationKeys created by different Collators can not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param target the RuleBasedCollationKey to compare to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @return Returns true if two objects are equal, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public boolean equals(Object target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (this == target) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (target == null || !getClass().equals(target.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        RuleBasedCollationKey other = (RuleBasedCollationKey)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return key.equals(other.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Creates a hash code for this RuleBasedCollationKey. The hash value is calculated on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * key itself, not the String from which the key was created.  Thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * if x and y are RuleBasedCollationKeys, then x.hashCode(x) == y.hashCode() if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * x.equals(y) is true.  This allows language-sensitive comparison in a hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * See the CollatinKey class description for an example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @return the hash value based on the string's collation order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return (key.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Converts the RuleBasedCollationKey to a sequence of bits. If two RuleBasedCollationKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * could be legitimately compared, then one could compare the byte arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * for each of those keys to obtain the same result.  Byte arrays are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * organized most significant byte first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public byte[] toByteArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        char[] src = key.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        byte[] dest = new byte[ 2*src.length ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        for( int i=0; i<src.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            dest[j++] = (byte)(src[i] >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            dest[j++] = (byte)(src[i] & 0x00ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * A RuleBasedCollationKey can only be generated by Collator objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    RuleBasedCollationKey(String source, String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        super(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private String key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}