1. /*
  2. * @(#)CollationKey.java 1.14 00/01/19
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. /*
  11. * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  12. * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  13. *
  14. * The original version of this source code and documentation is copyrighted
  15. * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  16. * materials are provided under terms of a License Agreement between Taligent
  17. * and Sun. This technology is protected by multiple US and International
  18. * patents. This notice and attribution to Taligent may not be removed.
  19. * Taligent is a registered trademark of Taligent, Inc.
  20. *
  21. */
  22. package java.text;
  23. /**
  24. * A <code>CollationKey</code> represents a <code>String</code> under the
  25. * rules of a specific <code>Collator</code> object. Comparing two
  26. * <code>CollationKey</code>s returns the relative order of the
  27. * <code>String</code>s they represent. Using <code>CollationKey</code>s
  28. * to compare <code>String</code>s is generally faster than using
  29. * <code>Collator.compare</code>. Thus, when the <code>String</code>s
  30. * must be compared multiple times, for example when sorting a list
  31. * of <code>String</code>s. It's more efficient to use <code>CollationKey</code>s.
  32. *
  33. * <p>
  34. * You can not create <code>CollationKey</code>s directly. Rather,
  35. * generate them by calling <code>Collator.getCollationKey</code>.
  36. * You can only compare <code>CollationKey</code>s generated from
  37. * the same <code>Collator</code> object.
  38. *
  39. * <p>
  40. * Generating a <code>CollationKey</code> for a <code>String</code>
  41. * involves examining the entire <code>String</code>
  42. * and converting it to series of bits that can be compared bitwise. This
  43. * allows fast comparisons once the keys are generated. The cost of generating
  44. * keys is recouped in faster comparisons when <code>String</code>s need
  45. * to be compared many times. On the other hand, the result of a comparison
  46. * is often determined by the first couple of characters of each <code>String</code>.
  47. * <code>Collator.compare</code> examines only as many characters as it needs which
  48. * allows it to be faster when doing single comparisons.
  49. * <p>
  50. * The following example shows how <code>CollationKey</code>s might be used
  51. * to sort a list of <code>String</code>s.
  52. * <blockquote>
  53. * <pre>
  54. * // Create an array of CollationKeys for the Strings to be sorted.
  55. * Collator myCollator = Collator.getInstance();
  56. * CollationKey[] keys = new CollationKey[3];
  57. * keys[0] = myCollator.getCollationKey("Tom");
  58. * keys[1] = myCollator.getCollationKey("Dick");
  59. * keys[2] = myCollator.getCollationKey("Harry");
  60. * sort( keys );
  61. * <br>
  62. * //...
  63. * <br>
  64. * // Inside body of sort routine, compare keys this way
  65. * if( keys[i].compareTo( keys[j] ) > 0 )
  66. * // swap keys[i] and keys[j]
  67. * <br>
  68. * //...
  69. * <br>
  70. * // Finally, when we've returned from sort.
  71. * System.out.println( keys[0].getSourceString() );
  72. * System.out.println( keys[1].getSourceString() );
  73. * System.out.println( keys[2].getSourceString() );
  74. * </pre>
  75. * </blockquote>
  76. *
  77. * @see Collator
  78. * @see RuleBasedCollator
  79. * @version 1.14, 01/19/00
  80. * @author Helena Shih
  81. */
  82. public final class CollationKey implements Comparable {
  83. /**
  84. * Compare this CollationKey to the target CollationKey. The collation rules of the
  85. * Collator object which created these keys are applied. <strong>Note:</strong>
  86. * CollationKeys created by different Collators can not be compared.
  87. * @param target target CollationKey
  88. * @return Returns an integer value. Value is less than zero if this is less
  89. * than target, value is zero if this and target are equal and value is greater than
  90. * zero if this is greater than target.
  91. * @see java.text.Collator#compare
  92. */
  93. public int compareTo(CollationKey target)
  94. {
  95. int result = key.compareTo(target.key);
  96. if (result <= Collator.LESS)
  97. return Collator.LESS;
  98. else if (result >= Collator.GREATER)
  99. return Collator.GREATER;
  100. return Collator.EQUAL;
  101. }
  102. /**
  103. * Compares this CollationKey with the specified Object for order. Returns
  104. * a negative integer, zero, or a positive integer as this CollationKey
  105. * is less than, equal to, or greater than the given Object.
  106. *
  107. * @param o the Object to be compared.
  108. * @return a negative integer, zero, or a positive integer as this
  109. * Collation Key is less than, equal to, or greater than the given
  110. * Object.
  111. * @exception ClassCastException the specified Object is not a
  112. * CollationKey.
  113. * @see Comparable
  114. * @since 1.2
  115. */
  116. public int compareTo(Object o) {
  117. return compareTo((CollationKey)o);
  118. }
  119. /**
  120. * Compare this CollationKey and the target CollationKey for equality.
  121. * The collation rules of the Collator object which created these keys are applied.
  122. * <strong>Note:</strong> CollationKeys created by different Collators can not be
  123. * compared.
  124. * @param target the CollationKey to compare to.
  125. * @return Returns true if two objects are equal, false otherwise.
  126. */
  127. public boolean equals(Object target) {
  128. if (this == target) return true;
  129. if (target == null || !getClass().equals(target.getClass())) {
  130. return false;
  131. }
  132. CollationKey other = (CollationKey)target;
  133. return key.equals(other.key);
  134. }
  135. /**
  136. * Creates a hash code for this CollationKey. The hash value is calculated on the
  137. * key itself, not the String from which the key was created. Thus
  138. * if x and y are CollationKeys, then x.hashCode(x) == y.hashCode() if
  139. * x.equals(y) is true. This allows language-sensitive comparison in a hash table.
  140. * See the CollatinKey class description for an example.
  141. * @return the hash value based on the string's collation order.
  142. */
  143. public int hashCode() {
  144. return (key.hashCode());
  145. }
  146. /**
  147. * Returns the String that this CollationKey represents.
  148. */
  149. public String getSourceString() {
  150. return source;
  151. }
  152. /**
  153. * Converts the CollationKey to a sequence of bits. If two CollationKeys
  154. * could be legitimately compared, then one could compare the byte arrays
  155. * for each of those keys to obtain the same result. Byte arrays are
  156. * organized most significant byte first.
  157. */
  158. public byte[] toByteArray() {
  159. char[] src = key.toCharArray();
  160. byte[] dest = new byte[ 2*src.length ];
  161. int j = 0;
  162. for( int i=0; i<src.length; i++ ) {
  163. dest[j++] = (byte)(src[i] >>> 8);
  164. dest[j++] = (byte)(src[i] & 0x00ff);
  165. }
  166. return dest;
  167. }
  168. /**
  169. * A CollationKey can only be generated by Collator objects.
  170. */
  171. CollationKey(String source, String key) {
  172. this.source = source;
  173. this.key = key;
  174. }
  175. private String source = null;
  176. private String key = null;
  177. }