Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/RuleBasedCollator.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved27* (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved28*29* The original version of this source code and documentation is copyrighted30* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These31* materials are provided under terms of a License Agreement between Taligent32* and Sun. This technology is protected by multiple US and International33* patents. This notice and attribution to Taligent may not be removed.34* Taligent is a registered trademark of Taligent, Inc.35*36*/3738package java.text;3940import java.text.Normalizer;41import java.util.Vector;42import java.util.Locale;4344/**45* The <code>RuleBasedCollator</code> class is a concrete subclass of46* <code>Collator</code> that provides a simple, data-driven, table47* collator. With this class you can create a customized table-based48* <code>Collator</code>. <code>RuleBasedCollator</code> maps49* characters to sort keys.50*51* <p>52* <code>RuleBasedCollator</code> has the following restrictions53* for efficiency (other subclasses may be used for more complex languages) :54* <ol>55* <li>If a special collation rule controlled by a <modifier> is56specified it applies to the whole collator object.57* <li>All non-mentioned characters are at the end of the58* collation order.59* </ol>60*61* <p>62* The collation table is composed of a list of collation rules, where each63* rule is of one of three forms:64* <pre>65* <modifier>66* <relation> <text-argument>67* <reset> <text-argument>68* </pre>69* The definitions of the rule elements is as follows:70* <UL>71* <LI><strong>Text-Argument</strong>: A text-argument is any sequence of72* characters, excluding special characters (that is, common73* whitespace characters [0009-000D, 0020] and rule syntax characters74* [0021-002F, 003A-0040, 005B-0060, 007B-007E]). If those75* characters are desired, you can put them in single quotes76* (e.g. ampersand => '&'). Note that unquoted white space characters77* are ignored; e.g. <code>b c</code> is treated as <code>bc</code>.78* <LI><strong>Modifier</strong>: There are currently two modifiers that79* turn on special collation rules.80* <UL>81* <LI>'@' : Turns on backwards sorting of accents (secondary82* differences), as in French.83* <LI>'!' : Turns on Thai/Lao vowel-consonant swapping. If this84* rule is in force when a Thai vowel of the range85* \U0E40-\U0E44 precedes a Thai consonant of the range86* \U0E01-\U0E2E OR a Lao vowel of the range \U0EC0-\U0EC487* precedes a Lao consonant of the range \U0E81-\U0EAE then88* the vowel is placed after the consonant for collation89* purposes.90* </UL>91* <p>'@' : Indicates that accents are sorted backwards, as in French.92* <LI><strong>Relation</strong>: The relations are the following:93* <UL>94* <LI>'<' : Greater, as a letter difference (primary)95* <LI>';' : Greater, as an accent difference (secondary)96* <LI>',' : Greater, as a case difference (tertiary)97* <LI>'=' : Equal98* </UL>99* <LI><strong>Reset</strong>: There is a single reset100* which is used primarily for contractions and expansions, but which101* can also be used to add a modification at the end of a set of rules.102* <p>'&' : Indicates that the next rule follows the position to where103* the reset text-argument would be sorted.104* </UL>105*106* <p>107* This sounds more complicated than it is in practice. For example, the108* following are equivalent ways of expressing the same thing:109* <blockquote>110* <pre>111* a < b < c112* a < b & b < c113* a < c & a < b114* </pre>115* </blockquote>116* Notice that the order is important, as the subsequent item goes immediately117* after the text-argument. The following are not equivalent:118* <blockquote>119* <pre>120* a < b & a < c121* a < c & a < b122* </pre>123* </blockquote>124* Either the text-argument must already be present in the sequence, or some125* initial substring of the text-argument must be present. (e.g. "a < b & ae <126* e" is valid since "a" is present in the sequence before "ae" is reset). In127* this latter case, "ae" is not entered and treated as a single character;128* instead, "e" is sorted as if it were expanded to two characters: "a"129* followed by an "e". This difference appears in natural languages: in130* traditional Spanish "ch" is treated as though it contracts to a single131* character (expressed as "c < ch < d"), while in traditional German132* a-umlaut is treated as though it expanded to two characters133* (expressed as "a,A < b,B ... &ae;\u00e3&AE;\u00c3").134* [\u00e3 and \u00c3 are, of course, the escape sequences for a-umlaut.]135* <p>136* <strong>Ignorable Characters</strong>137* <p>138* For ignorable characters, the first rule must start with a relation (the139* examples we have used above are really fragments; "a < b" really should be140* "< a < b"). If, however, the first relation is not "<", then all the all141* text-arguments up to the first "<" are ignorable. For example, ", - < a < b"142* makes "-" an ignorable character, as we saw earlier in the word143* "black-birds". In the samples for different languages, you see that most144* accents are ignorable.145*146* <p><strong>Normalization and Accents</strong>147* <p>148* <code>RuleBasedCollator</code> automatically processes its rule table to149* include both pre-composed and combining-character versions of150* accented characters. Even if the provided rule string contains only151* base characters and separate combining accent characters, the pre-composed152* accented characters matching all canonical combinations of characters from153* the rule string will be entered in the table.154* <p>155* This allows you to use a RuleBasedCollator to compare accented strings156* even when the collator is set to NO_DECOMPOSITION. There are two caveats,157* however. First, if the strings to be collated contain combining158* sequences that may not be in canonical order, you should set the collator to159* CANONICAL_DECOMPOSITION or FULL_DECOMPOSITION to enable sorting of160* combining sequences. Second, if the strings contain characters with161* compatibility decompositions (such as full-width and half-width forms),162* you must use FULL_DECOMPOSITION, since the rule tables only include163* canonical mappings.164*165* <p><strong>Errors</strong>166* <p>167* The following are errors:168* <UL>169* <LI>A text-argument contains unquoted punctuation symbols170* (e.g. "a < b-c < d").171* <LI>A relation or reset character not followed by a text-argument172* (e.g. "a < ,b").173* <LI>A reset where the text-argument (or an initial substring of the174* text-argument) is not already in the sequence.175* (e.g. "a < b & e < f")176* </UL>177* If you produce one of these errors, a <code>RuleBasedCollator</code> throws178* a <code>ParseException</code>.179*180* <p><strong>Examples</strong>181* <p>Simple: "< a < b < c < d"182* <p>Norwegian: "< a, A < b, B < c, C < d, D < e, E < f, F183* < g, G < h, H < i, I < j, J < k, K < l, L184* < m, M < n, N < o, O < p, P < q, Q < r, R185* < s, S < t, T < u, U < v, V < w, W < x, X186* < y, Y < z, Z187* < \u00E6, \u00C6188* < \u00F8, \u00D8189* < \u00E5 = a\u030A, \u00C5 = A\u030A;190* aa, AA"191*192* <p>193* To create a <code>RuleBasedCollator</code> object with specialized194* rules tailored to your needs, you construct the <code>RuleBasedCollator</code>195* with the rules contained in a <code>String</code> object. For example:196* <blockquote>197* <pre>198* String simple = "< a< b< c< d";199* RuleBasedCollator mySimple = new RuleBasedCollator(simple);200* </pre>201* </blockquote>202* Or:203* <blockquote>204* <pre>205* String Norwegian = "< a, A < b, B < c, C < d, D < e, E < f, F < g, G < h, H < i, I" +206* "< j, J < k, K < l, L < m, M < n, N < o, O < p, P < q, Q < r, R" +207* "< s, S < t, T < u, U < v, V < w, W < x, X < y, Y < z, Z" +208* "< \u00E6, \u00C6" + // Latin letter ae & AE209* "< \u00F8, \u00D8" + // Latin letter o & O with stroke210* "< \u00E5 = a\u030A," + // Latin letter a with ring above211* " \u00C5 = A\u030A;" + // Latin letter A with ring above212* " aa, AA";213* RuleBasedCollator myNorwegian = new RuleBasedCollator(Norwegian);214* </pre>215* </blockquote>216*217* <p>218* A new collation rules string can be created by concatenating rules219* strings. For example, the rules returned by {@link #getRules()} could220* be concatenated to combine multiple <code>RuleBasedCollator</code>s.221*222* <p>223* The following example demonstrates how to change the order of224* non-spacing accents,225* <blockquote>226* <pre>227* // old rule228* String oldRules = "=\u0301;\u0300;\u0302;\u0308" // main accents229* + ";\u0327;\u0303;\u0304;\u0305" // main accents230* + ";\u0306;\u0307;\u0309;\u030A" // main accents231* + ";\u030B;\u030C;\u030D;\u030E" // main accents232* + ";\u030F;\u0310;\u0311;\u0312" // main accents233* + "< a , A ; ae, AE ; \u00e6 , \u00c6"234* + "< b , B < c, C < e, E & C < d, D";235* // change the order of accent characters236* String addOn = "& \u0300 ; \u0308 ; \u0302";237* RuleBasedCollator myCollator = new RuleBasedCollator(oldRules + addOn);238* </pre>239* </blockquote>240*241* @see Collator242* @see CollationElementIterator243* @author Helena Shih, Laura Werner, Richard Gillam244*/245public class RuleBasedCollator extends Collator{246// IMPLEMENTATION NOTES: The implementation of the collation algorithm is247// divided across three classes: RuleBasedCollator, RBCollationTables, and248// CollationElementIterator. RuleBasedCollator contains the collator's249// transient state and includes the code that uses the other classes to250// implement comparison and sort-key building. RuleBasedCollator also251// contains the logic to handle French secondary accent sorting.252// A RuleBasedCollator has two CollationElementIterators. State doesn't253// need to be preserved in these objects between calls to compare() or254// getCollationKey(), but the objects persist anyway to avoid wasting extra255// creation time. compare() and getCollationKey() are synchronized to ensure256// thread safety with this scheme. The CollationElementIterator is responsible257// for generating collation elements from strings and returning one element at258// a time (sometimes there's a one-to-many or many-to-one mapping between259// characters and collation elements-- this class handles that).260// CollationElementIterator depends on RBCollationTables, which contains the261// collator's static state. RBCollationTables contains the actual data262// tables specifying the collation order of characters for a particular locale263// or use. It also contains the base logic that CollationElementIterator264// uses to map from characters to collation elements. A single RBCollationTables265// object is shared among all RuleBasedCollators for the same locale, and266// thus by all the CollationElementIterators they create.267268/**269* RuleBasedCollator constructor. This takes the table rules and builds270* a collation table out of them. Please see RuleBasedCollator class271* description for more details on the collation rule syntax.272* @see java.util.Locale273* @param rules the collation rules to build the collation table from.274* @exception ParseException A format exception275* will be thrown if the build process of the rules fails. For276* example, build rule "a < ? < d" will cause the constructor to277* throw the ParseException because the '?' is not quoted.278*/279public RuleBasedCollator(String rules) throws ParseException {280this(rules, Collator.CANONICAL_DECOMPOSITION);281}282283/**284* RuleBasedCollator constructor. This takes the table rules and builds285* a collation table out of them. Please see RuleBasedCollator class286* description for more details on the collation rule syntax.287* @see java.util.Locale288* @param rules the collation rules to build the collation table from.289* @param decomp the decomposition strength used to build the290* collation table and to perform comparisons.291* @exception ParseException A format exception292* will be thrown if the build process of the rules fails. For293* example, build rule "a < ? < d" will cause the constructor to294* throw the ParseException because the '?' is not quoted.295*/296RuleBasedCollator(String rules, int decomp) throws ParseException {297setStrength(Collator.TERTIARY);298setDecomposition(decomp);299tables = new RBCollationTables(rules, decomp);300}301302/**303* "Copy constructor." Used in clone() for performance.304*/305private RuleBasedCollator(RuleBasedCollator that) {306setStrength(that.getStrength());307setDecomposition(that.getDecomposition());308tables = that.tables;309}310311/**312* Gets the table-based rules for the collation object.313* @return returns the collation rules that the table collation object314* was created from.315*/316public String getRules()317{318return tables.getRules();319}320321/**322* Returns a CollationElementIterator for the given String.323*324* @param source the string to be collated325* @return a {@code CollationElementIterator} object326* @see java.text.CollationElementIterator327*/328public CollationElementIterator getCollationElementIterator(String source) {329return new CollationElementIterator( source, this );330}331332/**333* Returns a CollationElementIterator for the given CharacterIterator.334*335* @param source the character iterator to be collated336* @return a {@code CollationElementIterator} object337* @see java.text.CollationElementIterator338* @since 1.2339*/340public CollationElementIterator getCollationElementIterator(341CharacterIterator source) {342return new CollationElementIterator( source, this );343}344345/**346* Compares the character data stored in two different strings based on the347* collation rules. Returns information about whether a string is less348* than, greater than or equal to another string in a language.349* This can be overriden in a subclass.350*351* @exception NullPointerException if <code>source</code> or <code>target</code> is null.352*/353public synchronized int compare(String source, String target)354{355if (source == null || target == null) {356throw new NullPointerException();357}358359// The basic algorithm here is that we use CollationElementIterators360// to step through both the source and target strings. We compare each361// collation element in the source string against the corresponding one362// in the target, checking for differences.363//364// If a difference is found, we set <result> to LESS or GREATER to365// indicate whether the source string is less or greater than the target.366//367// However, it's not that simple. If we find a tertiary difference368// (e.g. 'A' vs. 'a') near the beginning of a string, it can be369// overridden by a primary difference (e.g. "A" vs. "B") later in370// the string. For example, "AA" < "aB", even though 'A' > 'a'.371//372// To keep track of this, we use strengthResult to keep track of the373// strength of the most significant difference that has been found374// so far. When we find a difference whose strength is greater than375// strengthResult, it overrides the last difference (if any) that376// was found.377378int result = Collator.EQUAL;379380if (sourceCursor == null) {381sourceCursor = getCollationElementIterator(source);382} else {383sourceCursor.setText(source);384}385if (targetCursor == null) {386targetCursor = getCollationElementIterator(target);387} else {388targetCursor.setText(target);389}390391int sOrder = 0, tOrder = 0;392393boolean initialCheckSecTer = getStrength() >= Collator.SECONDARY;394boolean checkSecTer = initialCheckSecTer;395boolean checkTertiary = getStrength() >= Collator.TERTIARY;396397boolean gets = true, gett = true;398399while(true) {400// Get the next collation element in each of the strings, unless401// we've been requested to skip it.402if (gets) sOrder = sourceCursor.next(); else gets = true;403if (gett) tOrder = targetCursor.next(); else gett = true;404405// If we've hit the end of one of the strings, jump out of the loop406if ((sOrder == CollationElementIterator.NULLORDER)||407(tOrder == CollationElementIterator.NULLORDER))408break;409410int pSOrder = CollationElementIterator.primaryOrder(sOrder);411int pTOrder = CollationElementIterator.primaryOrder(tOrder);412413// If there's no difference at this position, we can skip it414if (sOrder == tOrder) {415if (tables.isFrenchSec() && pSOrder != 0) {416if (!checkSecTer) {417// in french, a secondary difference more to the right is stronger,418// so accents have to be checked with each base element419checkSecTer = initialCheckSecTer;420// but tertiary differences are less important than the first421// secondary difference, so checking tertiary remains disabled422checkTertiary = false;423}424}425continue;426}427428// Compare primary differences first.429if ( pSOrder != pTOrder )430{431if (sOrder == 0) {432// The entire source element is ignorable.433// Skip to the next source element, but don't fetch another target element.434gett = false;435continue;436}437if (tOrder == 0) {438gets = false;439continue;440}441442// The source and target elements aren't ignorable, but it's still possible443// for the primary component of one of the elements to be ignorable....444445if (pSOrder == 0) // primary order in source is ignorable446{447// The source's primary is ignorable, but the target's isn't. We treat ignorables448// as a secondary difference, so remember that we found one.449if (checkSecTer) {450result = Collator.GREATER; // (strength is SECONDARY)451checkSecTer = false;452}453// Skip to the next source element, but don't fetch another target element.454gett = false;455}456else if (pTOrder == 0)457{458// record differences - see the comment above.459if (checkSecTer) {460result = Collator.LESS; // (strength is SECONDARY)461checkSecTer = false;462}463// Skip to the next source element, but don't fetch another target element.464gets = false;465} else {466// Neither of the orders is ignorable, and we already know that the primary467// orders are different because of the (pSOrder != pTOrder) test above.468// Record the difference and stop the comparison.469if (pSOrder < pTOrder) {470return Collator.LESS; // (strength is PRIMARY)471} else {472return Collator.GREATER; // (strength is PRIMARY)473}474}475} else { // else of if ( pSOrder != pTOrder )476// primary order is the same, but complete order is different. So there477// are no base elements at this point, only ignorables (Since the strings are478// normalized)479480if (checkSecTer) {481// a secondary or tertiary difference may still matter482short secSOrder = CollationElementIterator.secondaryOrder(sOrder);483short secTOrder = CollationElementIterator.secondaryOrder(tOrder);484if (secSOrder != secTOrder) {485// there is a secondary difference486result = (secSOrder < secTOrder) ? Collator.LESS : Collator.GREATER;487// (strength is SECONDARY)488checkSecTer = false;489// (even in french, only the first secondary difference within490// a base character matters)491} else {492if (checkTertiary) {493// a tertiary difference may still matter494short terSOrder = CollationElementIterator.tertiaryOrder(sOrder);495short terTOrder = CollationElementIterator.tertiaryOrder(tOrder);496if (terSOrder != terTOrder) {497// there is a tertiary difference498result = (terSOrder < terTOrder) ? Collator.LESS : Collator.GREATER;499// (strength is TERTIARY)500checkTertiary = false;501}502}503}504} // if (checkSecTer)505506} // if ( pSOrder != pTOrder )507} // while()508509if (sOrder != CollationElementIterator.NULLORDER) {510// (tOrder must be CollationElementIterator::NULLORDER,511// since this point is only reached when sOrder or tOrder is NULLORDER.)512// The source string has more elements, but the target string hasn't.513do {514if (CollationElementIterator.primaryOrder(sOrder) != 0) {515// We found an additional non-ignorable base character in the source string.516// This is a primary difference, so the source is greater517return Collator.GREATER; // (strength is PRIMARY)518}519else if (CollationElementIterator.secondaryOrder(sOrder) != 0) {520// Additional secondary elements mean the source string is greater521if (checkSecTer) {522result = Collator.GREATER; // (strength is SECONDARY)523checkSecTer = false;524}525}526} while ((sOrder = sourceCursor.next()) != CollationElementIterator.NULLORDER);527}528else if (tOrder != CollationElementIterator.NULLORDER) {529// The target string has more elements, but the source string hasn't.530do {531if (CollationElementIterator.primaryOrder(tOrder) != 0)532// We found an additional non-ignorable base character in the target string.533// This is a primary difference, so the source is less534return Collator.LESS; // (strength is PRIMARY)535else if (CollationElementIterator.secondaryOrder(tOrder) != 0) {536// Additional secondary elements in the target mean the source string is less537if (checkSecTer) {538result = Collator.LESS; // (strength is SECONDARY)539checkSecTer = false;540}541}542} while ((tOrder = targetCursor.next()) != CollationElementIterator.NULLORDER);543}544545// For IDENTICAL comparisons, we use a bitwise character comparison546// as a tiebreaker if all else is equal547if (result == 0 && getStrength() == IDENTICAL) {548int mode = getDecomposition();549Normalizer.Form form;550if (mode == CANONICAL_DECOMPOSITION) {551form = Normalizer.Form.NFD;552} else if (mode == FULL_DECOMPOSITION) {553form = Normalizer.Form.NFKD;554} else {555return source.compareTo(target);556}557558String sourceDecomposition = Normalizer.normalize(source, form);559String targetDecomposition = Normalizer.normalize(target, form);560return sourceDecomposition.compareTo(targetDecomposition);561}562return result;563}564565/**566* Transforms the string into a series of characters that can be compared567* with CollationKey.compareTo. This overrides java.text.Collator.getCollationKey.568* It can be overriden in a subclass.569*/570public synchronized CollationKey getCollationKey(String source)571{572//573// The basic algorithm here is to find all of the collation elements for each574// character in the source string, convert them to a char representation,575// and put them into the collation key. But it's trickier than that.576// Each collation element in a string has three components: primary (A vs B),577// secondary (A vs A-acute), and tertiary (A' vs a); and a primary difference578// at the end of a string takes precedence over a secondary or tertiary579// difference earlier in the string.580//581// To account for this, we put all of the primary orders at the beginning of the582// string, followed by the secondary and tertiary orders, separated by nulls.583//584// Here's a hypothetical example, with the collation element represented as585// a three-digit number, one digit for primary, one for secondary, etc.586//587// String: A a B \u00e9 <--(e-acute)588// Collation Elements: 101 100 201 510589//590// Collation Key: 1125<null>0001<null>1010591//592// To make things even trickier, secondary differences (accent marks) are compared593// starting at the *end* of the string in languages with French secondary ordering.594// But when comparing the accent marks on a single base character, they are compared595// from the beginning. To handle this, we reverse all of the accents that belong596// to each base character, then we reverse the entire string of secondary orderings597// at the end. Taking the same example above, a French collator might return598// this instead:599//600// Collation Key: 1125<null>1000<null>1010601//602if (source == null)603return null;604605if (primResult == null) {606primResult = new StringBuffer();607secResult = new StringBuffer();608terResult = new StringBuffer();609} else {610primResult.setLength(0);611secResult.setLength(0);612terResult.setLength(0);613}614int order = 0;615boolean compareSec = (getStrength() >= Collator.SECONDARY);616boolean compareTer = (getStrength() >= Collator.TERTIARY);617int secOrder = CollationElementIterator.NULLORDER;618int terOrder = CollationElementIterator.NULLORDER;619int preSecIgnore = 0;620621if (sourceCursor == null) {622sourceCursor = getCollationElementIterator(source);623} else {624sourceCursor.setText(source);625}626627// walk through each character628while ((order = sourceCursor.next()) !=629CollationElementIterator.NULLORDER)630{631secOrder = CollationElementIterator.secondaryOrder(order);632terOrder = CollationElementIterator.tertiaryOrder(order);633if (!CollationElementIterator.isIgnorable(order))634{635primResult.append((char) (CollationElementIterator.primaryOrder(order)636+ COLLATIONKEYOFFSET));637638if (compareSec) {639//640// accumulate all of the ignorable/secondary characters attached641// to a given base character642//643if (tables.isFrenchSec() && preSecIgnore < secResult.length()) {644//645// We're doing reversed secondary ordering and we've hit a base646// (non-ignorable) character. Reverse any secondary orderings647// that applied to the last base character. (see block comment above.)648//649RBCollationTables.reverse(secResult, preSecIgnore, secResult.length());650}651// Remember where we are in the secondary orderings - this is how far652// back to go if we need to reverse them later.653secResult.append((char)(secOrder+ COLLATIONKEYOFFSET));654preSecIgnore = secResult.length();655}656if (compareTer) {657terResult.append((char)(terOrder+ COLLATIONKEYOFFSET));658}659}660else661{662if (compareSec && secOrder != 0)663secResult.append((char)664(secOrder + tables.getMaxSecOrder() + COLLATIONKEYOFFSET));665if (compareTer && terOrder != 0)666terResult.append((char)667(terOrder + tables.getMaxTerOrder() + COLLATIONKEYOFFSET));668}669}670if (tables.isFrenchSec())671{672if (preSecIgnore < secResult.length()) {673// If we've accumulated any secondary characters after the last base character,674// reverse them.675RBCollationTables.reverse(secResult, preSecIgnore, secResult.length());676}677// And now reverse the entire secResult to get French secondary ordering.678RBCollationTables.reverse(secResult, 0, secResult.length());679}680primResult.append((char)0);681secResult.append((char)0);682secResult.append(terResult.toString());683primResult.append(secResult.toString());684685if (getStrength() == IDENTICAL) {686primResult.append((char)0);687int mode = getDecomposition();688if (mode == CANONICAL_DECOMPOSITION) {689primResult.append(Normalizer.normalize(source, Normalizer.Form.NFD));690} else if (mode == FULL_DECOMPOSITION) {691primResult.append(Normalizer.normalize(source, Normalizer.Form.NFKD));692} else {693primResult.append(source);694}695}696return new RuleBasedCollationKey(source, primResult.toString());697}698699/**700* Standard override; no change in semantics.701*/702public Object clone() {703// if we know we're not actually a subclass of RuleBasedCollator704// (this class really should have been made final), bypass705// Object.clone() and use our "copy constructor". This is faster.706if (getClass() == RuleBasedCollator.class) {707return new RuleBasedCollator(this);708}709else {710RuleBasedCollator result = (RuleBasedCollator) super.clone();711result.primResult = null;712result.secResult = null;713result.terResult = null;714result.sourceCursor = null;715result.targetCursor = null;716return result;717}718}719720/**721* Compares the equality of two collation objects.722* @param obj the table-based collation object to be compared with this.723* @return true if the current table-based collation object is the same724* as the table-based collation object obj; false otherwise.725*/726public boolean equals(Object obj) {727if (obj == null) return false;728if (!super.equals(obj)) return false; // super does class check729RuleBasedCollator other = (RuleBasedCollator) obj;730// all other non-transient information is also contained in rules.731return (getRules().equals(other.getRules()));732}733734/**735* Generates the hash code for the table-based collation object736*/737public int hashCode() {738return getRules().hashCode();739}740741/**742* Allows CollationElementIterator access to the tables object743*/744RBCollationTables getTables() {745return tables;746}747748// ==============================================================749// private750// ==============================================================751752final static int CHARINDEX = 0x70000000; // need look up in .commit()753final static int EXPANDCHARINDEX = 0x7E000000; // Expand index follows754final static int CONTRACTCHARINDEX = 0x7F000000; // contract indexes follow755final static int UNMAPPED = 0xFFFFFFFF;756757private final static int COLLATIONKEYOFFSET = 1;758759private RBCollationTables tables = null;760761// Internal objects that are cached across calls so that they don't have to762// be created/destroyed on every call to compare() and getCollationKey()763private StringBuffer primResult = null;764private StringBuffer secResult = null;765private StringBuffer terResult = null;766private CollationElementIterator sourceCursor = null;767private CollationElementIterator targetCursor = null;768}769770771