Path: blob/master/src/java.base/share/classes/java/text/RuleBasedCollationKey.java
41152 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996 - All Rights Reserved27* (C) Copyright IBM Corp. 1996 - All Rights Reserved28*29* The original version of this source code and documentation is copyrighted30* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These31* materials are provided under terms of a License Agreement between Taligent32* and Sun. This technology is protected by multiple US and International33* patents. This notice and attribution to Taligent may not be removed.34* Taligent is a registered trademark of Taligent, Inc.35*36*/3738package java.text;3940/**41* A RuleBasedCollationKey is a concrete implementation of CollationKey class.42* The RuleBasedCollationKey class is used by the RuleBasedCollator class.43*/4445final class RuleBasedCollationKey extends CollationKey {46/**47* Compare this RuleBasedCollationKey to target. The collation rules of the48* Collator object which created these keys are applied. <strong>Note:</strong>49* RuleBasedCollationKeys created by different Collators can not be compared.50* @param target target RuleBasedCollationKey51* @return Returns an integer value. Value is less than zero if this is less52* than target, value is zero if this and target are equal and value is greater than53* zero if this is greater than target.54* @see java.text.Collator#compare55*/56public int compareTo(CollationKey target)57{58int result = key.compareTo(((RuleBasedCollationKey)(target)).key);59if (result <= Collator.LESS)60return Collator.LESS;61else if (result >= Collator.GREATER)62return Collator.GREATER;63return Collator.EQUAL;64}6566/**67* Compare this RuleBasedCollationKey and the target for equality.68* The collation rules of the Collator object which created these keys are applied.69* <strong>Note:</strong> RuleBasedCollationKeys created by different Collators can not be70* compared.71* @param target the RuleBasedCollationKey to compare to.72* @return Returns true if two objects are equal, false otherwise.73*/74public boolean equals(Object target) {75if (this == target) return true;76if (target == null || !getClass().equals(target.getClass())) {77return false;78}79RuleBasedCollationKey other = (RuleBasedCollationKey)target;80return key.equals(other.key);81}8283/**84* Creates a hash code for this RuleBasedCollationKey. The hash value is calculated on the85* key itself, not the String from which the key was created. Thus86* if x and y are RuleBasedCollationKeys, then x.hashCode(x) == y.hashCode() if87* x.equals(y) is true. This allows language-sensitive comparison in a hash table.88* See the CollatinKey class description for an example.89* @return the hash value based on the string's collation order.90*/91public int hashCode() {92return (key.hashCode());93}9495/**96* Converts the RuleBasedCollationKey to a sequence of bits. If two RuleBasedCollationKeys97* could be legitimately compared, then one could compare the byte arrays98* for each of those keys to obtain the same result. Byte arrays are99* organized most significant byte first.100*/101public byte[] toByteArray() {102103char[] src = key.toCharArray();104byte[] dest = new byte[ 2*src.length ];105int j = 0;106for( int i=0; i<src.length; i++ ) {107dest[j++] = (byte)(src[i] >>> 8);108dest[j++] = (byte)(src[i] & 0x00ff);109}110return dest;111}112113/**114* A RuleBasedCollationKey can only be generated by Collator objects.115*/116RuleBasedCollationKey(String source, String key) {117super(source);118this.key = key;119}120private String key = null;121122}123124125