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