001 /* CollationKey.java -- Precomputed collation value 002 Copyright (C) 1998, 1999, 2000, 2003, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package java.text; 040 041 import java.util.Arrays; 042 043 /* Written using "Java Class Libraries", 2nd edition, plus online 044 * API docs for JDK 1.2 from http://www.javasoft.com. 045 * Status: Believed complete and correct. 046 */ 047 048 /** 049 * This class represents a pre-computed series of bits representing a 050 * <code>String</code> for under a particular <code>Collator</code>. This 051 * value may be compared bitwise against another <code>CollationKey</code> 052 * representing a different <code>String</code> under the same 053 * <code>Collator</code> in a manner than is usually more efficient than 054 * using the raw <code>Collator</code> compare methods. There is overhead 055 * associated with calculating this value, so it is generally not 056 * advisable to compute <code>CollationKey</code>'s unless multiple 057 * comparisons against a <code>String</code> will be done. (For example, 058 * in a sort routine). 059 * <p> 060 * This class cannot be instantiated directly. Instead, a 061 * <code>CollationKey</code> is created by calling the 062 * <code>getCollationKey</code> method on an instance of <code>Collator</code>. 063 * 064 * @author Aaron M. Renn (arenn@urbanophile.com) 065 * @author Tom Tromey (tromey@cygnus.com) 066 * @date March 25, 1999 067 */ 068 public class CollationKey implements Comparable<CollationKey> 069 { 070 /** 071 * This is the <code>Collator</code> this object was created from. 072 */ 073 private Collator collator; 074 075 /** 076 * This is the <code>String</code> this object represents. 077 */ 078 private String originalText; 079 080 /** 081 * This is the bit value for this key. 082 */ 083 private byte[] key; 084 085 CollationKey (Collator collator, String originalText, byte[] key) 086 { 087 this.collator = collator; 088 this.originalText = originalText; 089 this.key = key; 090 } 091 092 /** 093 * This method compares the specified object to this one. An integer is 094 * returned which indicates whether the specified object is less than, 095 * greater than, or equal to this object. 096 * 097 * @param ck The <code>CollationKey</code> to compare against this one. 098 * 099 * @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. 100 */ 101 public int compareTo (CollationKey ck) 102 { 103 int max = Math.min (key.length, ck.key.length); 104 105 for (int i = 0; i < max; ++i) 106 { 107 if (key[i] != ck.key[i]) 108 return key[i] - ck.key[i]; 109 } 110 111 return key.length - ck.key.length; 112 } 113 114 /** 115 * This method tests the specified <code>Object</code> for equality with 116 * this object. This will be true if and only if: 117 * <p> 118 * <ul> 119 * <li>The specified object must not be <code>null</code></li> 120 * <li>The specified object is an instance of <code>CollationKey</code>.</li> 121 * <li>The specified object was created from the same <code>Collator</code> 122 * as this object.</li> 123 * <li>The specified object has the same source string and bit key as 124 * this object.</li> 125 * </ul> 126 * 127 * @param obj The <code>Object</code> to test for equality. 128 * 129 * @return <code>true</code> if the specified object is equal to this one, <code>false</code> otherwise. 130 */ 131 public boolean equals (Object obj) 132 { 133 if (! (obj instanceof CollationKey)) 134 return false; 135 136 CollationKey ck = (CollationKey) obj; 137 138 if (ck.collator != collator) 139 return false; 140 141 if (!ck.getSourceString ().equals (getSourceString ())) 142 return false; 143 144 if (! Arrays.equals (ck.toByteArray (), toByteArray ())) 145 return false; 146 147 return true; 148 } 149 150 /** 151 * This method returns the <code>String</code> that this object was created 152 * from. 153 * 154 * @return The source <code>String</code> for this object. 155 */ 156 public String getSourceString() 157 { 158 return originalText; 159 } 160 161 /** 162 * This method returns a hash value for this object. The hash value 163 * returned will be the hash code of the bit key so that identical bit 164 * keys will return the same value. 165 * 166 * @return A hash value for this object. 167 */ 168 public int hashCode() 169 { 170 // We just follow BitSet instead of thinking up something new. 171 long h = originalText.hashCode(); 172 for (int i = key.length - 1; i >= 0; --i) 173 h ^= key[i] * (i + 1); 174 return (int) ((h >> 32) ^ h); 175 } 176 177 /** 178 * This method returns the collation bit sequence as a byte array. 179 * 180 * @return A byte array containing the collation bit sequence. 181 */ 182 public byte[] toByteArray() 183 { 184 return key; 185 } 186 }