39 |
package java.text; |
package java.text; |
40 |
|
|
41 |
/** |
/** |
42 |
* This class represents a pre-computed series of bits representing a |
* This class represents a pre-computed series of bits representing a |
43 |
* <code>String</code> for under a particular <code>Collator</code>. This |
* <code>String</code> for under a particular <code>Collator</code>. This |
44 |
* value may be compared bitwise against another <code>CollationKey</code> |
* value may be compared bitwise against another <code>CollationKey</code> |
45 |
* representing a different <code>String</code> under the same |
* representing a different <code>String</code> under the same |
46 |
* <code>Collator</code> in a manner than is usually more efficient than |
* <code>Collator</code> in a manner than is usually more efficient than |
47 |
* using the raw <code>Collator</code> compare methods. There is overhead |
* using the raw <code>Collator</code> compare methods. There is overhead |
48 |
* associated with calculating this value, so it is generally not |
* associated with calculating this value, so it is generally not |
49 |
* advisable to compute <code>CollationKey</code>'s unless multiple |
* advisable to compute <code>CollationKey</code>'s unless multiple |
50 |
* comparisons against a <code>String</code> will be done. (For example, |
* comparisons against a <code>String</code> will be done. (For example, |
51 |
* in a sort routine). |
* in a sort routine). |
52 |
* <p> |
* <p> |
53 |
* This class cannot be instantiated directly. Instead, a |
* This class cannot be instantiated directly. Instead, a |
54 |
* <code>CollationKey</code> is created by calling the |
* <code>CollationKey</code> is created by calling the |
55 |
* <code>getCollationKey</code> method on an instance of <code>Collator</code>. |
* <code>getCollationKey</code> method on an instance of <code>Collator</code>. |
56 |
* |
* |
57 |
* @version 0.0 |
* @author Aaron M. Renn <arenn@urbanophile.com> |
|
* |
|
|
* @author Aaron M. Renn (arenn@urbanophile.com) |
|
|
*/ |
|
|
public final class CollationKey implements Comparable |
|
|
{ |
|
|
|
|
|
/* |
|
|
* Instance Variables |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* This is the <code>Collator</code> this object was created from. |
|
|
*/ |
|
|
private Collator collator; |
|
|
|
|
|
/** |
|
|
* This is the <code>String</code> this object represents. |
|
|
*/ |
|
|
private String str; |
|
|
|
|
|
/** |
|
|
* This is the bit value for this key. |
|
|
*/ |
|
|
private byte[] key; |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors (no public constructors) |
|
58 |
*/ |
*/ |
59 |
|
public final class CollationKey implements Comparable |
|
CollationKey(Collator collator, String str, byte[] key) |
|
|
{ |
|
|
this.collator = collator; |
|
|
this.str = str; |
|
|
this.key = key; |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* This method returns the <code>String</code> that this object was created |
|
|
* from. |
|
|
* |
|
|
* @return The source <code>String</code> for this object. |
|
|
*/ |
|
|
public String |
|
|
getSourceString() |
|
|
{ |
|
|
return(str); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method returns the collation bit sequence as a byte array. |
|
|
* |
|
|
* @param A byte array containing the collation bit sequence. |
|
|
*/ |
|
|
public byte[] |
|
|
toByteArray() |
|
|
{ |
|
|
return(key); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method compares the specified object to this one. The specified |
|
|
* object must be an instance of <code>CollationKey</code> or an exception |
|
|
* will be thrown. An integer is returned which indicates whether the |
|
|
* specified object is less than, greater than, or equal to this object. |
|
|
* |
|
|
* @param obj The <code>Object</code> to compare against this one. |
|
|
* |
|
|
* @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. |
|
|
*/ |
|
|
public int |
|
|
compareTo(Object obj) |
|
|
{ |
|
|
return(compareTo((CollationKey)obj)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method compares the specified object to this one. An integer is |
|
|
* returned which indicates whether the specified object is less than, |
|
|
* greater than, or equal to this object. |
|
|
* |
|
|
* @param ck The <code>CollationKey</code> to compare against this one. |
|
|
* |
|
|
* @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. |
|
|
*/ |
|
|
public int |
|
|
compareTo(CollationKey ck) |
|
|
{ |
|
|
int i; |
|
|
for (i = 0; i < key.length; i++) |
|
|
{ |
|
|
if (ck.key.length <= i) |
|
|
return(1); |
|
|
|
|
|
if (key[i] < ck.key[i]) |
|
|
return(-1); |
|
|
|
|
|
if (key[i] > ck.key[i]) |
|
|
return(1); |
|
|
} |
|
|
|
|
|
if (i == ck.key.length) |
|
|
return(0); |
|
|
else |
|
|
return(-1); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method tests the specified <code>Object</code> for equality with |
|
|
* this object. This will be true if and only if: |
|
|
* <p> |
|
|
* <ul> |
|
|
* <li>The specified object must not be <code>null</code> |
|
|
* <li>The specified object is an instance of <code>CollationKey</code>. |
|
|
* <li>The specified object was created from the same <code>Collator</code> |
|
|
* as this object. |
|
|
* <li>The specified object has the same source string and bit key as |
|
|
* this object. |
|
|
* </ul> |
|
|
* |
|
|
* @param obj The <code>Object</code> to test for equality. |
|
|
* |
|
|
* @return <code>true</code> if the specified object is equal to this one, <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
equals(Object obj) |
|
|
{ |
|
|
if (obj == null) |
|
|
return(false); |
|
|
|
|
|
if (!(obj instanceof CollationKey)) |
|
|
return(false); |
|
|
|
|
|
CollationKey ck = (CollationKey)obj; |
|
|
|
|
|
if (!ck.collator.equals(collator)) |
|
|
return(false); |
|
|
|
|
|
if (!ck.getSourceString().equals(getSourceString())) |
|
|
return(false); |
|
|
|
|
|
if (!ck.toByteArray().equals(toByteArray())) |
|
|
return(false); |
|
|
|
|
|
return(true); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method returns a hash value for this object. The hash value |
|
|
* returned will be the hash code of the bit key so that identical bit |
|
|
* keys will return the same value. |
|
|
* |
|
|
* @return A hash value for this object. |
|
|
*/ |
|
|
public int |
|
|
hashCode() |
|
60 |
{ |
{ |
61 |
return(key.hashCode()); |
/** |
62 |
|
* This is the <code>Collator</code> this object was created from. |
63 |
|
*/ |
64 |
|
private Collator collator; |
65 |
|
|
66 |
|
/** |
67 |
|
* This is the <code>String</code> this object represents. |
68 |
|
*/ |
69 |
|
private String str; |
70 |
|
|
71 |
|
/** |
72 |
|
* This is the bit value for this key. |
73 |
|
*/ |
74 |
|
private byte[] key; |
75 |
|
|
76 |
|
CollationKey(Collator collator, String str, byte[] key) |
77 |
|
{ |
78 |
|
this.collator = collator; |
79 |
|
this.str = str; |
80 |
|
this.key = key; |
81 |
|
} |
82 |
|
|
83 |
|
/** |
84 |
|
* This method returns the <code>String</code> that this object was created |
85 |
|
* from. |
86 |
|
* |
87 |
|
* @return The source <code>String</code> for this object. |
88 |
|
*/ |
89 |
|
public String getSourceString() |
90 |
|
{ |
91 |
|
return(str); |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
|
* This method returns the collation bit sequence as a byte array. |
96 |
|
* |
97 |
|
* @param A byte array containing the collation bit sequence. |
98 |
|
*/ |
99 |
|
public byte[] toByteArray() |
100 |
|
{ |
101 |
|
return(key); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
|
* This method compares the specified object to this one. The specified |
106 |
|
* object must be an instance of <code>CollationKey</code> or an exception |
107 |
|
* will be thrown. An integer is returned which indicates whether the |
108 |
|
* specified object is less than, greater than, or equal to this object. |
109 |
|
* |
110 |
|
* @param obj The <code>Object</code> to compare against this one. |
111 |
|
* |
112 |
|
* @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. |
113 |
|
*/ |
114 |
|
public int compareTo(Object obj) |
115 |
|
{ |
116 |
|
return(compareTo((CollationKey)obj)); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
|
* This method compares the specified object to this one. An integer is |
121 |
|
* returned which indicates whether the specified object is less than, |
122 |
|
* greater than, or equal to this object. |
123 |
|
* |
124 |
|
* @param ck The <code>CollationKey</code> to compare against this one. |
125 |
|
* |
126 |
|
* @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. |
127 |
|
*/ |
128 |
|
public int compareTo(CollationKey ck) |
129 |
|
{ |
130 |
|
int i; |
131 |
|
for (i = 0; i < key.length; i++) |
132 |
|
{ |
133 |
|
if (ck.key.length <= i) |
134 |
|
return(1); |
135 |
|
|
136 |
|
if (key[i] < ck.key[i]) |
137 |
|
return(-1); |
138 |
|
|
139 |
|
if (key[i] > ck.key[i]) |
140 |
|
return(1); |
141 |
|
} |
142 |
|
|
143 |
|
if (i == ck.key.length) |
144 |
|
return(0); |
145 |
|
else |
146 |
|
return(-1); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
|
* This method tests the specified <code>Object</code> for equality with |
151 |
|
* this object. This will be true if and only if: |
152 |
|
* <p> |
153 |
|
* <ul> |
154 |
|
* <li>The specified object must not be <code>null</code> |
155 |
|
* <li>The specified object is an instance of <code>CollationKey</code>. |
156 |
|
* <li>The specified object was created from the same <code>Collator</code> |
157 |
|
* as this object. |
158 |
|
* <li>The specified object has the same source string and bit key as |
159 |
|
* this object. |
160 |
|
* </ul> |
161 |
|
* |
162 |
|
* @param obj The <code>Object</code> to test for equality. |
163 |
|
* |
164 |
|
* @return <code>true</code> if the specified object is equal to this one, <code>false</code> otherwise. |
165 |
|
*/ |
166 |
|
public boolean equals(Object obj) |
167 |
|
{ |
168 |
|
if (obj == null) |
169 |
|
return(false); |
170 |
|
|
171 |
|
if (!(obj instanceof CollationKey)) |
172 |
|
return(false); |
173 |
|
|
174 |
|
CollationKey ck = (CollationKey)obj; |
175 |
|
|
176 |
|
if (!ck.collator.equals(collator)) |
177 |
|
return(false); |
178 |
|
|
179 |
|
if (!ck.getSourceString().equals(getSourceString())) |
180 |
|
return(false); |
181 |
|
|
182 |
|
if (!ck.toByteArray().equals(toByteArray())) |
183 |
|
return(false); |
184 |
|
|
185 |
|
return(true); |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
|
* This method returns a hash value for this object. The hash value |
190 |
|
* returned will be the hash code of the bit key so that identical bit |
191 |
|
* keys will return the same value. |
192 |
|
* |
193 |
|
* @return A hash value for this object. |
194 |
|
*/ |
195 |
|
public int hashCode() |
196 |
|
{ |
197 |
|
return(key.hashCode()); |
198 |
|
} |
199 |
} |
} |
|
|
|
|
} // class CollationKey |
|
|
|
|