Most visited

Recently visited

Added in API level 1

RuleBasedCollator

public class RuleBasedCollator
extends Collator

java.lang.Object
   ↳ java.text.Collator
     ↳ java.text.RuleBasedCollator


RuleBasedCollator类是RuleBasedCollator的一个具体子类,它提供了一个简单的数据驱动的表Collator器。 通过这个课程,您可以创建一个基于表格的定制Collator RuleBasedCollator映射字符以排序键。

RuleBasedCollator对效率有以下限制(其他子类可用于更复杂的语言):

  1. If a special collation rule controlled by a <modifier> is specified it applies to the whole collator object.
  2. All non-mentioned characters are at the end of the collation order.

整理表由整理规则列表组成,其中每个规则是以下三种形式之一:

    <modifier>
    <relation> <text-argument>
    <reset> <text-argument>
 
The definitions of the rule elements is as follows:

这听起来比实践中更复杂。 例如,以下是表达同一事物的等效方式:

 a < b < c
 a < b & b < c
 a < c & a < b
 
Notice that the order is important, as the subsequent item goes immediately after the text-argument. The following are not equivalent:
 a < b & a < c
 a < c & a < b
 
Either the text-argument must already be present in the sequence, or some initial substring of the text-argument must be present. (e.g. "a < b & ae < e" is valid since "a" is present in the sequence before "ae" is reset). In this latter case, "ae" is not entered and treated as a single character; instead, "e" is sorted as if it were expanded to two characters: "a" followed by an "e". This difference appears in natural languages: in traditional Spanish "ch" is treated as though it contracts to a single character (expressed as "c < ch < d"), while in traditional German a-umlaut is treated as though it expanded to two characters (expressed as "a,A < b,B ... &ae;\u00e3&AE;\u00c3"). [\u00e3 and \u00c3 are, of course, the escape sequences for a-umlaut.]

可忽略的字符

对于可忽略的字符,第一条规则必须以关系开始(我们上面使用的例子实际上是片段;“a <b”实际上应该是“<a <b”)。 但是,如果第一个关系不是“<”,那么直到第一个“<”的所有文本参数都是可以忽略的。 例如,“, - <a <b”使“ - ”成为一个可以忽略的字符,正如我们前面在“黑鸟”一词中所看到的那样。 在不同语言的样本中,您会发现大部分口音都是可以忽略的。

规范化和口音

RuleBasedCollator自动处理其规则表,以包括重音字符的预先组合字符和组合字符版本。 即使提供的规则字符串只包含基本字符和单独的组合重音字符,预先组合的重音符号也会在表中与规则字符串中的所有规范组合字符匹配。

这允许您使用RuleBasedCollator来比较重音字符串,即使在collator设置为NO_DECOMPOSITION时也是如此。 但是有两个警告。 首先,如果要整理的字符串包含可能不是规范顺序的组合序列,则应该将整理器设置为CANONICAL_DECOMPOSITION或FULL_DECOMPOSITION以启用组合序列的排序。 其次,如果字符串包含具有兼容性分解的字符(例如全角和半角表单),则必须使用FULL_DECOMPOSITION,因为规则表仅包含规范映射。

错误

以下是错误:

If you produce one of these errors, a RuleBasedCollator throws a ParseException.

例子

简单:“<a <b <c <d”

挪威语:“<a,A <b,B <c,C <d,D <e,E <f,F <g,G <h,H <i, L <m,M <n,N <0,O <p,P <q,Q <r,R <s,S <t,T <u,U <v, y,y <z,z <\ u00E6,\ u00C6 <\ u00F8,\ u00D8 <\ u00E5 = a \ u003A,\ u00C5 = A \ u30A; aa,AA“

要创建一个RuleBasedCollator有适合您的需要的专门规则对象,在构造RuleBasedCollator与包含在规则String对象。 例如:

 String simple = "< a< b< c< d";
 RuleBasedCollator mySimple = new RuleBasedCollator(simple);
 
Or:
 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);
 

也可以看看:

Summary

Inherited constants

From class java.text.Collator

Public constructors

RuleBasedCollator(String rules)

RuleBasedCollator构造函数。

Public methods

Object clone()

标准覆盖; 语义没有变化。

int compare(String source, String target)

根据排序规则比较存储在两个不同字符串中的字符数据。

boolean equals(Object obj)

比较两个排序对象的相等性。

CollationElementIterator getCollationElementIterator(String source)

为给定的字符串返回一个CollationElementIterator。

CollationElementIterator getCollationElementIterator(CharacterIterator source)

为给定的字符串返回一个CollationElementIterator。

CollationKey getCollationKey(String source)

将字符串转换为一系列可与CollationKey.compareTo进行比较的字符。

String getRules()

获取排序规则对象的基于表格的规则。

int hashCode()

为基于表格的排序规则对象生成散列码

Inherited methods

From class java.text.Collator
From class java.lang.Object
From interface java.util.Comparator

Public constructors

RuleBasedCollator

Added in API level 1
RuleBasedCollator (String rules)

RuleBasedCollator构造函数。 这取得了表规则,并从它们中构建了一个归类表。 有关排序规则语法的更多详细信息,请参阅RuleBasedCollator类描述。

Parameters
rules String: the collation rules to build the collation table from.
Throws
ParseException A format exception will be thrown if the build process of the rules fails. For example, build rule "a < ? < d" will cause the constructor to throw the ParseException because the '?' is not quoted.

也可以看看:

Public methods

clone

Added in API level 1
Object clone ()

标准覆盖; 语义没有变化。

Returns
Object a shallow copy of this collator.

compare

Added in API level 1
int compare (String source, 
                String target)

根据排序规则比较存储在两个不同字符串中的字符数据。 返回有关字符串是否小于,大于或等于某种语言中的另一个字符串的信息。 这可以在子类中重写。

Parameters
source String: the source string.
target String: the target string.
Returns
int Returns an integer value. Value is less than zero if source is less than target, value is zero if source and target are equal, value is greater than zero if source is greater than target.
Throws
NullPointerException if source or target is null.

equals

Added in API level 1
boolean equals (Object obj)

比较两个排序对象的相等性。

Parameters
obj Object: the table-based collation object to be compared with this.
Returns
boolean true if the current table-based collation object is the same as the table-based collation object obj; false otherwise.

getCollationElementIterator

Added in API level 1
CollationElementIterator getCollationElementIterator (String source)

为给定的字符串返回一个CollationElementIterator。

Parameters
source String
Returns
CollationElementIterator

也可以看看:

getCollationElementIterator

Added in API level 1
CollationElementIterator getCollationElementIterator (CharacterIterator source)

为给定的字符串返回一个CollationElementIterator。

Parameters
source CharacterIterator
Returns
CollationElementIterator

也可以看看:

getCollationKey

Added in API level 1
CollationKey getCollationKey (String source)

将字符串转换为一系列可与CollationKey.compareTo进行比较的字符。 这将覆盖java.text.Collator.getCollationKey。 它可以在一个子类中被覆盖。

Parameters
source String: the string to be transformed into a collation key.
Returns
CollationKey the CollationKey for the given String based on this Collator's collation rules. If the source String is null, a null CollationKey is returned.

getRules

Added in API level 1
String getRules ()

获取排序规则对象的基于表格的规则。

在Android上,除非使用 RuleBasedCollator(String)构造此实例,否则返回的字符串将为空。

Returns
String returns the collation rules that the table collation object was created from.

hashCode

Added in API level 1
int hashCode ()

为基于表格的排序规则对象生成散列码

Returns
int a hash code value for this object.

Hooray!