2 *******************************************************************************
\r
3 * Copyright (C) 1996-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.text;
\r
11 * <p>A <code>CollationKey</code> represents a <code>String</code>
\r
12 * under the rules of a specific <code>Collator</code>
\r
13 * object. Comparing two <code>CollationKey</code>s returns the
\r
14 * relative order of the <code>String</code>s they represent.</p>
\r
16 * <p>Since the rule set of <code>Collator</code>s can differ, the
\r
17 * sort orders of the same string under two different
\r
18 * <code>Collator</code>s might differ. Hence comparing
\r
19 * <code>CollationKey</code>s generated from different
\r
20 * <code>Collator</code>s can give incorrect results.</p>
\r
22 * <p>Both the method
\r
23 * <code>CollationKey.compareTo(CollationKey)</code> and the method
\r
24 * <code>Collator.compare(String, String)</code> compare two strings
\r
25 * and returns their relative order. The performance characterictics
\r
26 * of these two approaches can differ.</p>
\r
28 * <p>During the construction of a <code>CollationKey</code>, the
\r
29 * entire source string is examined and processed into a series of
\r
30 * bits terminated by a null, that are stored in the <code>CollationKey</code>.
\r
31 * When <code>CollationKey.compareTo(CollationKey)</code> executes, it
\r
32 * performs bitwise comparison on the bit sequences. This can incurs
\r
33 * startup cost when creating the <code>CollationKey</code>, but once
\r
34 * the key is created, binary comparisons are fast. This approach is
\r
35 * recommended when the same strings are to be compared over and over
\r
38 * <p>On the other hand, implementations of
\r
39 * <code>Collator.compare(String, String)</code> can examine and
\r
40 * process the strings only until the first characters differing in
\r
41 * order. This approach is recommended if the strings are to be
\r
42 * compared only once.</p>
\r
44 * <p>More information about the composition of the bit sequence can
\r
46 * <a href="http://www.icu-project.org/userguide/Collate_ServiceArchitecture.html">
\r
47 * user guide</a>.</p>
\r
49 * <p>The following example shows how <code>CollationKey</code>s can be used
\r
50 * to sort a list of <code>String</code>s.</p>
\r
53 * // Create an array of CollationKeys for the Strings to be sorted.
\r
54 * Collator myCollator = Collator.getInstance();
\r
55 * CollationKey[] keys = new CollationKey[3];
\r
56 * keys[0] = myCollator.getCollationKey("Tom");
\r
57 * keys[1] = myCollator.getCollationKey("Dick");
\r
58 * keys[2] = myCollator.getCollationKey("Harry");
\r
63 * // Inside body of sort routine, compare keys this way
\r
64 * if( keys[i].compareTo( keys[j] ) > 0 )
\r
65 * // swap keys[i] and keys[j]
\r
69 * // Finally, when we've returned from sort.
\r
70 * System.out.println( keys[0].getSourceString() );
\r
71 * System.out.println( keys[1].getSourceString() );
\r
72 * System.out.println( keys[2].getSourceString() );
\r
77 * This class is not subclassable
\r
80 * @see RuleBasedCollator
\r
83 public final class CollationKey implements Comparable {
\r
87 final java.text.CollationKey key;
\r
92 CollationKey(java.text.CollationKey delegate) {
\r
93 this.key = delegate;
\r
97 * Return the source string that this CollationKey represents.
\r
98 * @return source string that this CollationKey represents
\r
101 public String getSourceString() {
\r
102 return key.getSourceString();
\r
106 * <p>Duplicates and returns the value of this CollationKey as a sequence
\r
107 * of big-endian bytes terminated by a null.</p>
\r
109 * <p>If two CollationKeys can be legitimately compared, then one can
\r
110 * compare the byte arrays of each to obtain the same result, e.g.
\r
112 * byte key1[] = collationkey1.toByteArray();
\r
113 * byte key2[] = collationkey2.toByteArray();
\r
114 * int key, targetkey;
\r
117 * key = key1[i] & 0xFF;
\r
118 * targetkey = key2[i] & 0xFF;
\r
119 * if (key < targetkey) {
\r
120 * System.out.println("String 1 is less than string 2");
\r
123 * if (targetkey < key) {
\r
124 * System.out.println("String 1 is more than string 2");
\r
127 * } while (key != 0 && targetKey != 0);
\r
129 * System.out.println("Strings are equal.");
\r
132 * @return CollationKey value in a sequence of big-endian byte bytes
\r
133 * terminated by a null.
\r
136 public byte[] toByteArray() {
\r
137 return key.toByteArray();
\r
141 * <p>Compare this CollationKey to another CollationKey. The
\r
142 * collation rules of the Collator that created this key are
\r
145 * <p><strong>Note:</strong> Comparison between CollationKeys
\r
146 * created by different Collators might return incorrect
\r
147 * results. See class documentation.</p>
\r
149 * @param target target CollationKey
\r
150 * @return an integer value. If the value is less than zero this CollationKey
\r
151 * is less than than target, if the value is zero they are equal, and
\r
152 * if the value is greater than zero this CollationKey is greater
\r
154 * @exception NullPointerException is thrown if argument is null.
\r
155 * @see Collator#compare(String, String)
\r
158 public int compareTo(CollationKey target) {
\r
159 return key.compareTo(target.key);
\r
163 * <p>Compare this CollationKey with the specified Object. The
\r
164 * collation rules of the Collator that created this key are
\r
167 * <p>See note in compareTo(CollationKey) for warnings about possible
\r
168 * incorrect results.</p>
\r
170 * @param obj the Object to be compared to.
\r
171 * @return Returns a negative integer, zero, or a positive integer
\r
172 * respectively if this CollationKey is less than, equal to, or
\r
173 * greater than the given Object.
\r
174 * @exception ClassCastException is thrown when the argument is not
\r
175 * a CollationKey. NullPointerException is thrown when the argument
\r
177 * @see #compareTo(CollationKey)
\r
180 public int compareTo(Object o) {
\r
181 return compareTo((CollationKey)o);
\r
185 * <p>Compare this CollationKey and the specified Object for
\r
186 * equality. The collation rules of the Collator that created
\r
187 * this key are applied.</p>
\r
189 * <p>See note in compareTo(CollationKey) for warnings about
\r
190 * possible incorrect results.</p>
\r
192 * @param target the object to compare to.
\r
193 * @return true if the two keys compare as equal, false otherwise.
\r
194 * @see #compareTo(CollationKey)
\r
195 * @exception ClassCastException is thrown when the argument is not
\r
196 * a CollationKey. NullPointerException is thrown when the argument
\r
200 public boolean equals(Object target) {
\r
202 return key.equals(((CollationKey)target).key);
\r
204 catch (Exception e) {
\r
211 * Compare this CollationKey and the argument target CollationKey for
\r
214 * rules of the Collator object which created these objects are applied.
\r
217 * See note in compareTo(CollationKey) for warnings of incorrect results
\r
219 * @param target the CollationKey to compare to.
\r
220 * @return true if two objects are equal, false otherwise.
\r
221 * @exception NullPointerException is thrown when the argument is null.
\r
224 public boolean equals(CollationKey target) {
\r
225 return key.equals(target.key);
\r
229 * <p>Returns a hash code for this CollationKey. The hash value is calculated
\r
230 * on the key itself, not the String from which the key was created. Thus
\r
231 * if x and y are CollationKeys, then x.hashCode(x) == y.hashCode()
\r
232 * if x.equals(y) is true. This allows language-sensitive comparison in a
\r
235 * @return the hash value.
\r
238 public int hashCode() {
\r
239 return key.hashCode();
\r
243 * Return a description of the CollationKey.
\r
244 * @return a description of the CollationKey, used for debugging
\r
245 * @stable ICU 3.4.2
\r
247 public String toString() {
\r
248 return key.toString();
\r