001 /*
002 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 /*
027 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
028 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
029 *
030 * The original version of this source code and documentation is copyrighted
031 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
032 * materials are provided under terms of a License Agreement between Taligent
033 * and Sun. This technology is protected by multiple US and International
034 * patents. This notice and attribution to Taligent may not be removed.
035 * Taligent is a registered trademark of Taligent, Inc.
036 *
037 */
038
039 package java.text;
040
041 /**
042 * A <code>CollationKey</code> represents a <code>String</code> under the
043 * rules of a specific <code>Collator</code> object. Comparing two
044 * <code>CollationKey</code>s returns the relative order of the
045 * <code>String</code>s they represent. Using <code>CollationKey</code>s
046 * to compare <code>String</code>s is generally faster than using
047 * <code>Collator.compare</code>. Thus, when the <code>String</code>s
048 * must be compared multiple times, for example when sorting a list
049 * of <code>String</code>s. It's more efficient to use <code>CollationKey</code>s.
050 *
051 * <p>
052 * You can not create <code>CollationKey</code>s directly. Rather,
053 * generate them by calling <code>Collator.getCollationKey</code>.
054 * You can only compare <code>CollationKey</code>s generated from
055 * the same <code>Collator</code> object.
056 *
057 * <p>
058 * Generating a <code>CollationKey</code> for a <code>String</code>
059 * involves examining the entire <code>String</code>
060 * and converting it to series of bits that can be compared bitwise. This
061 * allows fast comparisons once the keys are generated. The cost of generating
062 * keys is recouped in faster comparisons when <code>String</code>s need
063 * to be compared many times. On the other hand, the result of a comparison
064 * is often determined by the first couple of characters of each <code>String</code>.
065 * <code>Collator.compare</code> examines only as many characters as it needs which
066 * allows it to be faster when doing single comparisons.
067 * <p>
068 * The following example shows how <code>CollationKey</code>s might be used
069 * to sort a list of <code>String</code>s.
070 * <blockquote>
071 * <pre>
072 * // Create an array of CollationKeys for the Strings to be sorted.
073 * Collator myCollator = Collator.getInstance();
074 * CollationKey[] keys = new CollationKey[3];
075 * keys[0] = myCollator.getCollationKey("Tom");
076 * keys[1] = myCollator.getCollationKey("Dick");
077 * keys[2] = myCollator.getCollationKey("Harry");
078 * sort( keys );
079 * <br>
080 * //...
081 * <br>
082 * // Inside body of sort routine, compare keys this way
083 * if( keys[i].compareTo( keys[j] ) > 0 )
084 * // swap keys[i] and keys[j]
085 * <br>
086 * //...
087 * <br>
088 * // Finally, when we've returned from sort.
089 * System.out.println( keys[0].getSourceString() );
090 * System.out.println( keys[1].getSourceString() );
091 * System.out.println( keys[2].getSourceString() );
092 * </pre>
093 * </blockquote>
094 *
095 * @see Collator
096 * @see RuleBasedCollator
097 * @version 1.27, 05/05/07
098 * @author Helena Shih
099 */
100
101 public abstract class CollationKey implements Comparable<CollationKey> {
102 /**
103 * Compare this CollationKey to the target CollationKey. The collation rules of the
104 * Collator object which created these keys are applied. <strong>Note:</strong>
105 * CollationKeys created by different Collators can not be compared.
106 * @param target target CollationKey
107 * @return Returns an integer value. Value is less than zero if this is less
108 * than target, value is zero if this and target are equal and value is greater than
109 * zero if this is greater than target.
110 * @see java.text.Collator#compare
111 */
112 abstract public int compareTo(CollationKey target);
113
114 /**
115 * Returns the String that this CollationKey represents.
116 */
117 public String getSourceString() {
118 return source;
119 }
120
121 /**
122 * Converts the CollationKey to a sequence of bits. If two CollationKeys
123 * could be legitimately compared, then one could compare the byte arrays
124 * for each of those keys to obtain the same result. Byte arrays are
125 * organized most significant byte first.
126 */
127 abstract public byte[] toByteArray();
128
129 /**
130 * CollationKey constructor.
131 *
132 * @param source - the source string.
133 * @exception NullPointerException if <code>source</code> is null.
134 * @since 1.6
135 */
136 protected CollationKey(String source) {
137 if (source == null) {
138 throw new NullPointerException();
139 }
140 this .source = source;
141 }
142
143 final private String source;
144 }
|