1 | // CollationKey.java - Sort key for locale-sensitive String.
|
---|
2 |
|
---|
3 | /* Copyright (C) 1999, 2000 Free Software Foundation
|
---|
4 |
|
---|
5 | This file is part of libgcj.
|
---|
6 |
|
---|
7 | This software is copyrighted work licensed under the terms of the
|
---|
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
9 | details. */
|
---|
10 |
|
---|
11 | package java.text;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * @author Tom Tromey <tromey@cygnus.com>
|
---|
15 | * @date March 25, 1999
|
---|
16 | */
|
---|
17 | /* Written using "Java Class Libraries", 2nd edition, plus online
|
---|
18 | * API docs for JDK 1.2 from http://www.javasoft.com.
|
---|
19 | * Status: Believed complete and correct.
|
---|
20 | */
|
---|
21 |
|
---|
22 | public final class CollationKey implements Comparable
|
---|
23 | {
|
---|
24 | public int compareTo (CollationKey target)
|
---|
25 | {
|
---|
26 | int max = Math.min(key.length, target.key.length);
|
---|
27 |
|
---|
28 | for (int i = 0; i < max; ++i)
|
---|
29 | {
|
---|
30 | if (key[i] != target.key[i])
|
---|
31 | return key[i] - target.key[i];
|
---|
32 | }
|
---|
33 |
|
---|
34 | return key.length - target.key.length;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public int compareTo (Object o)
|
---|
38 | {
|
---|
39 | return compareTo ((CollationKey) o);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public boolean equals (Object obj)
|
---|
43 | {
|
---|
44 | if (! (obj instanceof CollationKey))
|
---|
45 | return false;
|
---|
46 |
|
---|
47 | CollationKey ck = (CollationKey) obj;
|
---|
48 |
|
---|
49 | if (key.length != ck.key.length)
|
---|
50 | return false;
|
---|
51 |
|
---|
52 | for (int i = 0; i < key.length; ++i)
|
---|
53 | if (key[i] != ck.key[i])
|
---|
54 | return false;
|
---|
55 |
|
---|
56 | return true;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public String getSourceString ()
|
---|
60 | {
|
---|
61 | return originalText;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public int hashCode ()
|
---|
65 | {
|
---|
66 | // We just follow BitSet instead of thinking up something new.
|
---|
67 | long h = originalText.hashCode();
|
---|
68 | for (int i = key.length - 1; i >= 0; --i)
|
---|
69 | h ^= key[i] * (i + 1);
|
---|
70 | return (int) ((h >> 32) ^ h);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public byte[] toByteArray ()
|
---|
74 | {
|
---|
75 | byte[] r = new byte[4 * key.length];
|
---|
76 | int off = 0;
|
---|
77 | for (int i = 0; i < key.length; ++i)
|
---|
78 | {
|
---|
79 | r[off++] = (byte) ((key[i] >>> 24) & 255);
|
---|
80 | r[off++] = (byte) ((key[i] >>> 16) & 255);
|
---|
81 | r[off++] = (byte) ((key[i] >>> 8) & 255);
|
---|
82 | r[off++] = (byte) ((key[i] ) & 255);
|
---|
83 | }
|
---|
84 | return r;
|
---|
85 | }
|
---|
86 |
|
---|
87 | CollationKey (CollationElementIterator iter, String originalText,
|
---|
88 | int strength)
|
---|
89 | {
|
---|
90 | this.originalText = originalText;
|
---|
91 |
|
---|
92 | // Compute size of required array.
|
---|
93 | int size = 0;
|
---|
94 | while (RuleBasedCollator.next(iter, strength)
|
---|
95 | != CollationElementIterator.NULLORDER)
|
---|
96 | ++size;
|
---|
97 |
|
---|
98 | iter.reset();
|
---|
99 | key = new int[size];
|
---|
100 | for (int i = 0; i < size; i++)
|
---|
101 | key[i] = RuleBasedCollator.next(iter, strength);
|
---|
102 | }
|
---|
103 |
|
---|
104 | // Original string.
|
---|
105 | private String originalText;
|
---|
106 |
|
---|
107 | // Collation key.
|
---|
108 | private int[] key;
|
---|
109 | }
|
---|