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