Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/CollationKey.java
38829 views
/*1* Copyright (c) 1997, 2013, 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</code> represents a <code>String</code> under the42* rules of a specific <code>Collator</code> object. Comparing two43* <code>CollationKey</code>s returns the relative order of the44* <code>String</code>s they represent. Using <code>CollationKey</code>s45* to compare <code>String</code>s is generally faster than using46* <code>Collator.compare</code>. Thus, when the <code>String</code>s47* must be compared multiple times, for example when sorting a list48* of <code>String</code>s. It's more efficient to use <code>CollationKey</code>s.49*50* <p>51* You can not create <code>CollationKey</code>s directly. Rather,52* generate them by calling <code>Collator.getCollationKey</code>.53* You can only compare <code>CollationKey</code>s generated from54* the same <code>Collator</code> object.55*56* <p>57* Generating a <code>CollationKey</code> for a <code>String</code>58* involves examining the entire <code>String</code>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</code>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</code>.64* <code>Collator.compare</code> 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</code>s might be used68* to sort a list of <code>String</code>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*/9899public abstract class CollationKey implements Comparable<CollationKey> {100/**101* Compare this CollationKey to the target CollationKey. The collation rules of the102* Collator object which created these keys are applied. <strong>Note:</strong>103* CollationKeys created by different Collators can not be compared.104* @param target target CollationKey105* @return Returns an integer value. Value is less than zero if this is less106* than target, value is zero if this and target are equal and value is greater than107* zero if this is greater than target.108* @see java.text.Collator#compare109*/110abstract public int compareTo(CollationKey target);111112/**113* Returns the String that this CollationKey represents.114*115* @return the source string of this CollationKey116*/117public String getSourceString() {118return source;119}120121122/**123* Converts the CollationKey to a sequence of bits. If two CollationKeys124* could be legitimately compared, then one could compare the byte arrays125* for each of those keys to obtain the same result. Byte arrays are126* organized most significant byte first.127*128* @return a byte array representation of the CollationKey129*/130abstract public byte[] toByteArray();131132133/**134* CollationKey constructor.135*136* @param source the source string137* @exception NullPointerException if {@code source} is null138* @since 1.6139*/140protected CollationKey(String source) {141if (source==null){142throw new NullPointerException();143}144this.source = source;145}146147final private String source;148}149150151