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