forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeCollation.java
342 lines (311 loc) · 12.3 KB
/
MergeCollation.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
* (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
*
* The original version of this source code and documentation is copyrighted
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
* materials are provided under terms of a License Agreement between Taligent
* and Sun. This technology is protected by multiple US and International
* patents. This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
*/
package java.text;
import java.util.ArrayList;
/**
* Utility class for normalizing and merging patterns for collation.
* Patterns are strings of the form <entry>*, where <entry> has the
* form:
* <pattern> := <entry>*
* <entry> := <separator><chars>{"/"<extension>}
* <separator> := "=", ",", ";", "<", "&"
* <chars>, and <extension> are both arbitrary strings.
* unquoted whitespaces are ignored.
* 'xxx' can be used to quote characters
* One difference from Collator is that & is used to reset to a current
* point. Or, in other words, it introduces a new sequence which is to
* be added to the old.
* That is: "a < b < c < d" is the same as "a < b & b < c & c < d" OR
* "a < b < d & b < c"
* XXX: make '' be a single quote.
* @see PatternEntry
* @author Mark Davis, Helena Shih
*/
final class MergeCollation {
/**
* Creates from a pattern
* @exception ParseException If the input pattern is incorrect.
*/
public MergeCollation(String pattern) throws ParseException
{
for (int i = 0; i < statusArray.length; i++)
statusArray[i] = 0;
setPattern(pattern);
}
/**
* recovers current pattern
*/
public String getPattern() {
return getPattern(true);
}
/**
* recovers current pattern.
* @param withWhiteSpace puts spacing around the entries, and \n
* before & and <
*/
public String getPattern(boolean withWhiteSpace) {
StringBuffer result = new StringBuffer();
PatternEntry tmp = null;
ArrayList<PatternEntry> extList = null;
int i;
for (i = 0; i < patterns.size(); ++i) {
PatternEntry entry = patterns.get(i);
if (entry.extension.length() != 0) {
if (extList == null)
extList = new ArrayList<>();
extList.add(entry);
} else {
if (extList != null) {
PatternEntry last = findLastWithNoExtension(i-1);
for (int j = extList.size() - 1; j >= 0 ; j--) {
tmp = extList.get(j);
tmp.addToBuffer(result, false, withWhiteSpace, last);
}
extList = null;
}
entry.addToBuffer(result, false, withWhiteSpace, null);
}
}
if (extList != null) {
PatternEntry last = findLastWithNoExtension(i-1);
for (int j = extList.size() - 1; j >= 0 ; j--) {
tmp = extList.get(j);
tmp.addToBuffer(result, false, withWhiteSpace, last);
}
extList = null;
}
return result.toString();
}
private final PatternEntry findLastWithNoExtension(int i) {
for (--i;i >= 0; --i) {
PatternEntry entry = patterns.get(i);
if (entry.extension.length() == 0) {
return entry;
}
}
return null;
}
/**
* emits the pattern for collation builder.
* @return emits the string in the format understable to the collation
* builder.
*/
public String emitPattern() {
return emitPattern(true);
}
/**
* emits the pattern for collation builder.
* @param withWhiteSpace puts spacing around the entries, and \n
* before & and <
* @return emits the string in the format understable to the collation
* builder.
*/
public String emitPattern(boolean withWhiteSpace) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < patterns.size(); ++i)
{
PatternEntry entry = patterns.get(i);
if (entry != null) {
entry.addToBuffer(result, true, withWhiteSpace, null);
}
}
return result.toString();
}
/**
* sets the pattern.
*/
public void setPattern(String pattern) throws ParseException
{
patterns.clear();
addPattern(pattern);
}
/**
* adds a pattern to the current one.
* @param pattern the new pattern to be added
*/
public void addPattern(String pattern) throws ParseException
{
if (pattern == null)
return;
PatternEntry.Parser parser = new PatternEntry.Parser(pattern);
PatternEntry entry = parser.next();
while (entry != null) {
fixEntry(entry);
entry = parser.next();
}
}
/**
* gets count of separate entries
* @return the size of pattern entries
*/
public int getCount() {
return patterns.size();
}
/**
* gets count of separate entries
* @param index the offset of the desired pattern entry
* @return the requested pattern entry
*/
public PatternEntry getItemAt(int index) {
return patterns.get(index);
}
//============================================================
// privates
//============================================================
ArrayList<PatternEntry> patterns = new ArrayList<>(); // a list of PatternEntries
private transient PatternEntry saveEntry = null;
private transient PatternEntry lastEntry = null;
// This is really used as a local variable inside fixEntry, but we cache
// it here to avoid newing it up every time the method is called.
private transient StringBuffer excess = new StringBuffer();
//
// When building a MergeCollation, we need to do lots of searches to see
// whether a given entry is already in the table. Since we're using an
// array, this would make the algorithm O(N*N). To speed things up, we
// use this bit array to remember whether the array contains any entries
// starting with each Unicode character. If not, we can avoid the search.
// Using BitSet would make this easier, but it's significantly slower.
//
private transient byte[] statusArray = new byte[8192];
private final byte BITARRAYMASK = (byte)0x1;
private final int BYTEPOWER = 3;
private final int BYTEMASK = (1 << BYTEPOWER) - 1;
/*
If the strength is RESET, then just change the lastEntry to
be the current. (If the current is not in patterns, signal an error).
If not, then remove the current entry, and add it after lastEntry
(which is usually at the end).
*/
private final void fixEntry(PatternEntry newEntry) throws ParseException
{
// check to see whether the new entry has the same characters as the previous
// entry did (this can happen when a pattern declaring a difference between two
// strings that are canonically equivalent is normalized). If so, and the strength
// is anything other than IDENTICAL or RESET, throw an exception (you can't
// declare a string to be unequal to itself). --rtg 5/24/99
if (lastEntry != null && newEntry.chars.equals(lastEntry.chars)
&& newEntry.extension.equals(lastEntry.extension)) {
if (newEntry.strength != Collator.IDENTICAL
&& newEntry.strength != PatternEntry.RESET) {
throw new ParseException("The entries " + lastEntry + " and "
+ newEntry + " are adjacent in the rules, but have conflicting "
+ "strengths: A character can't be unequal to itself.", -1);
} else {
// otherwise, just skip this entry and behave as though you never saw it
return;
}
}
boolean changeLastEntry = true;
if (newEntry.strength != PatternEntry.RESET) {
int oldIndex = -1;
if ((newEntry.chars.length() == 1)) {
char c = newEntry.chars.charAt(0);
int statusIndex = c >> BYTEPOWER;
byte bitClump = statusArray[statusIndex];
byte setBit = (byte)(BITARRAYMASK << (c & BYTEMASK));
if (bitClump != 0 && (bitClump & setBit) != 0) {
oldIndex = patterns.lastIndexOf(newEntry);
} else {
// We're going to add an element that starts with this
// character, so go ahead and set its bit.
statusArray[statusIndex] = (byte)(bitClump | setBit);
}
} else {
oldIndex = patterns.lastIndexOf(newEntry);
}
if (oldIndex != -1) {
patterns.remove(oldIndex);
}
excess.setLength(0);
int lastIndex = findLastEntry(lastEntry, excess);
if (excess.length() != 0) {
newEntry.extension = excess + newEntry.extension;
if (lastIndex != patterns.size()) {
lastEntry = saveEntry;
changeLastEntry = false;
}
}
if (lastIndex == patterns.size()) {
patterns.add(newEntry);
saveEntry = newEntry;
} else {
patterns.add(lastIndex, newEntry);
}
}
if (changeLastEntry) {
lastEntry = newEntry;
}
}
private final int findLastEntry(PatternEntry entry,
StringBuffer excessChars) throws ParseException
{
if (entry == null)
return 0;
if (entry.strength != PatternEntry.RESET) {
// Search backwards for string that contains this one;
// most likely entry is last one
int oldIndex = -1;
if ((entry.chars.length() == 1)) {
int index = entry.chars.charAt(0) >> BYTEPOWER;
if ((statusArray[index] &
(BITARRAYMASK << (entry.chars.charAt(0) & BYTEMASK))) != 0) {
oldIndex = patterns.lastIndexOf(entry);
}
} else {
oldIndex = patterns.lastIndexOf(entry);
}
if ((oldIndex == -1))
throw new ParseException("couldn't find last entry: "
+ entry, oldIndex);
return oldIndex + 1;
} else {
int i;
for (i = patterns.size() - 1; i >= 0; --i) {
PatternEntry e = patterns.get(i);
if (e.chars.regionMatches(0,entry.chars,0,
e.chars.length())) {
excessChars.append(entry.chars, e.chars.length(),
entry.chars.length());
break;
}
}
if (i == -1)
throw new ParseException("couldn't find: " + entry, i);
return i + 1;
}
}
}