public class RuleBasedCollator extends Collator
RuleBasedCollator类是的具体子类Collator ,提供了一个简单的,数据驱动的表collator。
有了这个类,你可以创建一个自定义的基于表的Collator 。
RuleBasedCollator将RuleBasedCollator映射到排序键。
RuleBasedCollator对效率有以下限制(其他子类可用于更复杂的语言):
排序规则表由排序规则列表组成,其中每个规则是三种形式之一:
<modifier>
<relation> <text-argument>
<reset> <text-argument>
规则要素的定义如下:
b c被视为bc 。 '@':表示口音向后排序,如法语。
'&':表示下一个规则遵循重置文本参数进行排序的位置。
这听起来比实际上更复杂。 例如,以下是表达相同内容的等效方法:
请注意,顺序很重要,因为随后的项目会在text-argument之后立即进行。 以下不等效:a < b < c a < b & b < c a < c & a < b
text-argument必须已经存在于序列中,或者text-argument的一些初始子字符串必须存在。 (例如“a <b&ae <e”是有效的,因为在“ae”被重置之前的序列中存在“a”)。 在后一种情况下,“ae”不输入并视为单个字符; 相反,“e”按照扩展为两个字符进行排序:“a”后跟“e”。 这种差异出现在自然语言中:在传统的西班牙语“ch”被视为一个单一的字符(表示为“c <ch <d”),而在传统的德语a-umlaut被看作是扩展到两个字符(表示为“a,A <b,B ...&ae; \ u00e3&AE; \ u00c3”)。 [\ u00e3和\ u00c3当然是a-umlaut的转义序列。]a < b & a < c a < c & a < b
不可思议的人物
对于可忽略的角色,第一条规则必须从一个关系开始(我们上面使用的例子是真正的片段;“a <b”应该是“<a <b”)。 但是,如果第一个关系不是“<”,则所有直到第一个“<”的文本参数都是可忽略的。 例如,“, - <a <b”使“ - ”成为一个可忽略的字符,正如我们前面在“黑鸟”一词中所看到的那样。 在不同语言的样本中,您会发现大多数口音都是可忽略的。
规范化和口音
RuleBasedCollator自动处理其规则表以包括重组字符和组合字符版本的重音字符。 即使提供的规则字符串只包含基本字符和单独的组合重音符号,则将在表格中输入与规则字符串中所有符号的所有规范组合匹配的预先组合的重音字符。
这使您可以使用RuleBasedCollator来比较重音字符串,即使将整理器设置为NO_DECOMPOSITION。 但是有两个注意事项。 首先,如果要整理的字符串包含可能不符合规范的组合序列,则应将归一化器设置为CANONICAL_DECOMPOSITION或FULL_DECOMPOSITION以启用对组合序列的排序。 第二,如果字符串包含具有兼容性分解的字符(例如全宽和半角形式),则必须使用FULL_DECOMPOSITION,因为规则表仅包括规范映射。
错误
以下是错误:
RuleBasedCollator抛出一个ParseException 。
例子
简单:“<a <b <c <d”
挪威语:“<a,A <b,B <c,C <d,D <e,E <f,F <g,G <h,H <i,I <j,J <k,K < L <m,M <n,N <o,O <p,P <q,Q <r,R <s,S <t,T <u,U <v,V <w,W <x, y,Y <z,Z <\ u00E6,\ u00C6 <\ u00F8,\ u00D8 <\ u00E5 = a \ u030A,\ u00C5 = A \ u030A; aa,AA“
要创建一个RuleBasedCollator有适合您的需要的专门规则对象,在构造RuleBasedCollator与包含在规则String对象。 例如:
要么:String simple = "< a< b< c< d"; RuleBasedCollator mySimple = new RuleBasedCollator(simple);
String Norwegian = "< a, A < b, B < c, C < d, D < e, E < f, F < g, G < h, H < i, I" +
"< j, J < k, K < l, L < m, M < n, N < o, O < p, P < q, Q < r, R" +
"< s, S < t, T < u, U < v, V < w, W < x, X < y, Y < z, Z" +
"< \u00E6, \u00C6" + // Latin letter ae & AE
"< \u00F8, \u00D8" + // Latin letter o & O with stroke
"< \u00E5 = a\u030A," + // Latin letter a with ring above
" \u00C5 = A\u030A;" + // Latin letter A with ring above
" aa, AA";
RuleBasedCollator myNorwegian = new RuleBasedCollator(Norwegian);
可以通过连接规则字符串来创建新的归类规则字符串。 例如,由getRules()返回的规则可以并置到多个RuleBasedCollator组合。
以下示例演示如何更改非间距重音符的顺序,
// old rule
String oldRules = "=\u0301;\u0300;\u0302;\u0308" // main accents
+ ";\u0327;\u0303;\u0304;\u0305" // main accents
+ ";\u0306;\u0307;\u0309;\u030A" // main accents
+ ";\u030B;\u030C;\u030D;\u030E" // main accents
+ ";\u030F;\u0310;\u0311;\u0312" // main accents
+ "< a , A ; ae, AE ; \u00e6 , \u00c6"
+ "< b , B < c, C < e, E & C < d, D";
// change the order of accent characters
String addOn = "& \u0300 ; \u0308 ; \u0302";
RuleBasedCollator myCollator = new RuleBasedCollator(oldRules + addOn);
Collator , CollationElementIterator
CANONICAL_DECOMPOSITION, FULL_DECOMPOSITION, IDENTICAL, NO_DECOMPOSITION, PRIMARY, SECONDARY, TERTIARY| Constructor and Description |
|---|
RuleBasedCollator(String rules)
RuleBasedCollator构造函数。
|
| Modifier and Type | Method and Description |
|---|---|
Object |
clone()
标准覆盖
语义不变。
|
int |
compare(String source, String target)
基于归类规则比较存储在两个不同字符串中的字符数据。
|
boolean |
equals(Object obj)
比较两个对照对象的相等性。
|
CollationElementIterator |
getCollationElementIterator(CharacterIterator source)
返回给定CharacterIterator的CollationElementIterator。
|
CollationElementIterator |
getCollationElementIterator(String source)
返回给定String的CollationElementIterator。
|
CollationKey |
getCollationKey(String source)
将字符串转换成可以与CollationKey.compareTo进行比较的一系列字符。
|
String |
getRules()
获取排序规则对象的基于表的规则。
|
int |
hashCode()
生成基于表的归类对象的哈希码
|
compare, equals, getAvailableLocales, getDecomposition, getInstance, getInstance, getStrength, setDecomposition, setStrengthfinalize, getClass, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongpublic RuleBasedCollator(String rules) throws ParseException
rules - 用于构建排序规则表的排序规则。
ParseException - 如果规则的构建过程失败,将抛出格式异常。
例如,构建规则“a <?<d”将导致构造函数抛出ParseException,因为'?'
没有引用。
Locale
public String getRules()
public CollationElementIterator getCollationElementIterator(String source)
source - 要整理的字符串
CollationElementIterator对象
CollationElementIterator
public CollationElementIterator getCollationElementIterator(CharacterIterator source)
source - 要整理的字符迭代器
CollationElementIterator对象
CollationElementIterator
public int compare(String source, String target)
compare在
Collator
source - 源字符串。
target - 目标字符串。
NullPointerException - 如果
source或
target为空。
CollationKey , Collator.getCollationKey(java.lang.String)
public CollationKey getCollationKey(String source)
getCollationKey在
Collator
source - 要转换为归类密钥的字符串。
CollationKey , Collator.compare(java.lang.String, java.lang.String)
public boolean equals(Object obj)
equals在接口
Comparator<Object>
equals在
Collator
obj - 要与之进行比较的基于表的归类对象。
Object.hashCode() , HashMap
public int hashCode()
hashCode在
Collator
Object.equals(java.lang.Object) ,
System.identityHashCode(java.lang.Object)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.