CollationKey.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* CollationKey.java -- Precomputed collation value
  2. Copyright (C) 1998, 1999, 2000, 2003, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.text;
  32. import java.util.Arrays;
  33. /* Written using "Java Class Libraries", 2nd edition, plus online
  34. * API docs for JDK 1.2 from http://www.javasoft.com.
  35. * Status: Believed complete and correct.
  36. */
  37. /**
  38. * This class represents a pre-computed series of bits representing a
  39. * <code>String</code> for under a particular <code>Collator</code>. This
  40. * value may be compared bitwise against another <code>CollationKey</code>
  41. * representing a different <code>String</code> under the same
  42. * <code>Collator</code> in a manner than is usually more efficient than
  43. * using the raw <code>Collator</code> compare methods. There is overhead
  44. * associated with calculating this value, so it is generally not
  45. * advisable to compute <code>CollationKey</code>'s unless multiple
  46. * comparisons against a <code>String</code> will be done. (For example,
  47. * in a sort routine).
  48. * <p>
  49. * This class cannot be instantiated directly. Instead, a
  50. * <code>CollationKey</code> is created by calling the
  51. * <code>getCollationKey</code> method on an instance of <code>Collator</code>.
  52. *
  53. * @author Aaron M. Renn (arenn@urbanophile.com)
  54. * @author Tom Tromey (tromey@cygnus.com)
  55. * @date March 25, 1999
  56. */
  57. public class CollationKey implements Comparable<CollationKey>
  58. {
  59. /**
  60. * This is the <code>Collator</code> this object was created from.
  61. */
  62. private Collator collator;
  63. /**
  64. * This is the <code>String</code> this object represents.
  65. */
  66. private String originalText;
  67. /**
  68. * This is the bit value for this key.
  69. */
  70. private byte[] key;
  71. CollationKey (Collator collator, String originalText, byte[] key)
  72. {
  73. this.collator = collator;
  74. this.originalText = originalText;
  75. this.key = key;
  76. }
  77. /**
  78. * This method compares the specified object to this one. An integer is
  79. * returned which indicates whether the specified object is less than,
  80. * greater than, or equal to this object.
  81. *
  82. * @param ck The <code>CollationKey</code> to compare against this one.
  83. *
  84. * @return A negative integer if this object is less than the specified object, 0 if it is equal or a positive integer if it is greater than the specified object.
  85. */
  86. public int compareTo (CollationKey ck)
  87. {
  88. int max = Math.min (key.length, ck.key.length);
  89. for (int i = 0; i < max; ++i)
  90. {
  91. if (key[i] != ck.key[i])
  92. return key[i] - ck.key[i];
  93. }
  94. return key.length - ck.key.length;
  95. }
  96. /**
  97. * This method tests the specified <code>Object</code> for equality with
  98. * this object. This will be true if and only if:
  99. * <p>
  100. * <ul>
  101. * <li>The specified object must not be <code>null</code></li>
  102. * <li>The specified object is an instance of <code>CollationKey</code>.</li>
  103. * <li>The specified object was created from the same <code>Collator</code>
  104. * as this object.</li>
  105. * <li>The specified object has the same source string and bit key as
  106. * this object.</li>
  107. * </ul>
  108. *
  109. * @param obj The <code>Object</code> to test for equality.
  110. *
  111. * @return <code>true</code> if the specified object is equal to this one, <code>false</code> otherwise.
  112. */
  113. public boolean equals (Object obj)
  114. {
  115. if (! (obj instanceof CollationKey))
  116. return false;
  117. CollationKey ck = (CollationKey) obj;
  118. if (ck.collator != collator)
  119. return false;
  120. if (!ck.getSourceString ().equals (getSourceString ()))
  121. return false;
  122. if (! Arrays.equals (ck.toByteArray (), toByteArray ()))
  123. return false;
  124. return true;
  125. }
  126. /**
  127. * This method returns the <code>String</code> that this object was created
  128. * from.
  129. *
  130. * @return The source <code>String</code> for this object.
  131. */
  132. public String getSourceString()
  133. {
  134. return originalText;
  135. }
  136. /**
  137. * This method returns a hash value for this object. The hash value
  138. * returned will be the hash code of the bit key so that identical bit
  139. * keys will return the same value.
  140. *
  141. * @return A hash value for this object.
  142. */
  143. public int hashCode()
  144. {
  145. // We just follow BitSet instead of thinking up something new.
  146. long h = originalText.hashCode();
  147. for (int i = key.length - 1; i >= 0; --i)
  148. h ^= key[i] * (i + 1);
  149. return (int) ((h >> 32) ^ h);
  150. }
  151. /**
  152. * This method returns the collation bit sequence as a byte array.
  153. *
  154. * @return A byte array containing the collation bit sequence.
  155. */
  156. public byte[] toByteArray()
  157. {
  158. return key;
  159. }
  160. }