Path: blob/master/src/java.base/share/classes/java/text/CollationKey.java
41152 views
/*1* Copyright (c) 1997, 2019, 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 {@code CollationKey} represents a {@code String} under the42* rules of a specific {@code Collator} object. Comparing two43* {@code CollationKey}s returns the relative order of the44* {@code String}s they represent. Using {@code CollationKey}s45* to compare {@code String}s is generally faster than using46* {@code Collator.compare}. Thus, when the {@code String}s47* must be compared multiple times, for example when sorting a list48* of {@code String}s. It's more efficient to use {@code CollationKey}s.49*50* <p>51* You can not create {@code CollationKey}s directly. Rather,52* generate them by calling {@code Collator.getCollationKey}.53* You can only compare {@code CollationKey}s generated from54* the same {@code Collator} object.55*56* <p>57* Generating a {@code CollationKey} for a {@code String}58* involves examining the entire {@code String}59* and converting it to series of bits that can be compared bitwise. This60* allows fast comparisons once the keys are generated. The cost of generating61* keys is recouped in faster comparisons when {@code String}s need62* to be compared many times. On the other hand, the result of a comparison63* is often determined by the first couple of characters of each {@code String}.64* {@code Collator.compare} examines only as many characters as it needs which65* allows it to be faster when doing single comparisons.66* <p>67* The following example shows how {@code CollationKey}s might be used68* to sort a list of {@code String}s.69* <blockquote>70* <pre>{@code71* // Create an array of CollationKeys for the Strings to be sorted.72* Collator myCollator = Collator.getInstance();73* CollationKey[] keys = new CollationKey[3];74* keys[0] = myCollator.getCollationKey("Tom");75* keys[1] = myCollator.getCollationKey("Dick");76* keys[2] = myCollator.getCollationKey("Harry");77* sort(keys);78*79* //...80*81* // Inside body of sort routine, compare keys this way82* if (keys[i].compareTo(keys[j]) > 0)83* // swap keys[i] and keys[j]84*85* //...86*87* // Finally, when we've returned from sort.88* System.out.println(keys[0].getSourceString());89* System.out.println(keys[1].getSourceString());90* System.out.println(keys[2].getSourceString());91* }</pre>92* </blockquote>93*94* @see Collator95* @see RuleBasedCollator96* @author Helena Shih97* @since 1.198*/99100public abstract class CollationKey implements Comparable<CollationKey> {101/**102* Compare this CollationKey to the target CollationKey. The collation rules of the103* Collator object which created these keys are applied. <strong>Note:</strong>104* CollationKeys created by different Collators can not be compared.105* @param target target CollationKey106* @return Returns an integer value. Value is less than zero if this is less107* than target, value is zero if this and target are equal and value is greater than108* zero if this is greater than target.109* @see java.text.Collator#compare110*/111public abstract int compareTo(CollationKey target);112113/**114* Returns the String that this CollationKey represents.115*116* @return the source string of this CollationKey117*/118public String getSourceString() {119return source;120}121122123/**124* Converts the CollationKey to a sequence of bits. If two CollationKeys125* could be legitimately compared, then one could compare the byte arrays126* for each of those keys to obtain the same result. Byte arrays are127* organized most significant byte first.128*129* @return a byte array representation of the CollationKey130*/131public abstract byte[] toByteArray();132133134/**135* CollationKey constructor.136*137* @param source the source string138* @throws NullPointerException if {@code source} is null139* @since 1.6140*/141protected CollationKey(String source) {142if (source==null){143throw new NullPointerException();144}145this.source = source;146}147148private final String source;149}150151152