1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/**
*******************************************************************************
* Copyright (C) 1996-2005, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
*
*******************************************************************************
*/
package libcore.icu;
import java.text.CharacterIterator;
import java.text.CollationKey;
import java.text.ParseException;
import java.util.Locale;
public final class RuleBasedCollatorICU implements Cloneable {
// Values from the native UColAttributeValue enum.
public static final int VALUE_DEFAULT = -1;
public static final int VALUE_PRIMARY = 0;
public static final int VALUE_SECONDARY = 1;
public static final int VALUE_TERTIARY = 2;
public static final int VALUE_DEFAULT_STRENGTH = VALUE_TERTIARY;
public static final int VALUE_QUATERNARY = 3;
public static final int VALUE_IDENTICAL = 15;
public static final int VALUE_OFF = 16;
public static final int VALUE_ON = 17;
public static final int VALUE_SHIFTED = 20;
public static final int VALUE_NON_IGNORABLE = 21;
public static final int VALUE_LOWER_FIRST = 24;
public static final int VALUE_UPPER_FIRST = 25;
public static final int VALUE_ON_WITHOUT_HANGUL = 28;
public static final int VALUE_ATTRIBUTE_VALUE_COUNT = 29;
// Values from the UColAttribute enum.
public static final int FRENCH_COLLATION = 0;
public static final int ALTERNATE_HANDLING = 1;
public static final int CASE_FIRST = 2;
public static final int CASE_LEVEL = 3;
public static final int DECOMPOSITION_MODE = 4;
public static final int STRENGTH = 5;
// The address of the ICU4C native peer.
private final long address;
public RuleBasedCollatorICU(String rules) throws ParseException {
if (rules == null) {
throw new NullPointerException("rules == null");
}
address = NativeCollation.openCollatorFromRules(rules, VALUE_OFF, VALUE_DEFAULT_STRENGTH);
}
public RuleBasedCollatorICU(Locale locale) {
address = NativeCollation.openCollator(locale);
}
private RuleBasedCollatorICU(long address) {
this.address = address;
}
public Object clone() {
return new RuleBasedCollatorICU(NativeCollation.safeClone(address));
}
public int compare(String source, String target) {
return NativeCollation.compare(address, source, target);
}
public int getDecomposition() {
return NativeCollation.getAttribute(address, DECOMPOSITION_MODE);
}
public void setDecomposition(int mode) {
NativeCollation.setAttribute(address, DECOMPOSITION_MODE, mode);
}
public int getStrength() {
return NativeCollation.getAttribute(address, STRENGTH);
}
public void setStrength(int strength) {
NativeCollation.setAttribute(address, STRENGTH, strength);
}
public void setAttribute(int type, int value) {
NativeCollation.setAttribute(address, type, value);
}
public int getAttribute(int type) {
return NativeCollation.getAttribute(address, type);
}
public CollationKey getCollationKey(String source) {
if (source == null) {
return null;
}
byte[] key = NativeCollation.getSortKey(address, source);
if (key == null) {
return null;
}
return new CollationKeyICU(source, key);
}
public String getRules() {
return NativeCollation.getRules(address);
}
public CollationElementIteratorICU getCollationElementIterator(String source) {
return CollationElementIteratorICU.getInstance(address, source);
}
public CollationElementIteratorICU getCollationElementIterator(CharacterIterator it) {
// We only implement the String-based API, so build a string from the iterator.
return getCollationElementIterator(characterIteratorToString(it));
}
private String characterIteratorToString(CharacterIterator it) {
StringBuilder result = new StringBuilder();
for (char ch = it.current(); ch != CharacterIterator.DONE; ch = it.next()) {
result.append(ch);
}
return result.toString();
}
@Override public int hashCode() {
return 42; // No-one uses RuleBasedCollatorICU as a hash key.
}
public boolean equals(String source, String target) {
return (compare(source, target) == 0);
}
@Override public boolean equals(Object object) {
if (object == this) {
return true;
}
if (!(object instanceof RuleBasedCollatorICU)) {
return false;
}
RuleBasedCollatorICU rhs = (RuleBasedCollatorICU) object;
return getRules().equals(rhs.getRules()) &&
getStrength() == rhs.getStrength() &&
getDecomposition() == rhs.getDecomposition();
}
@Override protected void finalize() throws Throwable {
try {
NativeCollation.closeCollator(address);
} finally {
super.finalize();
}
}
}
|