136 |
*/ |
*/ |
137 |
public class RuleBasedCollator extends Collator |
public class RuleBasedCollator extends Collator |
138 |
{ |
{ |
139 |
class CollationElement |
final class CollationElement |
140 |
{ |
{ |
141 |
String char_seq; |
String char_seq; |
142 |
int primary; |
int primary; |
150 |
this.secondary = secondary; |
this.secondary = secondary; |
151 |
this.tertiary = tertiary; |
this.tertiary = tertiary; |
152 |
} |
} |
153 |
|
|
154 |
} // inner class CollationElement |
} // inner class CollationElement |
155 |
|
|
156 |
/** |
/** |
162 |
* This is the table of collation element values |
* This is the table of collation element values |
163 |
*/ |
*/ |
164 |
private Object[] ce_table; |
private Object[] ce_table; |
165 |
|
|
166 |
/** |
/** |
167 |
* This method initializes a new instance of <code>RuleBasedCollator</code> |
* This method initializes a new instance of <code>RuleBasedCollator</code> |
168 |
* with the specified collation rules. Note that an application normally |
* with the specified collation rules. Note that an application normally |
308 |
* the second. The value depends not only on the collation rules in |
* the second. The value depends not only on the collation rules in |
309 |
* effect, but also the strength and decomposition settings of this object. |
* effect, but also the strength and decomposition settings of this object. |
310 |
* |
* |
311 |
* @param s1 The first <code>String</code> to compare. |
* @param source The first <code>String</code> to compare. |
312 |
* @param s2 A second <code>String</code> to compare to the first. |
* @param target A second <code>String</code> to compare to the first. |
313 |
* |
* |
314 |
* @return A negative integer if s1 < s2, a positive integer |
* @return A negative integer if source < target, a positive integer |
315 |
* if s1 > s2, or 0 if s1 == s2. |
* if source > target, or 0 if source == target. |
316 |
*/ |
*/ |
317 |
public int compare (String s1, String s2) |
public int compare (String source, String target) |
318 |
{ |
{ |
319 |
CollationElementIterator cei1 = getCollationElementIterator(s1); |
CollationElementIterator cei1 = getCollationElementIterator (source); |
320 |
CollationElementIterator cei2 = getCollationElementIterator(s2); |
CollationElementIterator cei2 = getCollationElementIterator (target); |
321 |
|
|
322 |
for(;;) |
for(;;) |
323 |
{ |
{ |
396 |
* |
* |
397 |
* @return A <code>CollationElementIterator</code> for the specified <code>String</code>. |
* @return A <code>CollationElementIterator</code> for the specified <code>String</code>. |
398 |
*/ |
*/ |
399 |
public CollationElementIterator getCollationElementIterator (String str) |
public CollationElementIterator getCollationElementIterator (String source) |
400 |
{ |
{ |
401 |
return new CollationElementIterator (this, str); |
return new CollationElementIterator (source, this); |
402 |
} |
} |
403 |
|
|
404 |
/** |
/** |
410 |
* |
* |
411 |
* @return A <code>CollationElementIterator</code> for the specified <code>String</code>. |
* @return A <code>CollationElementIterator</code> for the specified <code>String</code>. |
412 |
*/ |
*/ |
413 |
public CollationElementIterator getCollationElementIterator(CharacterIterator ci) |
public CollationElementIterator getCollationElementIterator (CharacterIterator source) |
414 |
{ |
{ |
415 |
StringBuffer sb = new StringBuffer(""); |
StringBuffer sb = new StringBuffer(""); |
416 |
|
|
417 |
// Right now we assume that we will read from the beginning of the string. |
// Right now we assume that we will read from the beginning of the string. |
418 |
char c = ci.first(); |
for (char c = source.first(); c != CharacterIterator.DONE; c = source.next()) |
|
while (c != CharacterIterator.DONE) |
|
419 |
{ |
{ |
420 |
sb.append(c); |
sb.append (c); |
|
c = ci.next(); |
|
421 |
} |
} |
422 |
|
|
423 |
return(getCollationElementIterator(sb.toString())); |
return getCollationElementIterator (sb.toString()); |
424 |
} |
} |
425 |
|
|
426 |
/** |
/** |
434 |
* |
* |
435 |
* @return A <code>CollationKey</code> for the specified <code>String</code>. |
* @return A <code>CollationKey</code> for the specified <code>String</code>. |
436 |
*/ |
*/ |
437 |
public CollationKey getCollationKey (String str) |
public CollationKey getCollationKey (String source) |
438 |
{ |
{ |
439 |
CollationElementIterator cei = getCollationElementIterator(str); |
CollationElementIterator cei = getCollationElementIterator (source); |
440 |
Vector vect = new Vector(25); |
Vector vect = new Vector(25); |
441 |
|
|
442 |
int ord = cei.next(); |
int ord = cei.next(); |
473 |
key [i * 4 + 3] = (byte)(j & 0x000000FF); |
key [i * 4 + 3] = (byte)(j & 0x000000FF); |
474 |
} |
} |
475 |
|
|
476 |
return(new CollationKey(this, str, key)); |
return new CollationKey (this, source, key); |
477 |
} |
} |
478 |
|
|
479 |
/** |
/** |
484 |
*/ |
*/ |
485 |
public String getRules() |
public String getRules() |
486 |
{ |
{ |
487 |
return(rules); |
return rules; |
488 |
} |
} |
489 |
|
|
490 |
/** |
/** |
494 |
*/ |
*/ |
495 |
public int hashCode() |
public int hashCode() |
496 |
{ |
{ |
497 |
return(System.identityHashCode(this)); |
return System.identityHashCode (this); |
498 |
} |
} |
499 |
|
|
500 |
/** |
/** |
501 |
* This method calculates the collation element value for the specified |
* This method calculates the collation element value for the specified |
502 |
* character(s). |
* character(s). |
503 |
*/ |
*/ |
504 |
int getCollationElementValue(String str) |
int getCollationElementValue (String source) |
505 |
{ |
{ |
506 |
CollationElement e = null; |
CollationElement e = null; |
507 |
|
|
508 |
// The table is sorted. Change to a binary search later. |
// The table is sorted. Change to a binary search later. |
509 |
for (int i = 0; i < ce_table.length; i++) |
for (int i = 0; i < ce_table.length; i++) |
510 |
if (((CollationElement)ce_table[i]).char_seq.equals(str)) |
if (((CollationElement) ce_table [i]).char_seq.equals (source)) |
511 |
{ |
{ |
512 |
e = (CollationElement)ce_table[i]; |
e = (CollationElement) ce_table [i]; |
513 |
break; |
break; |
514 |
} |
} |
515 |
|
|
516 |
if (e == null) |
if (e == null) |
517 |
e = new CollationElement(str, 0xFFFF, (short)0xFF, (short)0xFF); |
e = new CollationElement (source, 0xFFFF, (short)0xFF, (short)0xFF); |
518 |
|
|
519 |
int retval = (e.primary << 16) + (e.secondary << 8) + e.tertiary; |
int retval = (e.primary << 16) + (e.secondary << 8) + e.tertiary; |
520 |
|
|
521 |
return(retval); |
return retval; |
522 |
} |
} |
523 |
|
|
524 |
} // class RuleBaseCollator |
} // class RuleBaseCollator |